从 VBA 调用 Sharepoint Web 服务时,UpdateListItems 采用什么参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11092273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What arguments does UpdateListItems take when calling Sharepoint web service from VBA?
提问by sigil
I'm trying to add an item to a Sharepoint list using VBA. I don't want the user to have to install anything, so I'm just using Microsoft Soap Type Library. Code as follows:
我正在尝试使用 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
I keep getting a "Type Mismatch" error on the soap.UpdateListItems
line, regardless of whether I use listid
or listname
as the first parameter. I tried reading the WSDL to determine what type of parameter should be passed, but I don't understand it. What should I be passing here?
soap.UpdateListItems
无论我是使用listid
还是listname
作为第一个参数,我都不断收到“类型不匹配”错误。我尝试阅读 WSDL 以确定应该传递什么类型的参数,但我不明白。我应该在这里通过什么?
EDIT: I got it to work by using Microsoft Soap Type Library 3.0 instead, changing MSSOAPLib.SoapClient
->MSSOAPLib30.SoapClient30
and bstrwsdlfile
->par_wsdlfile
, and surrounding XMLstr
with:
编辑:我通过使用 Microsoft Soap Type Library 3.0 来让它工作,改变MSSOAPLib.SoapClient
->MSSOAPLib30.SoapClient30
和bstrwsdlfile
-> par_wsdlfile
,并围绕XMLstr
:
<Batch OnError='continue' ListVersion='1' ViewName='" & ListView & "'>
...
</Batch>
Still trying to work out a way to do this without requiring users to install MSSoap 3.0.
仍在尝试找到一种无需用户安装 MSSoap 3.0 即可完成此操作的方法。
采纳答案by sigil
I solved this by sending the XML as an HTTP POST, submitted via MSXML2.XMLHTTP. Code as follows:
我通过将 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
This requires a reference to Microsoft XML (I used "Microsoft XML, v6.0"), which AFAIK is in any standard set of VBA references. No DLL registration is needed. The function returns a DOMDocument with the result XML returned by UpdateListItems
, which you can parse to do error checking.
这需要对 Microsoft XML(我使用“Microsoft XML,v6.0”)的引用,AFAIK 在任何标准的 VBA 引用集中。不需要 DLL 注册。该函数返回一个 DOMDocument,其结果 XML 由 返回UpdateListItems
,您可以对其进行解析以进行错误检查。
回答by Moe
remove this line from your code: <Field Name='ID'>New</Field>
从您的代码中删除此行: <Field Name='ID'>New</Field>