0

I am deleting duplicate items from a list. I have written the below code but somehow "if" condition is always true even field values are different hence deleting all the rows of the list except first row.

        ListItem duplicate = null;
        ClientContext context = new ClientContext("SITE URL");
        context.Credentials = new SharePointOnlineCredentials(struser, password);

        List announcementsList = context.Web.Lists.GetByTitle("LIST Name");

        var query = new CamlQuery();
        query.ViewXml =
           @"<View>
          <Query>
             <GroupBy>
               <FieldRef Name='Role' />
             </GroupBy>
         </Query>
        </View>";

        ListItemCollection oitems = announcementsList.GetItems(query);

        context.Load(oitems, items => items.Include(
             item => item["Contact"],
             item => item["Role"],
             item => item["Priority"]));

        context.ExecuteQuery();

            foreach(ListItem item in oitems.ToList())
            {
            if (duplicate != null)
            {                 
                if (item.FieldValues.ToString().Equals(duplicate.FieldValues.ToString()))
                {
                    item.DeleteObject();
                }
            }
                duplicate = item;
            }                
            context.ExecuteQuery();
        }
  • item.FieldValues.ToString() is Dictionary<>.ToString(). Default implementations of the Object.ToString method return the fully qualified name of the object's type. So you compare object types which are the same. – AlexZh Jun 8 '18 at 10:57
  • But I can see that correct values are loading for item and duplicate object. And even if they are not equal, "if" condition becomes true. Can you please tell me why this is happening and what should I do to get it correct? – Ravi Jun 8 '18 at 11:31
  • Because item.FieldValues.ToString() returns "System.Collections.Generic.Dictionary2[System.Int32,System.String]"` value, not fields values. – AlexZh Jun 8 '18 at 11:38
  • Okay understood. So can you please suggest what should I use because when I am using item.FieldValues, it is not going inside the if condition at all. – Ravi Jun 8 '18 at 11:50
1

If your fields "Contact","Role","Prority" are text types then:

instead:

foreach(ListItem item in oitems.ToList())
{
    if (duplicate != null)
    {                 
         if (item.FieldValues.ToString().Equals(duplicate.FieldValues.ToString()))
         {
             item.DeleteObject();
         }
     }
     duplicate = item;
}

use:

foreach(ListItem item in oitems.ToList())
{
    if (duplicate != null)
    {                 
         if (item.FieldValues.Where(entry => 
                                    !Convert.ToString(item.FieldValues[entry.Key]).Equals(Convert.ToString(duplicate.FieldValues[entry.Key]))
                                    ).Count() > 0)
         {
             item.DeleteObject();
         }
         else
         {
             duplicate = item;
         }
     }
}
  • So after appending () after count it seems to be working.I have used .Count()>0 instead of .count>0 . Thanks a lot for your help :) – Ravi Jun 8 '18 at 14:34

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.