Question

I need to add items to a SharePoint list by using client object model and Lists.asmx web service.
How can this be achieved?

Also, I am not able to understand why do we use Lists.asmx web service when we can directly add items to SharePoint list using only client object model.

Can someone kindly explain my doubts with an example?

Was it helpful?

Solution

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = "My New Item!";
            oListItem["Body"] = "Hello World!";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}

There are certain scenario on when to use CSOM vs Services.

https://sharepoint.stackexchange.com/questions/26093/webservices-or-client-object-model

If you can you should use the Client Object Model (CSOM) - it does not support that many features as the web services but are superior in a number of ways such as:

data types batching of commands (more efficient usage of bandwith) optimization of data loaded (more efficient usage of bandwidth) more similar to the server side object model in terms of programming (the web services are in most cases just huge chunks of undocumented XML you need to parse) ... So, for performance you will likely suceed better using CSOM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top