From 24c19b5acbcd6a76169d74d2eae2b3fc754818b9 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Tue, 7 Jun 2016 13:34:30 -0600 Subject: [PATCH] Many changes - Accepts multiple fields as a dictionary object - Creates a new list item if there isn't an existing one - Updates an existing list item if the ID is passed in --- SPListLibrary.bas | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/SPListLibrary.bas b/SPListLibrary.bas index d3fdff4..4456ac6 100644 --- a/SPListLibrary.bas +++ b/SPListLibrary.bas @@ -1,39 +1,43 @@ Attribute VB_Name = "SPListLibrary" -Public Function Add_Item(ListName As String, SharepointUrl As String, ValueVar As String, FieldNameVar As String) As Integer +Public Function SPListItem(SharepointUrl As String, ListName As String, ListData As Dictionary, Optional itemid As Integer = 0) As Integer Dim objXMLHTTP As MSXML2.XMLHTTP Dim strListNameOrGuid As String Dim strBatchXml As String Dim strSoapBody As String Dim xmlhttpResponse As New MSXML2.DOMDocument Dim attr As Object - + Dim key As Variant + + strBatchXml = "" + For Each key In ListData + strBatchXml = strBatchXml & "" & ListData(key) & "" + Next key + + If itemid = 0 Then + strBatchXml = "" & strBatchXml & "" + Else + strBatchXml = "" & itemid & "" & strBatchXml & "" + End If + Set objXMLHTTP = New MSXML2.XMLHTTP - - strListNameOrGuid = ListName - - strBatchXml = "New" + ValueVar + "" - 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 = "" & strListNameOrGuid _ + & "xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" & ListName _ & "" & strBatchXml & "" - + objXMLHTTP.send strSoapBody - + If objXMLHTTP.Status = 200 Then Set xmlhttpResponse = objXMLHTTP.responseXML - Add_Item = CInt(xmlhttpResponse.SelectSingleNode("//UpdateListItemsResult//Results//Result//z:row").Attributes.getNamedItem("ows_ID").Text) + CreateSPItem = CInt(xmlhttpResponse.SelectSingleNode("//UpdateListItemsResult//Results//Result//z:row").Attributes.getNamedItem("ows_ID").Text) Else - MsgBox ("Some kind of error happened. Wut?") - Add_Item = -1 + CreateSPItem = -1 End If Set objXMLHTTP = Nothing -End Function - - +End Function \ No newline at end of file