3

我正在尝试使用 VBA 将项目添加到 Sharepoint 列表。我不希望用户必须安装任何东西,所以我只是使用 Microsoft Soap 类型库。代码如下:

Sub test()

Dim soap As MSSOAPLib.SoapClient
Dim XMLstr As String
Dim listid As String
Dim listname As String

Set soap = New SoapClient
Call soap.mssoapinit(bstrwsdlfile:="http://wss/mySharepointSite/_vti_bin/lists.asmx?wsdl")


listid = "{e285aa1a-my-list-ID-d446cdbf091e}"
listname = "thisList"

XMLstr = "<Method ID='1' Cmd='New'>" & _
            "<Field Name='ID'>New</Field>" & _
            "<Field Name='personID'>1337</Field>" & _
        "</Method>"

soap.UpdateListItems listid, XMLstr

End Sub

soap.UpdateListItems无论我是使用listid还是listname作为第一个参数,我都会在线上收到“类型不匹配”错误。我尝试阅读 WSDL 以确定应该传递哪种类型的参数,但我不明白。我应该在这里经过什么?

编辑:我通过使用 Microsoft Soap Type Library 3.0 来实现它,改变MSSOAPLib.SoapClient->MSSOAPLib30.SoapClient30bstrwsdlfile-> par_wsdlfile,并围绕XMLstr

<Batch OnError='continue' ListVersion='1' ViewName='" & ListView & "'>

...

</Batch>

仍在尝试找出一种无需用户安装 MSSoap 3.0 的方法。

4

2 回答 2

3

我通过将 XML 作为 HTTP POST 发送并通过 MSXML2.XMLHTTP 提交来解决了这个问题。代码如下:

Function updateSharePointList(listURL as string, list As String) As DOMDocument

Dim xmlhtp As New MSXML2.XMLHTTP
Dim XMLDOC As New DOMDocument
Dim xmlstr as String

xmlstr = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" & _
  "<soap12:Body>" & _
    "<UpdateListItems xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">" & _
      "<listName>" & list & "</listName>" & _
      "<updates>" & _
        "<Batch OnError='continue' ListVersion='1'>" & _
        "<Method ID='1' Cmd='New'>" & _
            "<Field Name='ID'>New</Field>" & _
            'all of your field updates go here, e.g.: 
            "<Field Name='userID'>1337</Field>" & _
            "<Field Name='comment'>first!</Field>" & _
        "</Method>" & _
        "</Batch>" & _
        "</updates>" & _
    "</UpdateListItems>" & _
  "</soap12:Body>" & _
"</soap12:Envelope>"

With xmlhtp
        .Open "POST", listURL, False
        .setRequestHeader "Host", "wss"
        .setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"

        .send xmlstr

        XMLDOC.LoadXML .responseText
        Set updateSharePointList = XMLDOC

End With

End Function

这需要对 Microsoft XML(我使用“Microsoft XML,v6.0”)的引用,AFAIK 在任何标准的 VBA 引用集中。不需要 DLL 注册。该函数返回一个 DOMDocument,其结果 XML 由 返回UpdateListItems,您可以对其进行解析以进行错误检查。

于 2012-06-20T17:32:25.940 回答
0

从您的代码中删除这一行:<Field Name='ID'>New</Field>

于 2012-07-09T12:43:31.993 回答