PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

SP 2010: SharePoint Server 2010 - Creating a custom Document ID provider 

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

In this article I will talk about how you can create your custom Document ID provider for your SharePoint Server 2010 installation. Sometimes I've been getting the question weather or not it's possible to change the behavior or change the way the Document ID's are generated, and some people have a tendency to say no to that question, just because there's no interface or out of the box functionality to do so.

I'll give you a quick walkthrough of how you can extend your Site Collection by adding a custom Document ID provider, that will automatically generate custom ID's based  on your own algorithms entirely!

Recommended reading about Document ID's before proceeding: 
http://msdn.microsoft.com/en-us/library/ee559302(office.14).aspx

Document ID overview

This section will give you a very brief conceptual overview of Document ID's in SharePoint Server 2010.

What is Document ID's?

Document ID's in SharePoint Server 2010 provide you with the ability to tag documents with a unique identification number. Something a lot of my clients have done manually or by implementing custom solutions to take care of in SharePoint 2007. With this new feature, you get all the required functionality to tag documents with unique identification numbers based on a specific pre-set formula with a custom prefix.

See this sample screenshot for an example:
image

Where do I enable Document ID's for my Site Collection?

In order to enable Document ID's in your Site Collection, you'll need to activate the Site Collection Feature called Document ID Service.

See this screenshot for an example:
image

How do I change the way my Document ID's are generated?

If you want to alter the way the Document ID's are generated for your documents in your Site Collection, you can do that by navigating to:

Site Actions - Site Settings - Document ID Settings, like so:
image

From this new settings page, you'll get the possibility to tell SharePoint how it should generate your unique ID's. You can specify a prefix for all the generated ID's:
image

I want to take it one step further!

If you're not quite satisfied with the way SharePoint 2010 generates your Document ID's for you, then you should most definitely follow along with the rest of this article as I will guide you through the steps to create your very own Document ID provider to generate exactly the kind of ID's you want - based on your very own code/algorithms!

Bring it on!

Learning about a SharePoint 2010 Custom Document ID provider

This section will give you an overview of what you will need in order to create a custom Document ID provider for SharePoint Server 2010!

Note: As of this writing MSDN isn't fully updated on these new SharePoint Server namespaces. Some details may or may not change when SharePoint Server 2010 is released into the wild (RTM)

Microsoft.Office.DocumentManagement

The namespace Microsoft.Office.DocumentManagement contains a class called DocumentIdProvider, which will be the base for our upcoming project!

Microsoft.Office.DocumentManagement.DocumentIdProvider

This is the class we will derive from when creating our custom provider. It contains three (3) abstract methods and one (1) abstract method that we need to implement:

A description of each of these methods and the property will be made inline in my code in the samples!

Creating a SharePoint Server 2010 Custom Document ID provider

Let's code this little piece of functionality, shall we. The final project (very basic) will look something like this:
image

So, let's get coding.

1. Create a class and derive from DocumentIdProvider

public class CustomDocumentIdProvider :
Microsoft.Office.DocumentManagement.DocumentIdProvider
    {
        // Method to generate the actual document ID. Awesomeness lives here!
        public override string GenerateDocumentId(SPListItem listItem)
        {
            // In this method we will tell SharePoint how it should generate
            // the unique ID we want.
            // In my case, I've just created a dummy-generator. 
            // Normally you would perhaps want to fetch this from another system or
            // generate it properly instead of like this.. 

            // Points to a method I've created that generates foo-ID's
            return FooSampleIDGenerator.GetFooUniqueID();
        } 

        public override bool DoCustomSearchBeforeDefaultSearch
        {
            // If set to true: It will call the GetDocumentUrlsById method before search
            // If set to false: It will use SharePoint Search before custom methods
            get { return false; }
        } 

        public override string[] GetDocumentUrlsById(SPSite site, string documentId)
        {
            // Returns an array of URLs pointing to 
            // documents with a specified DocumentId
            // An empty string array 

            // This is where you will implement your logic to find
            // documents based on a documentId if you don't want to use
            // the search-approach.
            return new string[] { };
        } 

        public override string GetSampleDocumentIdText(SPSite site)
        {
            // Returns the default Document ID value that will be initially
            // displayed in the Document ID search web part as a help when searching
            // for documents based on ID's.
            // This should correspond with the way you've designed your ID pattern
            return "AWESOME-12345-67890-SharePointRules";
        }
    }

2. Create a Feature Receiver to hook up your custom provider with your Site Collection

public class ProvisionCustomDocIdProviderEventReceiver : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        DocumentId.SetProvider(properties.Feature.Parent as SPSite,
                                                                 new CustomDocumentIdProvider());
    }
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        DocumentId.SetDefaultProvider(properties.Feature.Parent as SPSite);
    }
}

