Feature stapling always returns item does not exist: it may have been deleted by another user
-
28-09-2020 - |
Question
I'm attempting to create a feature stapled solution that pushes out custom branding to every new site collection created. The following code is in my event receiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using(SPSite sitecollection = properties.Feature.Parent as SPSite)
{
using (SPWeb site = sitecollection.OpenWeb())
{
foreach (SPWeb web in sitecollection.AllWebs)
{
try
{
SPList list = web.Lists.TryGetList("Composed Looks");
foreach (SPListItem item in list.Items)
{
if(item.Title != "Current" || item.Title != "Dynetics")
{
item.Delete();
item.Update();
}
}
}
finally
{
if(web != null)
{
web.Dispose();
}
}
}
}
}
}
But when deploying the solution I get the error:
Error occurred in deployment step 'Activate Features': Item does not exist. It may have been deleted by another user.
WHY??
Solution
By looking into your code, i think the problem is
item.Delete();
item.Update();
You are updating item after you are deleting, there is no need to update the item after delete.
You can not update a deleted item.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange