SharePoint – Adding the built-in Document-ID in the document file name

I discovered that this has been done many times after we did it. Anyway, I will share my experience.

Back in 2010 I was involved in a project which consisted of creating a strict SharePoint site with meta-data oriented document storage. No folders what so ever, except for 10 document libraries. This idea wasn’t that hard to implement considering how easy it is to create static metadata in SharePoint and adding it to Content Types. How ever, if you want to put all documents in the same container, you will have to use unique file-names, otherwise files with same names will overwrite each other with new versions.

Now, how do you create a unique file name that i based on the file name which the user has entered? Well, you can always just add a custom ID or the list-item-ID in the end of the file name or you can use folders like normal people and pray that no one uploads a document with identical name.

In this case we had already decided to use SharePoint 2010’s built-in feature for generating Document-ID. By using this we will also be able to use some other features that is connected to the Document-ID, like search and the ability to create dynamic URLs (which i will get back to).

One of the requirements was that the user should never have to see their document in SharePoint without the unique ID in the file name.

Solving the problem using workflow
At first, we did this via workflow when the Item was created but we ran in to some issues.

  • The workflow sometimes kicked in 10 seconds after the item was created. I’ve seen that this can be modified, but even after that it too slow.
  • If the item was updated in the workflow, the System Account would have been the modifier.
  • When users, after setting all metadata and saved the form, the old filename from the form was overwritten with the one set by the workflow.

Solving the problem using event receivers
When uploading a document in SharePoint it will trigger a couple of events. In our case, we were interested in ItemAdding and ItemAdded.

We wanted to use ItemAdding when renaming the file so that it was renamed when the edit form was displayed.
Well, there is no item in ItemAdding so we’re screwed there… ?! I would assume that using ItemAdding we could manipulate the data before the actual commit, but no. And not after the base call either.

ItemUpdating, ItemUpdated
Will not be triggered when uploading.

This is the event that is triggered right after the Item is committed to the document library, and this is what we had to work with.
You can’t change the SPFile.Name because it’s only get, you will have to change the Name Field (FileLeafRef) in the SPListItem properties. You will also have to trigger an update for this change to commit, remember that this will trigger the Update events.
If you want to prevent the update event from trigger you can always just set base.EventFiringEnabled to false and back again after the update is done. A more useful way to do this, is to make an IDisposeable Class that you can wrap your update code with as shown in the example. This is also something that you could use to prevent endless loops in the update event.

Basically it would look like this

And the result is this


The end result


You have to make people aware that if they want to upload a document and add it as a new version to an existing, they have to rename the file locally with the ID before uploading.


About Tobias Eriksson