问题
I have been attempting to add data to a Sharepoint 2013 list from Excel using VBA with no success. I am using as a template the example that is found all over the internet.
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
Since I am new to using XML and SOAP I decided to work backwards and just try to add an item to the list first. I don't have an issue with getting the data from the sheet. I don't pass any parameters into the sub, I just assign values.
I don't get any errors. But no data gets written to the list, either. I even get a status of 200 - which simply means "OK". I even tried taking some characters out of the list name or the url to see if that would give an error, but I still get a status of 200.
If I use Msxml2.ServerXMLHTTP instead of Msxml2.XMLHTTP I get a status of 401. Is there any way I can force an error so I can see what I need to correct to get this to work?
Here is what I am working with (hopefully I removed any sensitive information):
Sub Add_Item()
' Hard coding the parameters to start so just testing the output to SharePoint
Dim ListName As String
Dim SharepointUrl As String
Dim Branch_Cost_Center As String
Dim Field1NameVar
ListName = "Nightly Device Tracking Excel XML Test"
SharepointUrl = "https://{myurl}"
Field1NameVar = "Branch Cost Center"
Branch_Cost_Center = "919191"
' Remove the above when passing parameters
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=" + Field1NameVar + ">" + Branch_Cost_Center + "</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>"
'for debugging
Debug.Print strSoapBody
objXMLHTTP.send strSoapBody
If objXMLHTTP.Status = 200 Then
' Do something with response
End If
Set objXMLHTTP = Nothing
End Sub
I have tried using the internal name for the field and the GUID instead of the list name with no difference in result.
I set a watch variable on the objXMLHTTP, but don't know what to look for - there is a lot of information returned.
回答1:
Your field names need to have single quotes around them:
Field1NameVar = "'Branch Cost Center'"
来源:https://stackoverflow.com/questions/42638740/add-data-to-sharepoint-2013-list-from-excel-with-vba