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();
        }
4
  • 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
    Commented Jun 8, 2018 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
    Commented Jun 8, 2018 at 11:31
  • Because item.FieldValues.ToString() returns "System.Collections.Generic.Dictionary2[System.Int32,System.String]"` value, not fields values.
    – AlexZh
    Commented Jun 8, 2018 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
    Commented Jun 8, 2018 at 11:50

1 Answer 1

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;
         }
     }
}
1
  • 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
    Commented Jun 8, 2018 at 14:34

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.