Document IDs help you manage your documents by providing an easy way to track items regardless of their location. For example, if a legal contract gets moved from a document library on one site to a document library on another site, the contract would be easy to locate based on its Document ID. Document ID’s are automatically assigned to uploaded documents and this ID will follow the item throughout its entire life cycle. Document IDs can also be assigned to Document Sets.
What is Document ID Service and why it is important ?
Document ID Service facilitates the users with the following functionalities
- Generate a unique ID to each documents in the docment library
- Enable the users to search the documents using unique ID
- Create custom document ID by extending the document ID provider
Many enterprise organizations require this feature, because they have a huge volume of documents to be managed and it is difficult to search the documents as those documents are stored in different site collections. Using Document ID feature, users can able to search the document anywhere in SharePoint using the unique document ID.
Let’s walkthrough on how to enable the Document ID Service and to create a custom Document ID Service.
Document ID Service is a site collection feature and it is not activated by default. So, first if we want to use the document ID feature, we need to activate that feature.
Go to Site Settings -> Site Collection Features -> Document ID Service and click to activate button to activate.
Once the Document ID Service feature is activated,
Go to Site Settings -> Go to Site Collection Administration Section -> Select Document ID Settings link
In the above diagram, we can give a static value, which will be the unique document ID for the documents. These static value will be suffixed by an integer and for each document the integer value will be increased by one as shown in the below diagram.
Using out-of-the-box feature, we can only provide some static value as unique document ID for all documents. But, this might not use for all the scenarios. We might need the unique ID’s generated dynamically based on some logic like current datetime, combination of document metadata, etc., To fulfill these scenarios, we can extend the OOTB document ID provider and create our own custom document ID provider.
In order to create custom document ID provider, we need to use “Microsoft.Office.DocumentManagement” assembly, which is available only in SharePoint Server Standard license and above.
Creation of Custom Document ID :-
To create the custom document ID provider, open up the favorite Visual Studio 2010 IDE and select Empty SharePoint Project. Make sure that you have selected the .NET Framework 3.5 version and not .NET Framework 4.0 version.
Right click the Features folder and add a feature called “CustomDocumentIDService” and provide appropriate title and description for the feature. Finally, change the scope of the feature to Site. After creating a feature, right the click the feature and add a feature receiver as shown in the below diagram
In order to create custom document ID provider, we need to implement an abstract class namely “DocumentIDProvider”, which provides the following methods and property
GenerateDocumentID GenerateDocumentId method is used to do our logic on generating the custom document ID. It consists of a single input parameter SPListItem, so that we can generate the document ID based on the metadata of the SPListItem.
GetDocumentUrlsById This method is used to implement a custom search and returns an array of URLs that point to the documents. This method is used in conjunction with DoCustomSearchBeforeDefaultSearch property. When this property is true, this method is called before default SharePoint search and returns the array of string of URLs. If the property is false, first the default SharePoint search called, if it does not return any value, then it executes this method to return the string of URLs.
GetSampleDocumentIdText This method is used to set the default document id
DoCustomSearchBeforeDefaultSearch Returns either true or false, if true, the GetDocumentUrlsById() method called before default SharePoint search happens , otherwise, default SharePoint search executes, if no result returns, it will call the GetDocumentUrlsById() method.
In this sample, we will generate the unique document ID as “CustomDocID” and suffixed with the current date & time. Here is the code that generates the unique document ID
Once the generation logic is defined, we need to set custom document ID provider to the site collection, to do this, we need to have a feature receiver class as shown below
In the above code, SetProvider() method to set the custom document ID provider and SetDefaultProvider() method to revert back to default document ID Provider. After successful deployment of the custom document ID provider, below screen shows custom document ID feature
Once this feature is activated, the default document ID provider is disabled and SharePoint identified that there is a custom document ID provider installed by displaying the message on the Document ID Settings page
This message shows that the custom document ID provider installed successfully. Hereafter, every document that is uploading in this site collection will have the custom document ID.