Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Document ID Feature

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
30 Aug 2012CPOL3 min read 31.6K   178   6   1
The Document ID feature and how to programmatically retrieve documents based on the IDs.

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

Image 1 

Document copy 2

Image 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.

Image 3

In the appearing page Activate the Document ID Service feature.

Image 4

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.

Image 5

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.

Image 6

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.

Image 7

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.

Image 8

In the appearing dialog the Document ID is shown.

Image 9 

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

Image 10

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.

Image 11 

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.

C#
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.

Image 12

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
QuestionFacing an error while retrieving the url Pin
Member 105447887-Feb-14 18:59
Member 105447887-Feb-14 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.