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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22450717/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:22:38  来源:igfitidea点击:

Add and Update Single Item in Sharepoint List via VBA

excellistvbasharepoint

提问by AElxs

I am trying to add and/or update single items in a sharepoint list via VBA and I found a similar question: Import Sharepoint 2010 list data from Excel table using VBA

我正在尝试通过 VBA 添加和/或更新共享点列表中的单个项目,我发现了一个类似的问题: 使用 VBA 从 Excel 表中导入 Sharepoint 2010 列表数据

But i have to say that I can only delete items with this code and I am not familiar with this code to understand how i add.

但我不得不说我只能用这个代码删除项目,我不熟悉这个代码来理解我是如何添加的。

Thanks in advance

提前致谢

Edit: Also I was here: http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspxbut i just dont get it

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

回答by AElxs

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

Now i got it. This is how you can Add items to a sharepoint list. FieldNameVar is the name of a Field you have to put something in (for example could this Value be 'Title') and ValueVar is the Value you put in the FieldNameVar field.

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