Moving Documents in SharePoint 2010 without assigning new ECM Document IDs

9. February 2012

Using the new ECM Document ID service in SharePoint 2010, poses a challenge when it comes to moving documents in code. For example in a custom workflow that routes documents from one location to another. Moving documents with custom code will by default trigger the document id provider to assign new IDs to the documents in the new location. It goes like this no matter if you are using the default provider or a custom document id provider.

Fortunately, there is a way to programmatically add a document with a preset document id. All you need to do is to add the document with the two properties _dlc_DocId and _dlc_DocIdPersistId as shown in the code example below.

private SPFile AddFile(SPFolder folder, string filename, Stream content, string docId)
{
    var properties = new Hashtable();
    properties["_dlc_DocId"] = docId;
    properties["_dlc_DocIdPersistId"] = "True";
    return folder.Files.Add(filename, content, properties);
}

The document id provider will when _dlc_DocIdPersistId="True", assign the document id provided in the _dlc_DocId property rather than generating a new id.

The Document ID service in SharePoint is pretty nice and useful. But it does have some caveats that you should be aware of before you decide to employ it in your organization. But I am preparing another post on this topic – stay tuned!

SharePoint 2010, ECM