Create OR Update List item using CSOM C#

Updated: Sep 15, 2020

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"]);
} 
1 view0 comments

Recent Posts

See All

In this blog we will see how to export a list in excel using server side object model. First block we will retrieve all items from the list. This can be done in several ways. You can get all items wit