1

I'm trying to update a SharePoint list via C# So first I check if my item already exists, if it is, I add his id in my lUpdate variable. So my code works, it goes in the loop (for the update and the insert), it takes each values well but for the update, it doesn't update my list (my values stay the same in the list) but the insert works

Here find my code:

public static void SetSharePointitem()
      {

         ClientContext clientContext = new ClientContext("SharePointSite");
         List oList = clientContext.Web.Lists.GetByTitle("MyList");
         if (lUpdate!=null)
         {
            ListItem oListItem = oList.GetItemById(lUpdate);
            foreach (var item in projectCenter)
            {

               oListItem[item.FieldNameTarget] = item.FieldValue;
               oListItem.Update();

            }
            clientContext.ExecuteQuery();
         }
         else
         {
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
             foreach (var item in projectCenter)

            {

               oListItem[item.FieldNameTarget] = item.FieldValue;
               oListItem.Update();

            }
            clientContext.ExecuteQuery();
         }

      }

Any idea of what I'm doing wrong? When I reach the clientContext.ExecuteQuery(); in my first loop, I get an error message which says:

item does not exist

Thanks for your help

1
  • Did you try the code snippet below ?
    – Jerry
    Jul 3, 2019 at 1:37

1 Answer 1

0

I see you are updating oListItem object and this oListItem is coming from GetItemById, so it is a single list item, then no need to loop to set value and use try catch to validate if a list item exists, here is the modified code which works in my side to insert/update list item based on if it existed:

public static void SetSharePointitem(int lUpdate)
{
    ClientContext clientContext = new ClientContext("http://sp/sites/dev/");
    List oList = clientContext.Web.Lists.GetByTitle("MyList");
    try
    {

        ListItem oListItem = oList.GetItemById(lUpdate);
        clientContext.ExecuteQuery();


            oListItem["Title"] = "Updated";
            oListItem.Update();
            clientContext.ExecuteQuery();

    }
    catch (Exception ex)
    {


        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem newListItem = oList.AddItem(itemCreateInfo);


        newListItem["Title"] = "New Created";
        newListItem.Update();


        clientContext.ExecuteQuery();
    }

}

So in the code snipppet above, if it throws exception, means listItem not existed,then it will go to catch exception and create item instead of updating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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