SharePoint List Web-Services, list item insertion

June 7, 2011

A simple task as adding an item to a SharePoint list has a lot of good-to-know stuff attached to it, the first problem-solving track I toke was using the Windows SharePoint Services classes to construct objects like the SPSite, and SPWeb and for the good reasons this wouldn’t work as I’m doing some data integration processing on a remote integration service (pulling data from different sources, do some processing, and then inserting them into a SharePoint list), as the other objects will allow you to construct them if and only if the script will run on the SP host machine so there’s no other way except using the SharePoint Web-Services to access site-collection lists remotely. after digging here and there for the best way to do it, I finally created the following chuck of code on my pilot project – the code has some tips that would save you a few hours of debugging..

 

   1: WS_TopDealLists.Lists lists = new WS_TopDealLists.Lists();

   2:  

   3: // Impersonating the context to use a user with a contribute access on the list

   4: lists.Credentials = new NetworkCredential("username","PASS****","domain");

   5:  

   6: // This line must be included – the webservice reference already has it I know,

   7: // otherwise the GetList method will throw Exception 

   8: // of type 'Microsoft.SharePoint.SoapServer.SoapServerException'.

   9: lists.Url = "http://SPServer/sites/SiteCollection/sandbox/TopDeals/_vti_bin/Lists.asmx";

  10:  

  11: // Use these two lines to learn about the actual static names of the list fields

  12: //XmlNode xmlNode = lists.GetList("LIST_NAME");

  13: //System.IO.File.WriteAllText("Top10.xml", xmlNode.OuterXml);

  14:  

  15: string strBatch = "<Method ID='1' Cmd='New'>" +

  16:     "<Field Name='Title'>WaleedsTitle</Field>" +

  17:     "<Field Name='Deal_x0020_Size'>1,000,000</Field>" +

  18:     "<Field Name='Step'>Technical Win</Field>" +

  19:     "<Field Name='Rep'>TJ</Field>" +

  20:     "<Field Name='Partner'>WaleedTMC</Field>" +

  21:     "<Field Name='Special_x0020_Needs'>support from the presales</Field>" +

  22:     "<Field Name='Product_x0020_Status'>Running in Staging</Field></Method>";

  23:     

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

  25: XmlElement elBatch = xmlDoc.CreateElement("Batch");

  26:  

  27: elBatch.InnerXml = strBatch;

  28:  

  29: lists.UpdateListItems("LIST_NAME", elBatch);

Some helpful resources:

http://stackoverflow.com/questions/85392/sharepoint-how-do-insert-new-items-using-the-list-web-service

http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx

http://msdn.microsoft.com/en-us/library/lists.lists.getlist(v=office.12).aspx

http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/777310c3-009f-4efd-8006-783480dd1ee8

Advertisement

2 Responses to “SharePoint List Web-Services, list item insertion”

  1. Haytham Says:

    Alsalam alikom wa ra7mat Allah wa barakatoh

    Nice post.. one thing you might want to try is CSOM (Client Side Object Model) it does allow you to call sharepoint from any machine (doesn’t have the be the server box).. you can do pretty much everything you want… (almost everything)…

    http://msdn.microsoft.com/en-us/library/ee857094(office.14).aspx

    Thanks

  2. Waleed Abdelwahab Ali Says:

    Sounds interesting, thanks man


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 130 other followers