13APR 2010

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


Posted by Tobias Zimmergren

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!

  • Bacchu Santhosh

    Thanks for the awesome post, can this be developed as a sandbox solution?

    • http://www.zimmergren.net/ Tobias Zimmergren

      No problem. You can just try switching the project over to Sandbox = true and try it out or reference the classes on MSDN to see if they support the Sandbox.

  • Amit Dubey

    Hi,
    Thanks for the great post.

    Although, my query is bit off-track, but I think you can explain me.
    What if I want to use different settings for different Doc Libs in same Site Collection, how can we approach this?

    Regards,
    Amit

  • http://www.backupmd.net/news_and_media/sports/ sports

    A very good and informative article indeed . It helps me a lot to enhance my knowledge, I really like the way the writer presented his views.

  • Oberoiamit77

    The link to download the source is not working? Can you please post the correct one.

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Oberoiamit77,

      Unfortunately some of the download-links are broken since I moved over from my old blog platform to this new one hosted in WordPress. Just follow the steps one by one and you’ll end up having the same source as me.

      Regards,
      Tobias.

      • Oberoiamit77

        Hi, thanks for the information provided by you, i was able to develop something but being a newbie i am facing a very unique problem and I am not sure why this happening. Here it goes:
        1. I deployed the solution on the server.
        2. Called it on one of the website through server and it generated the unique document id.
        3. Now I called the same website through a machine on LAN and connecting to the same server, but this time no document id was generated.

        Could you please suggest on why this might be happening?

        Thanks for all your support.

  • http://www.holidayiq.com/kumarakom-resorts-15-430-13.html Kumarakom resorts

    I have found the post very useful and informative. Thank you for sharing this one. I’ll continually be coming on a regular basis you just read your posts.

  • Subramanya

    Thanks for the post…. i struggled a week for this…… It’s very useful for me…..

    One more issue i am still working on is, to concatenate the input fields value into my Custom generated document ID. Document ID is created once the file is uploaded i.e., ItemAdded Event. But the input fields will be given only after the document DI is created (This comes in ‘ItemUpdated ‘ Event)

    Can you please help me on this…. Because of this i am still unable to complete the project…..

  • M

    The download link no longer works – do you still have this available?

  • Pingback: 70-489 Developing Microsoft SharePoint Server 2013 Advanced Solutions – Preparation links | Martin Bodocky