This is the third and last part of the series: Using MS SharePoint as Data Source. In the earlier posts we learned the basics of SharePoint and the easy way to retrieve data to SharePoint list. In this post we are going to discuss the easiest way of posting data to the SharePoint list.
Posting data to SharePoint can be done in similar way but instead of Query we will use batch element in our CAML.
Batch element provides batch processing of commands within HTTP protocol
<Batch OnError = "Return" | "Continue" ListVersion = "" Version = "" ViewName = ""> <Method> ... </Method> ... </Batch>
To determine the record that needs to be updated, we should use the ID column at SharePoint. ID is a default column at SharePoint list which is auto generated for each record. If you don’t see the field you can enable this at list settings.
Batch element should look like below in your request for posting data. Note that the first field name is the ID which determines which records need to be updated. Consecutive fields are the fields for which value should be updated as passed.
<Batch OnError='Continue' ListVersion='1'> <Method ID='1' Cmd='Update'> <Field Name='ID'>1</Field> <Field Name='Grade'>MD </Field> <Field Name='Salary'>80000 </Field> </Method>" "</Batch>"
Rest should be straightforward as explained in the last post. Here is a generic function for your reference.
Function PostData(url,splist,spID, spUpdateFeild, spUpdateValue) PostData = false batch = "<Batch OnError='Continue' ListVersion='1'>" batch = batch + " <Method ID='1' Cmd='Update'>" batch = batch + " <Field Name='ID'>" & spID & "</Field>" batch = batch + " <Field Name='" & spUpdateFeild & "'>" & spUpdateValue & " </Field>" batch = batch + " </Method>" batch = batch + "</Batch>" request = "<?xml version='1.0' encoding='utf-8'?>" & _ "<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>" & splist & "</listName>" + _ " <updates>" & batch & "</updates>" & _ " </UpdateListItems>" & _ " </soap:Body>" & _ "</soap:Envelope>" Set oXML = CreateObject("Microsoft.XMLHTTP") with oXML .open "Get", url, False, null, null .setRequestHeader "Content-Type", "text/xml; charset=utf-8" .setRequestHeader "SOAPAction","http://schemas.microsoft.com/sharepoint/soap/UpdateListItems" .send request strResponse = .responseText end with if instr(strResponse,"<ErrorCode>0x00000000</ErrorCode>") then PostDataToSP = True End Function
The generic functions provided in this series can be directly used in UFT and TestComplete. Tested on UFT 12.52 and TestComplete 11 with SharePoint 2010 and SharePoint 2013. Hope you enjoyed the series, let us know your feedback/Comments or questions if any.