从 VBA 调用 Sharepoint Web 服务时,UpdateListItems 采用什么参数? [英] What arguments does UpdateListItems take when calling Sharepoint web service from VBA?
问题描述
我正在尝试使用 VBA 将项目添加到 Sharepoint 列表.我不希望用户必须安装任何东西,所以我只是使用 Microsoft Soap Type Library.代码如下:
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:
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 以确定应该传递什么类型的参数,但我不明白.我应该在这里传递什么?
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?
我通过改用 Microsoft Soap Type Library 3.0 使其工作,更改 MSSOAPLib.SoapClient
->MSSOAPLib30.SoapClient30
和 bstrwsdlfile
->par_wsdlfile
,以及将 XMLstr
包围起来:
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:
<Batch OnError='continue' ListVersion='1' ViewName='" & ListView & "'>
...
</Batch>
仍在尝试找到一种无需用户安装 MSSoap 3.0 的方法.
Still trying to work out a way to do this without requiring users to install MSSoap 3.0.
推荐答案
我通过将 XML 作为 HTTP POST 发送,通过 MSXML2.XMLHTTP 提交解决了这个问题.代码如下:
I solved this by sending the XML as an HTTP POST, submitted via MSXML2.XMLHTTP. Code as follows:
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,您可以对其进行解析以进行错误检查.
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.
这篇关于从 VBA 调用 Sharepoint Web 服务时,UpdateListItems 采用什么参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!