GeorgiM

Hi here,

if problems while deleting all items of some list i get strange errors. The exeption message is an out of bounds exception and in the sharepoint logs accur the message that an site


Code:

Code Snippet

SP list=...
if (listReference != null)
{
if (listReference.Items !=null )
{
int itemCount = listReference.Items.Count;
SPListItemCollection listItems = listReference.Items;
for (int k = 0; k < itemCount; k++)
{
SPListItem item = listItems[k];
item.Delete();
}
}

}


I excecute the code above for some lists aone after the other,but some times it works well and sometimes I'll get errors.

Exception: "...value out of valid rage" - out of bounds, but why

MOSS-Log: "It exists nowebsite with the name "/Lists/testliste/AllItems.aspx".

It is really strange and i think it maybe have to do something with some indexing jobs .

best regards

George


Re: SharePoint - Development and Programming Error while deleting all items of a list

sondergaard

Hi GeorgiM,

You should do an item.Update() after the delete

Does that work

-sondergaard






Re: SharePoint - Development and Programming Error while deleting all items of a list

GeorgiM

Hi sondergaard,

yes ok there must be an update-Method, but that wasn't it. The error still occur.

Code Snippet

SPList listReference = ...

if (listReference != null)
{
if (listReference.Items !=null )
{
int itemCount = listReference.Items.Count;
for (int k = 0; k < itemCount; k++)
{
listReference.Items.Delete(k);
}
}
listReference.Update();
}



- George -




Re: SharePoint - Development and Programming Error while deleting all items of a list

GeorgiM

OK breath deeply,

the solution is following:

Code Snippet

SPlist =...

if (listReference != null)
{
listReference.ParentWeb.AllowUnsafeUpdates=true;
if (listReference.Items != null)
{
for (int c = listReference.Items.Count - 1; c >= 0; c--)
listReference.ItemsCoffee.Delete();
}
listReference.ParentWeb.AllowUnsafeUpdates=false;
}


Yes and it was a really simple nullpointer exception and no update() is needed. Think the update is proceeed internal of the Delete() method.


- George -