.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


Insert a Sharepoint list item from web services

Wanted to use Sharepoint's web services to manipulate a list on one of our WSS sites. 
Then 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.

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>

thanks Ben Reichelt’s Weblog


Categories: MOSS | VS 2005 | WSS
Posted by admin on Thursday, February 05, 2009 11:58 AM
Permalink | Comments (0) | Post RSSRSS comment feed