通过VBA在Sharepoint列表中添加和更新单个项目

时间:2014-03-17 09:31:25

标签: excel list vba sharepoint

我正在尝试通过VBA添加和/或更新共享点列表中的单个项目,我发现了一个类似的问题: Import Sharepoint 2010 list data from Excel table using VBA

但我不得不说我只能用这段代码删除项目,而且我不熟悉这段代码,以了解我的添加方式。

提前致谢

编辑:我也在这里:http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx但我还是不明白

1 个答案:

答案 0 :(得分:5)

Sub Add_Item(ListName As String, SharepointUrl As String, ValueVar As String, FieldNameVar As String)

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='3' Cmd='New'><Field Name='ID'>New</Field><Field Name=" + FieldNameVar + ">" + ValueVar + "</Field></Method></Batch>"


objXMLHTTP.Open "POST", SharepointUrl + "_vti_bin/Lists.asmx", False
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

现在我明白了。这是您可以将项添加到sharepoint列表的方法。 FieldNameVar是您必须放入某个字段的字段的名称(例如,此值可以是“标题”),而ValueVar是您放在FieldNameVar字段中的值。