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 occur when the same document exists 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 page that appears, Activate the Document ID Service feature.
On activating the feature a Timer Job will be assigned to generate Document IDs 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 page that appears click on the Run Now button. This should create Document IDs for all the documents in site collection libraries.
Document ID Settings
Once the feature is activated you can change the settings through the Document ID Settings link as shown below. This link is visible only after you enable the Document ID Service feature.
In the page that appears 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 dialog that appears the Document ID is shown.
Note: Copying documents will create a new Document ID for the new document. Moving the document will retain the original Document ID.
Using Document ID Programmatically
Now we can use the Server Object Model to get a document using the Document ID. Please use the following steps to do that.
Step 1: Create a new SharePoint 2010 > Console Application
Step 2: Add a reference to the 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 it.
Step 3: Create the code to fetch by Document ID
You have to use a DocumentIdProvider instance to do it. 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
http://tinyurl.com/7s9ffby
In this article we have explored the Document ID feature and retrieving documents based on the Unique ID.
The following are the points to summarize:
- The 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