Life and things around it (WP)

Life experiences, SharePoint, coding issues and photography…

SharePoint 2010 : How to retain Document ID while moving documents

leave a comment »

<<< Cross posted from my main blog here >>> 

Today’s water cooler discussion revolved around Document ID management system of SharePoint. It is a nice feature and can actually help to find the documents quickly and in a predicted manner. Basically you get a short URL in SharePoint for your document, rather than a cryptic one and SharePoint will find the document no matter where you moved it.

But there is a catch, it will be able to find it if you move it within the Site Collection. If you move it out of the site collection, a new Doc ID will be generated and assigned to the document. BOOM!! your short URL is pointing to nothing now.

So how do we move documents without changing their Doc ID?

For this, we would need to understand how the document ID generation, assignment etc. happens. There is a comprehensive document on MSDN which provides there details. You can read about it here.

So basically, SharePoint uses event handlers to assign Document IDs at runtime (there are Timer Jobs also, but about them – some other day). There is 1 interesting paragraph in the article which catches my fancy.

When a new item is added, SharePoint Server 2010 first checks to see whether the item has a document ID. If the item has a document ID, the server checks to see whether the PreserveID attribute is set to True or False, and then sets it to False if it is currently set to True. If the item does not already have a document ID, the server gets a document ID for the item from the specified provider, writes it to metadata, and sets the PreserveID attribute to False.

This is saying that there is an attribute “PreserveID” which is checked before updating the document ID but is again set to False, effectively not retaining its value. But could not find any explanation for this anywhere on MSDN.

Time to dive into trusted .NET Reflector!

The code for the Document ID infrastructure is under Microsoft.Office.DocumentManagement.dll, precisely in ItemChangedInternal method, where it checks for an internal property on a document “_dlc_DocIdPersistId” (called as PreserveID attribute) in the article. But every time code finds this attribute set to True, it just flips it back to False but skips assigning new document ID, which is exactly we need.

Now for that particular execution cycle, the document ID will not be changed, but next move it will be changed as PreserveID attribute has been set to False. This would work well when you are using custom code to add files to the library and you can use the code as given in a very good blog. But what if you are using OOTB features to move around the documents? How do you set the PreserveID attribute?

It turns out to be a simple trick of adding an ItemAdding event handler for the library and setting the property on every document being added.

public override void ItemAdding(SPItemEventProperties properties)
           properties.AfterProperties["_dlc_DocIdPersistId"] = "true";

This would execute every time a new document is being added to the library and it just says that I want to retain the document ID. ItemChangedInternal handler will check if a document ID exists, it will just skip assigning the new ID, otherwise just assign a new document ID.

Hope this helps out some of you out there.

Happy Coding Smile

Written by Manpreet Alag

March 19, 2013 at 6:09 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: