Monday, January 16, 2012

SharePoint 2010 Custom Document ID Provider


Document ID in another cool feature in SharePoint 2010. It actally assigns a unique identification number to Document in site collection. In order to make it available in your site collection you need to activate feature "Document ID Service".



After you activate this feature you will get another setting "Document ID Settings" under Site Collection Administration. Here you can specify the prefix for the generated Document ID's.

 Below settings will open on click
  
There could be a situation arise where you are not happy with Document ID format. For example you have two sites "Admin" and "Account" under site collection. You want some naming convention for Document ID in such a way that you can easily found the document location. With default "Document ID" its not possible. To get this you need to create your own Custom Document ID Provider.

Steps to create custom Document ID Provider

Open Visual Studio 2010.

Go to File => New => Project.

Select Empty SharePoint Project template from the installed templates.

Enter the Name "DocumentIDHelper" and click Ok.

Right click on the solution and click on Add a new item.

Select the Class template from the installed template.

Enter the Name as SPDIDProvider.cs and click Ok.
Add the following references to your project
Microsoft.SharePoint.dll
Microsoft.Office.DocumentManagement.dll
 
Add the following namespaces in class file

Using Microsoft.SharePoint;
Using Microsoft.Office.DocumentManagement;
 

The new class needs to be derived from "DocumentIdProvider". DocumentIdProvider contains three abstract methods and one abstract property.

Method 1: GenerateDocumentIdThis method is used to generate Custom Document ID. This method takes one parameters that is of type SPListItem and this method return generated Document ID as string.

Method 2: GetDocumentUrlsById
This method is used to return the url of the document based on Document ID, This method takes two parameters SPSite and string (i.e Document ID). This is helpful

incase we need to search the document and document is still not indexed. If we dont want to return any url we can return empty string as
return new string[] { };

Method 3: GetSampleDocumentIdTextThis is shown as default value in Document ID search webpart.
 
Property 1: DoCustomSearchBeforeDefaultSearch
If returns true then It will call the GetDocumentUrlsById method before search
Id returns False then It will use SharePoint Search before custom methods


Complete code for SPDIDProvider.cs class

public override string GenerateDocumentId(SPListItem listItem)
        {
            try
            {
                string webname = "Root";

                //If site is not root web then get web name otherwise use web name as Root
                if (!listItem.Web.IsRootWeb)
                    webname = listItem.Web.Name;

                string documentID = webname + "-" + listItem.ParentList.RootFolder.Name + "-ID" + listItem.ID;
                return documentID;
            }
            catch (Exception ex)
            {
                GenerateLog(ex.Message);
            }
            return "";
        }

        public override string[] GetDocumentUrlsById(SPSite site, string documentId)
        {
            return new string[] { };
        }

        public override string GetSampleDocumentIdText(SPSite site)
        {
            return "Web-Library-ID1";
        }

        public override bool DoCustomSearchBeforeDefaultSearch
        {
            get
            {
                return false;
            }
        }
Now next step is to set Custom Document ID Provider as default Provider.

For this add new Feature "CustomDocumentIDGenerator" to your Project. Right Click your feature and Add Event Receiver.
 On FeatureActivated Set our custom Provider as Default document ID Provider

      public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPDIDProvider iProvider = new SPDIDProvider();
            DocumentId.SetProvider((SPSite)properties.Feature.Parent, iProvider);
        }


On Feature deactivating set default Document Provider
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            DocumentId.SetDefaultProvider((SPSite)properties.Feature.Parent);
        }


Next step is deployment. After deployment again go to Document ID Settings. And you will see screen as below
See highlighted text "A custom Document ID provider has been configured for the site collection"

Now try to upload document to different sites. You will see diffrent Document IDs format in diffrent webs.

Document ID in Admin Web
Document ID in Account Web 

5 comments:

  1. nice article ! realy informative ;)

    ReplyDelete
  2. Hi Anmol,
    This is very helpfull thanks a lot for the source code which i downloaded and installed the web part, but how will it get activated? As it showing a custom id provider is installed but some how its not giving the id to the uploaded documnets as provided by you in example above. Please advice. my mail id is oberoiamit77@gmail.com

    Thanks!

    ReplyDelete
  3. You've saved me a lot of time. Thank you!

    ReplyDelete

Popular Posts