Arozora
Arozora

Reputation: 5

SharePoint Update list items

I'm trying to update a SharePoint list, I found a code sample on Internet (officail Microsoft documentation) So this is the code:

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

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

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.Items.GetById(3);

            oListItem["Title"] = "My Updated Title.";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}

If I copy/past this code in visual studio, I have an error to this line:

ListItem oListItem = oList.Items.GetById(3);

List doesn't contain a definition for Items and no accessible extensions method 'accepting a first argumentof type 'List' could be find

Any idea of what I have to do to use this code?

Thank

Upvotes: 0

Views: 836

Answers (1)

Chandan Rauniyar
Chandan Rauniyar

Reputation: 824

The code you've provided to update a list item applies to SharePoint 2010. For newer version try

ListItem oListItem = oList.GetItemById(3);

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// Assume that the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// Assume there is a list item with ID=1. 
ListItem listItem = announcementsList.GetItemById(1); 

// Write a new value to the Body field of the Announcement item.
listItem["Body"] = "This is my new value!!"; 
listItem.Update(); 

context.ExecuteQuery();

Upvotes: 2

Related Questions