Insert a Sharepoint list item from web services
I recently wanted to use Sharepoint’s web services to manipulate a list on one of our WSS sites. I wanted to submit a list item from a remote location, so using the Lists web service (http://<server-url>/_vti_bin/lists.asmx) seemed to make sense.
There are many examples of inserting a list item in Sharepoint using the web services out there on google, and they are helpful. They tell you how the Lists api works, because its not really intuitive, as the argument to the web service is an XmlNode object. Basically, what you submit to the web service is a xml document that contains the values for the list fields that you want to add, something like this (lifted and modified from here):
string sBatch = string.Empty;
sBatch = “<Method ID=\”1\” Cmd=\”New\”>”;
sBatch += “<Field Name=\”ID\”>New</Field>”;
sBatch += “<Field Name=\”Title\”>My Title</Field>”;
sBatch += “<Field Name=\”EventDate\”>” + DateTime.Now + “</Field>”;
sBatch += “</Method>”;
This is one way to construct the xml document that you need to submit. This xml will add an item with a title of “My Title” and an Event Date of the current date/time. This code works, theres nothing wrong with it. Its just that this is the extent of the examples out there, I couldn’t find anything that was more complex, for instance, an example of inserting an item into a Sharepoint list where one of the fields in the list is a lookup to another Sharepoint list.
Consider a list where there is a Status column, which is a lookup to another list that has possible values of “Submitted”, “Approved”, “Rejected”, and “Completed”. How do you specify the value for this field? I initially tried simply inserting the text value of what I wanted, so my xml would look like this:
<Method ID=”1″ Cmd=”New”>
<Field Name=”ID”>New</Field>
<Field Name=”Title”>My Title</Field>
<Field Name=”EventDate”>3/25/2006 12:00 PM</Field>
<Field Name=”Status”>Submitted</Field>
</Method>
But this doesn’t work. After some more trial and error, I tried the index of the value that I wanted, in this case 0, because “Submitted” is the first item in the list, and presto! My item was inserted, all was good. The downside that I see to this, is that if the indices of the items change, you have to change your xml. You could get around this by querying the lookup list and finding the index of the item dynamically each time, its your call. So if you want to insert an item into Sharepoint with a field that is a lookup field, here is an example of what your xml should look like:
<Method ID=”1″ Cmd=”New”>
<Field Name=”ID”>New</Field>
<Field Name=”Title”>My Title</Field>
<Field Name=”EventDate”>3/25/2006 12:00 PM</Field>
<Field Name=”Status” Type=”Lookup”>0</Field>
</Method>
I hope this helps someone else who is scouring google for help
UPDATE: I forgot an important piece to the resulting xml. You need to include the Type attribute on the Field element, I have added it to the xml above in red, my apologies.
Pratush B said,
Wrote on October 30, 2006 @ 6:15 pm
umm..there is another way though..to make it a bit more generic for code, we can pass id#;value and avoid using type=”Lookup”.
New
My Title
3/25/2006 12:00 PM
0;#Submitted
but its better using the type=”LookUp” as it avoids incorrect values
anuj handa said,
Wrote on July 19, 2007 @ 2:03 am
I am trying to update a lookup column in the sharepoint list using the Web service.
var xmldoc1;
var batch;//The batch variable builds up the XML that we are going to submit to the SharePoint list via the SharePoint web service
batch = “” issueID “”;
xmldoc1=”
xmldoc1=xmldoc1 ” ListName ”;
xmldoc1=xmldoc1 ” batch ”;
FeedbackHTTP1=new ActiveXObject(”MSXML2.XMLHTTP.3.0″);
FeedbackHTTP1.Open(”POST”, WSSSiteURL “/_vti_bin/Lists.asmx?op=UpdateListItems”, true);
FeedbackHTTP1.setRequestHeader(”Content-Type”,”text/xml; charset=utf-8″);
FeedbackHTTP1.setRequestHeader(”SOAPAction”,”http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
FeedbackHTTP1.onreadystatechange=LookUpReady;
FeedbackHTTP1.Send(xmldoc1);
return false;
}
function LookUpReady()
{
if (FeedbackHTTP1.readyState==4){
var xmlResponse=new ActiveXObject(”MSXML2.DOMDocument.3.0″);
xmlResponse.async=false;
xmlResponse.loadXML(FeedbackHTTP1.responseXML.xml);
}
}
But this is not throwing error and is neither updating the column ……Any Help?
SharePoint Experience: Forms Server and the anonymous users said,
Wrote on September 29, 2007 @ 11:14 am
[…] (Forms Server)http://weblog.vb-tech.com/nick/archive/2006/02/24/1453.aspx (WSS web services)http://benreichelt.net/blog/2006/3/25/Insert-a-Sharepoint-list-item-from-web-services/ (Insert an […]
Jim Raley said,
Wrote on January 10, 2008 @ 1:36 am
I’ve successfully done this, but I recently discovered that HTML-enabled multi-line fields have a problem here: submitting such a field via JS seems to strip out the line breaks and formatting.
If I put in a couple paragraphs, even if only using the default font settings, it all gets crushed together. I have not, however, tried to hard code a sample string of HTML into the soap to see if it survives.
Thoughts or workarounds? Can we set the content type (in the request header) to something other than “text/xml”? I think that’s the problem, but I lack a solution.
Ben Runchey said,
Wrote on January 11, 2008 @ 9:30 am
Ben,
Thanks for this post. I have having to utilize many of the services on the List web service and am enjoying the syntax of CAML (not really!). Your post has made things a bit easier.
-Ben
Ben C said,
Wrote on March 26, 2008 @ 10:10 am
I have been struggling to update multiselect list columns in SharePoint. Has anyone found a away to do this.
Also I tried inserting the ID as in the above example and it works for dropdown list, however I am trying to submit a value the PRIORITY field in an Issue List, which can be (1) High, (2) Normal, or (3) Low.
When I submit a 1 it will correctly set the dropdown in Edit Item, but in View Item it will only show a 1, which may be confusing to a read only user.