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()
isDictionary<>.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.item.FieldValues.ToString()
returns"System.Collections.Generic.Dictionary
2[System.Int32,System.String]"` value, not fields values.