我正在尝试使用VBA将项目添加到Sharepoint列表。我不希望用户必须安装任何东西,所以我只是使用Microsoft Soap Type Library。代码如下:
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
- &gt; MSSOAPLib30.SoapClient30
和bstrwsdlfile
- &gt; par_wsdlfile
以及周围环境来使用它XMLstr
with:
<Batch OnError='continue' ListVersion='1' ViewName='" & ListView & "'>
...
</Batch>
仍然试图找到一种方法来执行此操作,而无需用户安装MSSoap 3.0。
答案 0 :(得分: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,其中包含UpdateListItems
返回的结果XML,您可以对其进行解析以进行错误检查。
答案 1 :(得分:0)
从代码中删除此行:<Field Name='ID'>New</Field>