0

i have sharepoint list , in which i get all data , now i want to update an item in sharepoint list , so i want to get by id and than update that list here is my code. here i get nothing in olistitem

using (ClientContext context = new ClientContext("http://URL/"))
{
    context.AuthenticationMode = ClientAuthenticationMode.Default;
    context.Credentials = new NetworkCredential("USername", "********");
    Web webObj = context.Web;
    List listObj = webObj.Lists.GetByTitle("Leave Requests");
    CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(10);
    ListItemCollection listitemcoll = listObj.GetItems(camlquery);
    context.Load(listitemcoll);
    context.ExecuteQuery();

    ListItem oListItem = listObj.GetItemById(186);
}
0

You can use the below code to update a specific list item by id. You really dont have to get all list items(unless you want to do something like display them later) before updating a list item.

        string siteUrl = "http://servername:2525/";  
        ClientContext clientContext = new ClientContext(siteUrl);  
        List oList = clientContext.Web.Lists.GetByTitle("TestList");  
        ListItem oListItem = oList.GetItemById(5);  
        oListItem["Title"] = "Hello World Updated!!!";  
        oListItem.Update();  
        clientContext.ExecuteQuery()
0

In Client object model you need to create your query, load object and execute query for getting result.

In above code, you just query to SharePoint but not execute that query.

Please use below code:

 using (ClientContext context = new ClientContext("http://URL/"))
                                {
                                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                                    context.Credentials = new NetworkCredential("USername", "********");
                                    Web webObj = context.Web;
                                    List listObj = webObj.Lists.GetByTitle("Leave Requests");
                                    CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(10);
                                    ListItemCollection listitemcoll = listObj.GetItems(camlquery);
                                    context.Load(listitemcoll);
                                    context.ExecuteQuery();

                                    ListItem oListItem = listObj.GetItemById(186);
context.Load(oListItem);
                                    context.ExecuteQuery();


                                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.