GSCustomMenuSection

Contents for SharePoint, ReactJS, C#, ASP .Net, JavaScript, JQuery, SQL

Friday, December 18, 2015

Create OR Update List item using CSOM C#

In this blog we will see how to create item/Update a list item using CSOM

Add two references to your visual studio solution
Microsoft.SharePoint.Client.dll &
Microsoft.SharePoint.Client.Runtime.dll


string webUri = "http://servername/sites/demo";
using (ClientContext context = new ClientContext(webUri))
{
    //By Default it will use windows credentials
    //If want to specify credentials use below line
    context.Credentials = new NetworkCredential(
                             "username",
                             "password",
                             "domain");

    Web web = context.Web;

    //Load the web object 
    context.Load(web); 
    List list = web.Lists.GetByTitle("MyList"); 

    //---------------Create a new Item-----------------//
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem newListItem = targetList.AddItem(itemCreateInfo);
    newListItem["Title"] = "My Item 1";
    newListItem["Comments"] = "Test item";
    newListItem.Update();

    context.Load(newListItem);


    //-----------------Set/ Update an item-----------//
    ListItem listItem = announcementsList.Items.GetById(1); 

    //Set new value to the Comments field.
    listItem["Comments"] = "Updated test value!!"; 
    listItem.Update(); 

    //---------------------------------------------------------//  
    //call Execute Query 
    //to get all the loaded object working
    context.ExecuteQuery();

    Console.WriteLine("Item created!  ID: " + newListItem.Id + "\nTitle: " + newListItem["Title"]);
}

No comments :

Post a Comment