You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NewtonSoft与Visual Basic操作SharePoint 365:获取文件ID遇阻

问题:通过Drive ID和文件名获取文件ID(用于SharePoint元数据更新)

我的最终目标是在上传带默认值的新文件后更新其元数据。目前已用Visual Basic实现文档创建并上传至同步文件夹的功能,计划采用NewtonSoft.json和System.net完成元数据更新步骤,已完成以下操作:

  • 在Azure Active Directory中创建应用并生成密钥;
  • 借助网络代码和上述ID生成访问令牌,该步骤已验证可行。

已实现的代码

生成访问令牌的代码

Public Function GetAccessToken() As String
    Dim tenantId = "My Tenant ID"
    Dim clientId = "MY Client ID"
    Dim authorityUri = $"https://login.microsoftonline.com/{tenantId}"
    Dim redirectUri = "http://localhost"
    Dim scopes = New List(Of String) From {
        "https://myaddress.sharepoint.com/.default"
    }
    Dim clientSecret = "My Secret ID"
    Dim confidentialClient = ConfidentialClientApplicationBuilder.Create(clientId).WithClientSecret(clientSecret).WithAuthority(New Uri(authorityUri)).WithRedirectUri(redirectUri).Build()
    Dim accessTokenRequest = confidentialClient.AcquireTokenForClient(scopes)
    Dim accessToken = accessTokenRequest.ExecuteAsync().Result.AccessToken
    'MsgBox(accessToken)

    Return accessToken
End Function

获取Drive ID的代码

Public Function GetDriveID(ByVal strMainSite As String) As String
        Dim strDriveId As String = ""
        Dim url As String = "https://graph.microsoft.com/v1.0/drives/{drive-id}"
        'Replace {drive-id} with the actual ID of the SharePoint Drive you want to access.

        Dim client As New WebClient()
        strAccessToken = GetAccessToken()
        client.Headers.Add(HttpRequestHeader.Authorization, "Bearer  " & strAccessToken)
        'Replace {access-token} with a valid access token obtained from SharePoint.

        Dim response As String = client.DownloadString(url)
        Dim jsonObject As JObject = JObject.Parse(response)

        Dim driveId As String = jsonObject("id").ToString()

        Return driveId
End Function

SharePointClient类代码

Public Class SharePointClient
        Private ReadOnly baseUrl As String
        Private ReadOnly httpClient As HttpClient

        Public Sub New(baseUrl As String, accessToken As String)
            Me.baseUrl = baseUrl.TrimEnd("/")
            Me.httpClient = New HttpClient()
            Me.httpClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken)
        End Sub

        Public Async Function GetDriveIdFromNameAsync(siteId As String, driveName As String) As Task(Of String)
            Dim endpoint As String = $"{baseUrl}/sites/{siteId}/drives"
            'Me.httpClient.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", strAccessToken)
            Dim response As HttpResponseMessage = Await httpClient.GetAsync(endpoint)

            If response.IsSuccessStatusCode Then
                Dim json As JObject = JObject.Parse(Await response.Content.ReadAsStringAsync())
                Dim drives As JArray = json("value")

                For Each drive As JObject In drives
                    If drive("name").ToString().Equals(driveName, StringComparison.OrdinalIgnoreCase) Then
                        Return drive("id").ToString()
                    End If
                Next
            End If

            Return Nothing
        End Function
End Class

当前卡点

目前卡在通过Drive ID和文件名获取文件ID的环节,而文件ID是后续更新元数据必需的,寻求解决方案。

内容的提问来源于stack exchange,提问作者itsrainman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 17:23:08