Monday, March 14, 2011

Delete All Items, including Folders, from SharePoint List Programmatically and Efficiently

From this post http://www.praveenmodi.com/programmatically-delete-all-items-sharepoint-list/ by Praveen Modi.

///
/// Purges items and folders from a list
/// Define WSSV3 to remove list folders
///

/// The SPList you want to
/// purge items from
private static void PurgeList(SPList list)
{
Console.WriteLine("Purging list: " + list.Title);
Console.WriteLine("Base Type: " + list.BaseType.ToString());

// ===========================================================
// list.ItemCount returns a count that includes all items
// "AND" folders.
// You can't use list.Items.DeleteItemById() to remove a
// folder
// ===========================================================
System.Collections.Hashtable hItems =
new System.Collections.Hashtable(list.ItemCount);

// ===========================================================
// SPList.Items returns all list items in the entire list
// regardless of folder containment
// Note, just because list.ItemCount includes folders,
// list.Items does not.
// ===========================================================
foreach (SPListItem item in list.Items)
hItems.Add(item.ID,null);

// Remove the list items
foreach (int ID in hItems.Keys)
list.Items.DeleteItemById(ID);
// Clear the hashtable
hItems.Clear();
// ===========================================================
// SPList.Folders returns all folder items in the entire list
// regardless of parent folder containment
// ===========================================================
foreach (SPListItem item in list.Folders)
hItems.Add(item.ID,null);

// Remove the folder items
foreach (int ID in hItems.Keys)
{
list.Folders.DeleteItemById(ID);
}
}

Friday, March 11, 2011

Expiration Policy Timer job does not seem to run

Copied from this post: http://ithinksharepoint.blogspot.com/2009/03/expiration-policy-timer-job-does-not.html

beginning-of-copy

The policy is run once a day, however fortunately you can start it manually. Using Central Admin->Operations->Information Management Policy Configuration, I clicked on the Expiration policy link. Then within there clicked "Process Expired Items Now". Looked at the Timer Job Status (Central Admin->Operations->Timer Job Status) and there was no reference of the Expiration Policy.

Looked within Timer Job Definitions (just under the Timer Job Status link) and a timer job had been created called "Expiration Policy (manually initiated)" but it had never been run. Actually neither had the daily "Expiration Policy". I went back to the Information Management Policy Configuration and into the Expiration policy. The "Process Expired Items Now" button was greyed out, to clear this I ended up deleting the "Expiration Policy (manual initiated)" timer job. This enabled the "Process Expired Items Now" button... however nothing.

Other things that I tried included stsadm -o execadmsvcjobs - though nanda.

Then I thought about the fact that the job had never been run and I found this article
http://www.sharepointblogs.com/teameli/archive/2008/10/13/record-center-information-management-policy-jobs-not-running.aspx

Following the instructions sorted the problem...

end-of-copy