Tag Archives: item.SystemUpdate

Event handler to archive items when deleted

The requirement – copy a list item that is deleted to an archive list. The solution – event receiver that copies the item. The code below does the job, but again is only a sample – you still need to implement error handling, and there are hard coded variables there you will want to change.

Note: this code is specifically for list items, not for documents in document libraries. You can easily change the code to support documents as well (see the CopyAttachments function for an example on how to copy files).

public override void ItemDeleting(SPItemEventProperties properties) {

//note: may require permission elevation. //TODO: add error handling //get the item being deleted

SPListItem item = properties.ListItem;

//get the target list

SPList targetList = properties.Web.Lists[“Announcements Archive”];

//create the new item

SPListItem newItem = targetList.Items.Add();

//copy the list item to the target

foreach (SPField f in item.Fields) {

if (!f.ReadOnlyField && newItem.Fields.ContainsField(f.InternalName)) newItem[newItem.Fields.GetFieldByInternalName(f.InternalName).Id] = item[f.Id]; }

//copy “special” read only fields that can be written to

newItem[“Created By”] = item[“Created By”];

newItem[“Modified By”] = item[“Modified By”];

newItem[“Modified”] = item[“Modified”];

newItem[“Created”] = item[“Created”];

newItem.SystemUpdate(false);

CopyAttachments(item, newItem);

base.ItemDeleting(properties); }

Note: for the “CopyAttachments” View this Page : How to copy attachments from one list item to another

Ref : Sharepoint Tips And Tricks

Advertisements

Delete All Old Versions from SharePoint 2010 Document Libraries

How many versions of a typical document do you need to keep?  5?  10?  100?
SharePoint content databases often get cluttered with redundant versions of the same documents.  In some cases, I’ve seen presentation libraries with less than 10 GB of active content, but hundreds of GBs of content due to gradual changes.
If you’re ready to clear out old versions, SharePoint 2010’s Management Shell makes it easy.  The script below will iterate through all lists and update them to keep only the last 2 copies.  It will also loop through and delete unneeded versions.

Get-SPWebApplication |  Get-SPSite -Limit All |  Get-SPWeb -Limit All |  ForEach-Object  { ForEach($list in $_.Lists) { If($list.EnableVersioning -eq $true) { $list.MajorVersionLimit = 2; $list.Update(); ForEach($item in$list.Items) { $item.URL; $item.SystemUpdate() } } } }

You can see that it…
  1. Loops through all web applications
  2. Loops through their site collections
  3. Loops through their webs
  4. Loops through their lists
  5. If versioning’s enabled, it sets the major version limit to 2
  6. In order to remove old versions, it needs to loop through each item and perform a system update

The script can be easily amended to deal with only specific site collections / libraries.  To keep a different number of versions, modify the MajorVersionLimit variable above.