Thursday, June 13, 2013

Document ID Feature

Copyright from:  http://www.codeproject.com

Introduction

In this article we can explore the Document ID feature and how to programmatically retrieve documents based on the IDs.

Scenario

Your customers are using Document Libraries for creating, updating and sharing documents. A problem could arise when the same document could exist in multiple libraries with the same name. There should be a way to identify the document using some Unique ID so that the document could be managed effectively independent of the location.
Document copy 1
 
Document copy 2

Solution

SharePoint already contains the Document ID service feature which could be used to address this problem. Document ID Service is a Content Management feature and explicit activation is needed in the site collection level. Once activated all the documents in the site collection will be assigned a unique ID.
Note: The Document ID is different from Record ID.  Additionally List items cannot be assigned Document ID.

How to enable Document ID Service?

You have to go to the Site Collection Top Level Site and choose Site Actions > Site Settings > Site collection features.
In the appearing page Activate the Document ID Service feature.
On activating the feature a Timer Job will be assigned to generate Document ID for all the existing documents.
You can make the Timer Job run immediately by going into Central Administration > Monitoring > Check Job Status > Scheduled Jobs link.
Click on the Job named Document ID assignment job and in the appearing page click on the Run Now button. This should create Document ID for all the documents in site collection libraries.

Document ID Settings

Once the feature is activated you can change the settings through Document ID Settings link as shown below. This link is only visible once you enable Document ID Service feature.
In the appearing page you can change the Settings like Prefix for generating ids for example: COM-DOC-1, COM-DOC-2 etc. You can assign a prefix of 4 to 12 characters.
Click the OK button to continue. You need to run the Timer Job again to see the immediate assignment of IDs.

Viewing the Document ID

Once the Document ID is assigned you can go back to any library in the Site Collection and use the View Properties menu item to view the ID associated.
In the appearing dialog the Document ID is shown.
 
Note: Copying documents will create new Document ID for the new document. Moving document will retain the original Document ID.

Using Document ID Programmatically

Now we can use Server Object Model to get a document using the Document ID. Please follow the following steps to achieve the same:
Step 1: Create a new SharePoint 2010 > Console Application
Step 2: Add reference to Microsoft.Office.DocumentManagement assembly
This assembly should be residing in the 14hive\ISAPI folder. You can use the Add Reference dialog box > SharePoint tab for locating the same.
 
Step 3: Create the code to fetch by Document ID.
You have to use a DocumentIdProvider instance to perform the same. The following code fetches the Document URL based on the Document ID.
static void Main(string[] args)
{
    SPSite site = new SPSite("http://localhost/sites/newsitecollection");
    DocumentIdProvider provider = DocumentId.GetProvider(site); ;
    var result = provider.GetDocumentUrlsById(site, "DOCUMENT-4-1");
    Console.WriteLine(result[0]);
    Console.ReadKey(false);
}
You need to change the Site URL and Document ID string according to yours. On running the code the URL of the Document is shown.
With the document URL in hand you can write your own code to manipulate it.
Note: You can also use the DocumentId.FindUrlById() method to retrieve the URL as a string.

References

Summary

In this article we have explored the Document ID feature and retrieving documents based on the Unique ID.
Following are the points to summarize with:
  • Document ID Service is a Content Management Feature
  • A Site Collection Level Feature
  • Once activated all documents are assigned unique IDs
  • Programmatically we can retrieve document by ID
  • Prefix can be used along with the Document ID
  • A Timer Job is responsible for assigning the IDs

No comments: