Friday, June 22, 2012

Generating Custom Document ID in SharePoint 2010

Earlier we saw what Document ID is. If you have not read the post about Document ID in SharePoint 2010, I would strongly recommend you reading it first before reading this post.

Once you have fair understanding on document ID, this is the next step on generating your own document ID in SharePoint 2010.

First question that comes into mind is why would we need to generate custom document ID? 

Well, the answer is simple. If you have any specific requirement to generate ID based on some department, some functions inside your organization which helps you to search for document easily plus taking an advantage of document ID feature so that even if the document is moved anywhere across the site collection, you can use the same URL that is generated for document.

First thing to note is that we need to use Microsoft.Office.DocumentManagement namespace which has a class called DocumentIdProvider.

In this class we need to override three methods and one property.

1)    GenerateDocumentId  - Method – This is the method where we will actually generate our own document ID

2)    GetDocumentUrlsById  -Method – This returns an array of URLs which points to this document.

3)    GetSampleDocumentIdText  - Method – Default Document ID value that will be shown in the search web part as a part of help.

4)    DoCustomSearchBeforeDefraultSearch – Property – If set to true, then it will call the GetDocumentURLsById method before search or if set to false, then it uses SharePoint search before custom methods.

Now let’s go ahead and create a class that inherits from DocumentIDProvider and writes these methods.

Go ahead and create a project and add a reference to DocumentManagement namespace.

And here is the entire code of the class

Note: Replace Name with Title property of web to get the title of web.

We have written code in respective methods based on the summary that we have noted earlier for each method and property above.

Now we are done with one part. Next, we need to register this custom document id with the site collection via feature. So go ahead and create a feature scoped at site collection level.

Now go ahead and add the event receivers for this feature.

Remember we would be applying our custom document ID generator when we activate this feature and when we de activate this feature we want SharePoint to use default generator back again.

So we are going to write code only in two methods activated and deactivating.

Before deploying change the property on activate feature default to false by going to the properties settings of the feature.

Now build the project and deploy the package.

Open the site and go to the site collection feature. Make sure that Document ID service feature is turned on.

Also activates the feature that we have deployed.

Now go to Document ID settings under site collection administration options.

And you will notice that it says custom document id provider has been configured for this site collection.

And there you go once you upload documents, you get new DocumentID generated by your custom code.

When you turn the feature off, it sets back to default Document ID provider.  

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to