通过 Excel/VBA 更新 Sharepoint Online (o365) 中的列表

通过 Excel/VBA 更新 Sharepoint Online (o365) 中的列表

我目前正在使用 VBA 通过 SOAP 更新/创建新的 SharePoint 列表项。

我的 IT 部门建议他们迁移到 SharePoint Online,并且相同的代码不再起作用,我认为这是由于身份验证过程不同造成的。

我花了将近四天的时间尝试通过 VBA 为 o365 寻找替代解决方案,但不幸的是,我没有太多运气。

我希望有人可以提供帮助,我已经将我原始的 VBA/SOAP 代码作为我尝试复制的参考。

非常感谢,里斯

Sub addItemtoList()

    Dim LISTNAME As String
    Dim SharepointUrl As String
    Dim ValueVar As String
    Dim FieldNameVar As String
    Dim CurUserName As String, CurPassword As String
    CurUserName = "user.name"
    CurPassword = "password123"

    LISTNAME = "SharepointlistName"      'Or Guild

    SharepointUrl = "https://xxxxxxxx.sharepoint.com/sites/au/finance/xxxxxx/"
    ValueVar = "Testing Value Add"
    FieldNameVar = "'Title'"

    Dim objXMLHTTP As MSXML2.XMLHTTP

    Dim strListNameOrGuid As String
    Dim strBatchXml As String
    Dim strSoapBody As String

    Set objXMLHTTP = New MSXML2.XMLHTTP

    strListNameOrGuid = LISTNAME


    'Add New Item'
    strBatchXml = "<Batch OnError='Continue'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name=" + FieldNameVar + ">" + ValueVar + "</Field></Method></Batch>"

    objXMLHTTP.Open "POST", SharepointUrl + "_vti_bin/Lists.asmx", False, CurUserName, CurPassword
    objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
    objXMLHTTP.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"

    strSoapBody = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " _
     & "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " _
     & "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><UpdateListItems " _
     & "xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>" & strListNameOrGuid _
     & "</listName><updates>" & strBatchXml & "</updates></UpdateListItems></soap:Body></soap:Envelope>"

    objXMLHTTP.send strSoapBody

    If objXMLHTTP.Status = 200 Then
    ' Do something with response
    End If

    Set objXMLHTTP = Nothing

    End Sub

相关内容