Actually, you're all done with the code for now.

3. Go to Document ID Settings page and see this message appear

image

This basically means that your custom provider has been successfully enabled (as per our Feature Receiver).

4. See that the Document ID's on your documents now is using your custom provider

(Please allow for some time to pass so the Timer Jobs can do their magic, or manually go into Central Admin and run the timer jobs instantaneously)

Behold, awesome custom Document ID provider in action:
image 

Summary and Download

What did we just do?

What we just did was to create a custom Document ID provider that generates our very own custom Document ID's based on whatever algorithm or pattern we want. There's no need to follow the built-in format for your generated IDs - which some people have presented in their seminars and blogs. So there you go, step by step!

This could be especially awesome if you've got an external system generating Document ID's already, and you want SharePoint to use those ID's alongside whatever other system is running. Use your own imagination as to what can be done. The code is in your hands, Obi Wan Coder!

Download project

You can download my sample project here

Enjoy, and please don't be afraid to leave comments!

 
Posted on 13-Apr-10 by Tobias Zimmergren
31 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: 2010, Custom Search, How To, SP2010, Tips
 

Links to this post (Trackbacks/Pingbacks)

Comments

Tuesday, 13 Apr 2010 09:26 by Robin
Mate! I was almost too afraid to comment because of the awesomeness that is dripping of this post (btw make this comment window a bit bigger.. it's impossible to write something down here) To cut the crap, really nice post mate. Can't wait to use to stuff at customers! :)

Wednesday, 14 Apr 2010 10:59 by Sezai
This is awesome, clients usually specify a custom document numbering requirement for document management systems in SharePoint. Its so much nicer to make use of what's available in the platform instead of building it all from scratch yourself

Thursday, 15 Apr 2010 11:07 by Brian Hodge
this is most spectacular. everyone i have talked to said this was not possible, that you had to use the way put forth by the designed GUI. even microsoft people said this was not possible. THANKS YOU TOBIAS! i owe you a beer or 5 at the conference next week!

Thursday, 15 Apr 2010 07:17 by Rob
very good post about doc ids. did not think u could change this prefix thing. can u have it to whatever u want, to follow whatever standard our company has?

Thursday, 22 Apr 2010 08:56 by Dave McDavey
Very sexy stuff. I like how you can customize the usage of the Document ID's to your own choice. Great job covering it!

Thursday, 13 May 2010 01:45 by Tobias Zimmergren
Thanks for the comments guys! Appreciate it!

Thursday, 13 May 2010 01:46 by Tobias Zimmergren
Rob, basically you decide for yourself what the Document ID should generate in this case. So yes, you can follow your company standard. It's all just simple C# development :-)

Friday, 14 May 2010 04:13 by Dennis Wylde
Good Lord! Thanks for providing this code, exactly what I need to get my project finalized!

Saturday, 15 May 2010 10:16 by Glenn Ljung
tja tobbe. grym artikel. den får full poäng i min bok. hoppas vi ses på usergroupmötet i stockholm

Monday, 21 Jun 2010 07:48 by Ninja Awesome
Hayiiia! great article and very informative shit. will take this with me to my SharePoint safegarden behind the lotus flowers! *dissapears with smoke granades*

Sunday, 11 Jul 2010 08:10 by
I have developed a custom document provider and it works fine. But the problem is that the GenerateDocumentId method gets called twice for a document set type. Any ideas on it?

Thursday, 29 Jul 2010 12:01 by Hans van den Akker
Hi Zimmergren, Is document Id generation running in the User context or System context? I can imagine system creation scenario's and user. But is the kicker consistent in user either system context? Mucho awesomeness ofcourse.

Friday, 6 Aug 2010 01:41 by Matt
Hi, great post, howver I seem to have an issue that when the feature is deployed, and I view the properties of the document or navigate to http:///_layouts/DocIdReDir.aspx?ID= I get the following error. Operation Completed Successfully No Documents with the ID were found in this site collection. Any ideas? Many Thanks

Friday, 6 Aug 2010 01:56 by Matt
Hi, great post, howver I seem to have an issue that when the feature is deployed, and I view the properties of the document or navigate to http:///_layouts/DocIdReDir.aspx?ID= I get the following error. Operation Completed Successfully No Documents with the ID were found in this site collection. Any ideas? Many Thanks

Saturday, 7 Aug 2010 11:03 by Matt
Hi, great post, howver I seem to have an issue that when the feature is deployed, and I view the properties of the document or navigate to http:///_layouts/DocIdReDir.aspx?ID= I get the following error. Operation Completed Successfully No Documents with the ID were found in this site collection. Any ideas? Many Thanks

Name:
URL:
Email:
Comments: