Monday, February 18, 2013

SharePoint 2010 Records Management LockItem and UnlockItem

Well today i was doing some poc on Inplaee record management in SharePoint 2010 and faced some interesting points.


The reason i started doing poc is to lock a SharePoint list item and unlock a item.
becuase once it locked other user cannot edit the item. There are so many other methods also there to accomplish this. But i found two methods in SharePoint 2010 API which is there in below namespace

using
Microsoft.Office.RecordsManagement.RecordsRepository

Also, we need to add "Microsoft.Office.Policy" dll to get the above namespace.

The two methods are

Records.LockItemRecords.UnlockItem

Now there are other methods are there like Records.DeclareItemAsRecord and
Records
.UndeclareItemAsRecord to acheive the same. now is question like why i didnt use
the reason i didnt use is once you declareitemasrecord i missed the delete this list option in list settings. then i thought we can use the alternative.

Okay again in this method also i faced problem like it requires
_vti_ItemHoldRecordStatus column to the lock and unlock methods. (Note this is hidden column)

Then i started creating contenttype and given the below reference
<?xml version="1.0" encoding="utf-8"?><
Elements xmlns="http://schemas.microsoft.com/sharepoint/"><!--
Parent ContentType: Item (0x01) --><
ContentType ID="0x0100d6c72b455db04e61824b10c707ca4f9c"Name="LockItem - TestContentTypeSample"Group="Custom Content Types"Description="My Content Type"Inherits="TRUE"Version="0"><
FieldRefs><
FieldRef ID="{F9A44731-84EB-43a4-9973-CD2953AD8646}" Name="_vti_ItemDeclaredRecord" DisplayName="Declared Record" Required="FALSE"/><
FieldRef ID="{3AFCC5C7-C6EF-44f8-9479-3561D72F9E8E}" Name="_vti_ItemHoldRecordStatus" DisplayName="Hold and Record Status" Required="FALSE"/><
FieldRef ID="{740931E6-D79E-44a6-A752-A06EB23C11B0}" Name="_vti_ItemIsLocked" DisplayName="Is Locked For Edit" Required="FALSE"/></
FieldRefs></
ContentType></
Elements>

Next i created list definition with above content type refer below snippet

<List xmlns:ows="Microsoft SharePoint" Title="ListSample" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/LockItem-ListSample" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/"><
MetaData><
ContentTypes><
ContentType ID="0x0100d6c72b455db04e61824b10c707ca4f9c" Name="LockItem - TestContentTypeSample" Group="Custom Content Types" Description="My Content Type" Inherits="TRUE" Version="0"><
FieldRefs><
FieldRef ID="{F9A44731-84EB-43a4-9973-CD2953AD8646}" Name="_vti_ItemDeclaredRecord" DisplayName="Declared Record" Required="FALSE" /><
FieldRef ID="{3AFCC5C7-C6EF-44f8-9479-3561D72F9E8E}" Name="_vti_ItemHoldRecordStatus" DisplayName="Hold and Record Status" Required="FALSE" /><
FieldRef ID="{740931E6-D79E-44a6-A752-A06EB23C11B0}" Name="_vti_ItemIsLocked" DisplayName="Is Locked For Edit" Required="FALSE" /></
FieldRefs></
ContentType></
ContentTypes><
Fields><
Field ID="{F9A44731-84EB-43a4-9973-CD2953AD8646}" Name="_vti_ItemDeclaredRecord" StaticName="_vti_ItemDeclaredRecord" DisplayName="$Resources:dlccore,RecordResourcesItemDeclaredRecord_DisplayName;" SourceID="http://schemas.microsoft.com/sharepoint/v3" Group="$Resources:dlccore,RecordResourcesColumnGroup;" Type="DateTime" Indexed="FALSE" Hidden="FALSE" CanToggleHidden="TRUE" ShowInNewForm="FALSE" ShowInEditForm="FALSE" ShowInFileDlg="FALSE" ShowInDisplayForm="TRUE" Required="FALSE" Sealed="TRUE" ReadOnly="TRUE" /><
Field ID="{740931E6-D79E-44a6-A752-A06EB23C11B0}" Name="_vti_ItemIsLocked" StaticName="_vti_ItemIsLocked" DisplayName="$Resources:dlccore,RecordResourcesItemIsLocked_DisplayName;" SourceID="http://schemas.microsoft.com/sharepoint/v3" Group="$Resources:dlccore,RecordResourcesColumnGroup;" Type="Boolean" Indexed="FALSE" Hidden="TRUE" CanToggleHidden="TRUE" ShowInNewForm="FALSE" ShowInEditForm="FALSE" ShowInFileDlg="FALSE" ShowInDisplayForm="TRUE" Required="FALSE" Sealed="TRUE" ReadOnly="TRUE" /><
Field ID="{3AFCC5C7-C6EF-44f8-9479-3561D72F9E8E}" Name="_vti_ItemHoldRecordStatus" StaticName="_vti_ItemHoldRecordStatus" DisplayName="Hold and Record Status" SourceID="http://schemas.microsoft.com/sharepoint/v3" Group="Document and Record Management Columns" Type="Integer" Min="0" Max="32" Decimals="0" Indexed="FALSE" Hidden="TRUE" CanToggleHidden="FALSE" ShowInNewForm="FALSE" ShowInEditForm="FALSE" ShowInFileDlg="FALSE" ShowInDisplayForm="FALSE" Required="FALSE" Sealed="TRUE" ReadOnly="TRUE"/></
Fields><
Views><
View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE"><
Toolbar Type="Standard" />
Now i got the list instance. next make sure you have selected the below option in list settings->record declartaion settings->


Finally below is the code snippet to lock or unlock the item


  protected
void btnLock_Click(object sender, EventArgs e){

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists.TryGetList("ListSample");
SPListItem item = list.GetItemById(Convert.ToInt32(txtID.Text));
if (item != null){

Records.LockItem(item, "itemToLock");}
}



protected void btnUnLock_Click(object sender, EventArgs e){

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists.TryGetList("ListSample");
SPListItem item = list.GetItemById(Convert.ToInt32(txtID.Text));
if (item != null){

if (Records.IsLocked(item)){

Records.UnlockItem(item, "itemToLock");}
}
}

if you locked with "itemToLock" you must unlock with the same name otherwise you will receive an error saying object refernce not set to an instance of an object :)