Wednesday, May 12, 2010

Adding list item to List using web service

Hi All,

In this post, I will show you how to add list item to the list using SharePoint web service.

I am writing a code in Windows Application and I have taken reference of Lists.asmx of respective site.

I have DummyList as list in my site and in that I have three columns. Name of the columns are title, group which is Choice column (GroupA,groupB,groupC) and Price which is number.

private void button6_Click(object sender, EventArgs e)
string strQuery = "<Method ID='1' Cmd='New'>" +
"<Field Name='Title'>" + "Title Test" + "</Field>" +
"<Field Name='Price'>" + 12000 + "</Field>" +
"<Field Name='Group'>" + "GroupB" + "</Field></Method>";

DownloadListItems.Lists objLists = new EncryptAndDecrypt.DownloadListItems.Lists();

objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;

objLists.Url = "{site url}/_vti_bin/lists.asmx";

String listName = "DummyList";

XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");

elBatch.InnerXml = strQuery;

XmlNode ndReturn = objLists.UpdateListItems(listName, elBatch);

string result = ndReturn.OuterXml.ToString();


Just make sure that you pass proper credentials to the web service. Rest the code itself is self explanatory.

Just observe the result string above, and see ErrorCode element in it, it should be 0x00000000. If it is, then it is a success, else there is some error and item has not been added.

That is it. You have just added list item using web service.

------Modified on 21-05-2010-----------

Let us explore bit more on the same post. We will now add lookup field through web service. Yes, this is bit different than adding the simple field using web service.

We will take the same example as we show above.

I have made a little change in the list settings. I removed the Group column and recreated the same with lookup column to another list with same values. I have another list which has GroupA, GroupB and GroupC as title values in it. And then in DummyList I’ve created that lookup field.

So now let us move on. There are two ways you can insert lookup field through web service. Either you can mention in Field tag that this is the lookup field or you can directly reference that look up value by mentioning; # sign in between.

Below are the examples that describe both the ways.

1) "<Field Name='Group' Type='Lookup'>" + 2 + "</Field></Method>";

2) "<Field Name='Group'>" + "2;#GroupB" + "</Field></Method>";

Where first is mentioning field is lookup and second mentioning ID + ItemName in it. ID is the ID for the item in master list from which we have taken the reference. In my case GroupB’s ID in master list is two.

Modify the above code and then let’s run this sample now. See the result. Make sure that if anything is wrong, observe the result and in that check the ErrorCode element, you will see some error code in that. If there is not error, then as I mentioned earlier it will return you 0x00000000.

One interesting fact is, I don’t know how accurate this is but even in second way, you write like “2;#GoupC”, then also it will insert GroupB as it takes ID as the main value. Does it mean that we can specify any value after ;# sign as far as that value is present in the master list and that will not impact on what you want to insert??

Humm..interesting. I recommend make sure to query your master list with lookup value and get the ID and then combince that ID + “;#” + value and thenuse it in this method to populate your list lookup column.

I'll keep you all posted on what's more with List Item using SharePoint web service.


Shahid Riaz Bhatti said...

This is really nice Article. I am a ASP.Net web developer and started to learn MOSS development on my own. I have achieved the same thing using MOSS object model and I was looking for that how to do this using MOSS web services. This article helped me alot and I will write the similar article which will do the same thing in aspx page. Thanks dude for sharing this. I will post my link for you to review.

Shawn said...

This looks great but I am in need of understanding the 'passing proper credentials' piece. I am wanting to submit a list item from a web from a wordpress site hosted at a different service provider. Is this doable? How would I pass credentials? I'm assuming it can't be done anonymously...

SharePoint Kings said...


passing credential is from this line

objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;

above line is using login user credential but you can use proper credential means admin credential

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to