Saturday 4 August 2012

Generating Custom 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.
GenerateDocumentId  - Method – This is the method where we will actually generate our own document ID

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

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

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.

SharePoint 2010 Approval Workflow email - Date Issue


Issue:

In SharePoint 2010. When we use OOTB approval workflow on a content type.
If we have set the duration for the due date as n days.
We receive two mails,
1.       For approver, that the task is allocated. This mail contain the correct date.
2.       For the initiator, that the approval has started. This mail contains the wrong date.



Solution

Got an work around and was able to resolve this issue :)
1.       I have customized the default SharePoint Approval workflow using SharePoint designer.
2.       There is a step for the email to the initiator.
3.       I have added a new step above it to calculate the due date, because the due date calculated initially by the logic always returned Null value (None). Added an action to calculate date

4.       Used the variable on the email template






 5.   Initially the date on the email template was “None” so the output was coming to be 01/01/0001
6.       Now after inserting the new variable we are getting the expected output