Wednesday, December 3, 2008

How to add a new list item to SharePoint using the List Item web service

How to add a new list item to SharePoint using the List Item web service ?

Problem Statement:
I want to add data into the "SalesReport" List on a sharepoint Site.

Solution:



Right click on the references folder and then ‘Add Web Reference…”.
WebService Reference: http:///_vti_bin/Lists.asmx

Type in the url to sites webservice you wish to call in the next box,
give it an appropriate name (we have given it as SharePointkingsWebService ) and click ok.

Create a method and place the below code.

try
{

SharePointkingsWebService.Lists list = new SharePointkingsWebService.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
list.Credentials = new System.Net.NetworkCredential("SharePointkings", "Password", "SharePointkings");
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string item = "" + "New" + "This is a test by Sharepoint Team" + "";
batch_element.InnerXml = item;
list.UpdateListItems("SalesReport", batch_element);

return true;
}
catch (Exception ex)
{
return false;
}


....And the job is done!!!

1 comment:

Candice said...

How would you do this with a "Links" list?




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 sharepointkings@gmail.com 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 sharepointkings@gmail.com