Tuesday 13 September 2011

Creating a custom seed Document Id provider for Sharepoint 2010 programmatically

One of the main features in Sharepoint 2010 is the Document Id. This field is a UNIQUE id that is setup up for every single document. This Document id is essential for Document Management Systems, Why? because allows you to find a document that lives with thousands in the same house. This is exactly the same than a digital print.

You can setup the system to do that automatically for you, OR you can build your own solution to do it. As usual, if you need a sophisticated Document Id generation, this is your article.

Let’s go to do it step by step.

1- You need to activate the Document ID Service, go to Site Collection Administration and click on Site Collection Features.

image

2- Now find the Document ID Service and click in activate.

image

3- Go to Document ID Settings in the Site Collection Administration.

image

4- Be sure the Assign Document IDs is ticked.

image

5- Now let’s go to create our project. Go to Visual Studio 2010->New Project->Sharepoint->2010->Empty Sharepoint Project.

6- Go to references and add: Microsoft.Office.DocumentManagement

image

7- Go to your project, Features, Add Feature. Rename the feature with this name DocIdProciderFeature. Right Click and Add Event Receiver.

8- Now go to your project Right Click Add Class->Call it netsourcecodeDocumentIdProvider. If you add the

using Microsoft.Office.DocumentManagement and add the class DocumentIdProvider, do a right click and Implent Abstract Class you should get this:

image

Notice we are overriding four methods, this is where we are going to implement the logic. Now remove all the code from the class and copy this one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.DocumentManagement;
namespace netsourcecodeDocumentIdProvider
{
    public class netsourcecodeDocumentIdProvider:DocumentIdProvider
    {
        public override bool DoCustomSearchBeforeDefaultSearch
        {
            //## TRUE=IT WILL CALL GetDocumentUrlsById BEFORE SEARCH
            //## FALSE=IT WILL USE SHAREPOINT SEARCH BEFORE ANYTHING ELSE           
            get { return false; }
        }
        public override string GenerateDocumentId(Microsoft.SharePoint.SPListItem listItem)
        {
            //## THIS IS WHERE YOU WILL IMPLEMENT YOUR ID SEED
            return "NETSC-"+ DateTime.Now.Ticks.ToString();
        }
        public override string[] GetDocumentUrlsById(Microsoft.SharePoint.SPSite site, string documentId)
        {
            //## THIS RETURN AN ARRAY WITH THE URLS WITH THAT PARTICULAR ID
            return new string[] { };
        }
        public override string GetSampleDocumentIdText(Microsoft.SharePoint.SPSite site)
        {
            //## THIS WILL BE RETURNED WHEN YOU ARE GOING TO THE NICE
            //## AND BEAUTIFUL SEARCH BOX... AND TYPE SOMETHING
            return "NETSC-123456789123456789";
        }
    }
}

9- Now go to DocIdProciderFeature.EventREceiver.cs and copy these two methods into your class (don’t forget to change the name of the server where you want to deploy in the _sSharepointSite constant):

private const string _sSharepointSite = @"http://SHAREPOINT_SERVER_TO_BE_DEPLOYED/site/";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   DocumentId.SetProvider(new SPSite(_sSharepointSite), new netsourcecodeDocumentIdProvider());
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
   DocumentId.SetDefaultProvider(new SPSite(_sSharepointSite));
}        

10- Your project should look like this now. image


11- Deploy and Enjoy!. To test it just upload a document and check the properties.


Conclusion: The Document Id will be one of the cores of your solution if you planning to deliver a DMS, so be aware you can for example join Document Id for your current systems and auto generate them for you with this class. Possibilities are endless.


Download the code here:


image

No comments: