Software Technology Tips

Sharepoint 2010 has so many new exciting features and one of them is InPlace Record Management. By using this feature one can Declare Item as Record or Lock the record so that it will obey  a set of policy based on Policy declaration. If the policy declaration is that it should not allow anybody to edit or delete the document if its a record or locked then it will not allow any user to edit the document or delete it.
 
Here I am going to share the code snippet which will allow the user to edit the item which is already declared as Record or Locked programatically.

Steps to follow
-------------------
1. Undeclare the Record or Unlock the record programatically
2. Edit the item as per the requirement programatically
3. Relock or Declare the item as Record programatically
Issue
--------------------
After undeclaring the item as record when the user tries to modify or edit the item  SharePoint, it will throw the exception as : 
"The file documentname.docx has been modified by SHAREPOINT\system on xxx-date" or "The file has been locked by the current user and can't be edited"
 
Reason
--------------------
When the user undeclares the item as record then the SharePoint process first keeps that document on hold to relase the lock. Usually this takes some time to realse the hold and undeclare the item as a record. During the same when the user again tries to modify or edit the item then the Sharepoint will not allow this operation as one operation is already in progress with the same document or item.
 
 

Solution
----------------------
1. After undeclaring the item or document as Record we have to dispose the spweb object so that all the Operation are complete
2. Then we have to create one more web object to the second and third operation which is Editing the item or document and relocking the item again.

   /// <summary>
  /// This method is used to undeclare one item which is declared as record 
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>

     private void UnDeclareItemAsRecord(object sender, EventArgs e)
      {
        SPList list = null;
        SPListItem item = null;
        SPSite site = null;
        SPWeb spWeb = null;
        SPSecurity.RunWithElevatedPrivileges(delegate()
         {
          using (site = new SPSite("
http://Mindfire.com"))
           {
            using (spWeb = site.OpenWeb())
              {
               list = spWeb.Lists["Mindfire"];
                if (list != null)
                   {
                     item = list.Items[0];
                   //Checking whether the item is a record or not
                            if (item != null && Records.IsRecord(item))
                               Records.UndeclareItemAsRecord(item);                      //undeclares the item as a record
                     }
                  }
               }
          });
        }

    
        /// <summary>
        /// This method is used to update the field and declare the item as Record
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
     private void UpdateItemAndDeclareAsRecord(object sender, EventArgs e)
        {
            SPList list = null;
            SPListItem item = null;
            SPSite site = null;
            SPWeb spWeb = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (site = new SPSite("
http://Mindfire.com"))
              {
                  using (spWeb = site.OpenWeb())
                  {
                     list = spWeb.Lists["Mindfire"];
                      if (list != null)
                       {
                        item = list.Items[0];
                        //Checking whether the item is a record or not
                         if (item != null)
                           {
                            if(!spWeb.AllowUnsafeUpdates)
                            spWeb.AllowUnsafeUpdates = true;
                            item["ows_ApprovalStatus"] = "Archived";
                            item.Update();
                            if (spWeb.AllowUnsafeUpdates)
                            spWeb.AllowUnsafeUpdates = false;
                         }
                        if (!Records.IsRecord(item))
                        Records.DeclareItemAsRecord(item);   //declares the item as a record
                       }
                    }
                }
            });
        }
 
Note: To use the Record management programatically, please add Microsoft.Office.Policy dll reference to your project reference and use Microsoft.Office.RecordsManagement.RecordsRepository;  in using block.
 


Related Tags:

SharePoint, SharePoint 2010, Edit Locked Document, Record Management

Author: Srikanta Barik

SharePoint

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

copyright (c) Mindfire Solutions 2007-2014. Login