Insert SharePoint 2010 Document ID in a Word document

So the document id feature is a cool thing with SharePoint, but how can you add the document id inside the document text? My client wanted to use the document id as a reference value in their letters, in the classic “Our ref.” field.

Adding the document id for one document is easy: First save your document in a library where the document id feature is enabled, close and re-open the document, select insert – quick parts – document properties, and select the “Document Id Value”.

But end users don’t want to be bothered with inserting quick parts! They use office templates and want the value to pop up by default in the correct place.

Now this turned out to be a challenge, but here is the best way I have found so far: You will need:

  • A content type in your content type hub
    • the document if feature must be enabled in the CT hub
  • A site collection where the document ID feature is enabled.
  • A site collection where the document ID feature is NOT enabled.
  • Both site collections must be subscribing to the content type hub, so they inherit the content type.

Prepare your content type:

  • Make sure the DocId featue is enabled in the content type hub.
  • In the content type hub, go to site settings – site content types.
    • Create a content type if you haven’t already done so.
  • Edit your content type, select “Information management policy settings”.
  • Click “Enable Labels”.
  • In the Label format input field, write {Document ID Value} and click OK.
  • Republish your content type (under “Manage publishing for this content type”).
  • Run the following timer jobs:
    • “Content Type hub”
    • “Content Type Subscriber – <webapp name>”.

Prepare your Microsoft Word Document Template

  • Create a document library in the site collection where there is no document id.
    • Note: It is crucial that the DocId feature is disabled. If your document template gets a document id here, it will be stuck in the template, and all documents based on that template will get the same document id as the template, which is obviously not what you want!
  • Go to library settings, advanced settings, and enable content types.
  • Click “add from existing site content types” and add your content type.
  • Upload your document template to this library.
  • Make sure that it is given the correct content type.
  • Open the template in MS Word from the library.
  • Place the cursor in the correct location.
  • Click Insert – Quick Parts – Document Properties – Label.
  • Add a Word macro to the template: Open the template in Word, click the Developer tab – Macros – call it Reload and hit Create – use the following code (I got this from this MSDN forum thread)
Sub Reload()
   ActiveDocument.Reload
End Sub
  • Add another macro to call Reload directly after save. This will make the Document ID show in the document, and the user can print etc.
Sub FileSave()
   Dialogs(wdDialogFileSaveAs).Show
   Reload
End Sub
  • Save and close word.
  • Upload the template to the content type:
    • In the content type hub, go to site settings – site content types, edit your content type, click advanced settings, add it there.

Prepare the document library

  • Create a document library in the site collection where document ids are enabled.
  • Go to library settings, advanced settings, and enable content types.
  • Click add from existing site content types and add your content type.
  • Your content type (including the template) is now available for the end user.

The end user can now get the document id for free

  • In the document library ribbon, click “New Document” and select the newly created content type.
  • Fill in the desired fields etc. in the template and save it to the SP library.
    • SharePoint will now generate a document Id for the document.
  • Voilá, after save the reload macro is called, and the {Document ID Value} text is replaced with the actual document id value:

Background
The document template has the label containing the document id. When a user creates a document from the template and saves it to a SharePoint Library where the document id feature is enabled, SharePoint will assign a document id to the document, and demote this value from the list/library into the document itself.

Please add a comment below if you find this post useful.

Note: If you do not use a content type hub, please see Isaac Blum’s blog post on the same subject.

Note also that when a document that has a document ID is uploaded to SharePoint, the document ID will be replaced by a new document ID, as it is considered a copy. Because of this, you might want so send your letters that contain document IDs as PDF documents (or on paper of course).

About Øystein Garnes

Software Project Manager, former .Net and SharePoint 2010 developer, husband, Boston Terrier owner, amateur caveman chef, amateur genealogist, cross country skier, food blogger.
This entry was posted in SharePoint and tagged , . Bookmark the permalink.

34 Responses to Insert SharePoint 2010 Document ID in a Word document

  1. Jason says:

    Hello,

    I get this error:

    There were errors on the page:
    •The label reference, Document ID Value, could not be found.

    In the CT Hub when I try to add the Label to the content type. I do not have the Document ID Feature enabled in the CT Hub. Should I? If so, you might want to add that as a step in the beginning.

    Thanks!

    • Hi Jason,

      You’re quite right, the DocId feature must be enabled also in the Content Type Hub. I’ll update the blog post!

      Thank you and good luck!

      Øystein

  2. Emma says:

    Hi,

    I’m having a nightmare with the macros. I have added them to my Word 2010 form, saved it as a .docm & added it to my content type. When I create a new document in the SharePoint document library the macro runs fine, however if a user on another machine does the same then the macro doesn’t run. Please help as this is driving me mad!!

    Thanks,

    Emma

    • Hi Emma,

      I’m sorry to hear about your nightmare! Hard to say what could be the problem, but I’ll give it a try. There are security settings on the Office clients. Could the security level for macros be different on the machine with the problem? I believe it is possible to tell Office to never run macros.

      Also, have you made sure that the macros are in the Word document that is opened on the client where it does not run? Try pressing Alt + F11 and see if the expected macro is there. If not, there could be something with the content type. Are you using a content type hub? There could be some synchronization issues.

      Good luck, and let me know how it goes. 🙂

      • Dominik Gauss says:

        Check again that you inserted the macro into your custom template and not (as i experienced first) into your local “normal.dotm” template while you created your custom template

      • Thanks Dominik. Yes, it’s very easy to make that mistake.

        Also, the macro I provided above should be seen as a starting point. The macros must be different to handle libraries where checkout is required, and be more complicated to handle both cases. And you should consider overriding both FileSave() and FileSaveAs().

  3. M J Norrie says:

    Thanks for this, it helped enormously. We managed this on a single site collection with Document ID Service enabled.

    We managed to get the Document ID on to the template without needing the non-document id enabled site collection or the Content Type Hub.

    The behaviour it gives is as follows. When a new document is created, the document id quick part displays the words “Document ID Value” instead of an actual ID until the document is saved and reopened. We decided not to use the macro to save and reopen as our administrators don’t like macros and it is not worth the fight!

    Here’s how we did it: Create content type and set up the label {Document ID Value} as you describe (this assumes Document ID Service is enabled). Go to a document library and add the content type. Create a new document (based on the content type) and add the Quick Part for Document ID Value (it appears on my machine simply as “Label” at this point. dont know why!). Once you are happy with the template, DO NOT SAVE the document to the SharePoint library, instead use “Save As” to another non-SharePoint location, I used the desktop. (this put in the quick part without assigning a value to it).

    Now, use this document as the template for the content type.

  4. Christos K says:

    Nice!

  5. Mai Borset says:

    Hi, Thanks it works great for Word Documents,
    Would there be another formula/solution for Excel documents and PowerPoint.
    I have some kind of a solution regarding Excel; the ID/Version # is showing up on the front page of the Excel document, but not in the footer of each page.
    PDF I feel is impossible, but do you have any other solutions regarding this?

    • Hi Mai, thanks for your comment.

      I’m afraid I have no experience with inserting the SharePoint Document ID into any of the other Microsoft Office document types. Since I’m not working with SharePoint for the time being, I am not able to try it out for you. I’d love to hear your results here if you dig more into it!

      As for the PDF question, that depends on what you’re trying to achieve and how. The only thing I’ve tested, is to create PDF documents from Word documents after the Document ID has been successfully inserted. This is easily done, e.g. with a PDF printer or by using the save as PDF option in Office 2010.

      Best Regards,
      Øystein Garnes

  6. Derek says:

    is it possible to do something like this for other office programs? i need to do this for excel and visio, and possibly powerpoint as well..

  7. Avinash says:

    Please help. I am having issues with the approval status and document id and version not showing up on my documents. I am using content type hubs. my documents are beginngin with SP-DOC. show i make this setting in both content type hub and the real library.

  8. Avinash says:

    I am using content type hub feature to hold all my content types…. procedures, work instructions etc. In my site collection i am subscribing to these content types from the hub. I created document templates (which have a label in the header, holding the information DOCID, Version, Approval Status)which go into the content type hub. But however, when a user opens the document, They are not showing the DOCID, Version, Approval Status. I think i figured out the problem. I need to make templates from blank documents. Now, They are showing up on the documents only for me and people with approve permissions. Not showing up the versio and approval status for read only permission users.

    • The users need permissions to save the documents into the SharePoint document library, because the document id is being generated when the document is saved…

      Best Regards,
      Øystein Garnes

      • Cate says:

        Hi,

        Like Avinash, I’m also trying to get the Approval Status into our document template headers using a content type hub. But the Approval Status field can’t be added to the Label of a publishable content type (it is “Not Found”), because the Approval Status column only gets added once the content type has been added to a document library with content approval setup. So how can you do this in the hub and publish to your subscribing sites collections?

  9. Avinash says:

    Hi Oystein,

    Thank you very much. I followed all the steps, except macro. Now, I followed using Macros and it saved my document approver some time and shows up the approval status, docid and version. However, this is causing the approver of documents to create version one and version two. Reason being when she opens pending documents and save them it fixes the document id and all others but increases version from one to two. Anyway we can avoid this.

    Thanks,
    Avinash

    • Hi Avinash,

      The site admins can change the settings for libraries. Specifically, you want to look at the version settings, there are many things you can change here. Also, your macros must handle different types of version settings. I’ll send you a suggestion for the macros.

  10. Ken Stockton says:

    In your first set of bullets, you mention the “document if” feature. Did you mean “Document ID” feature?

  11. Patrick says:

    Hi Oystein,
    I really enjoyed your post on this.
    I would like to add images to header and text to footer to word docs in sharepoint lib
    My goal is users will pic the image and text they would like to add.
    The images and text could be even stored in libraries or list.
    Do you think this is possible with the same approach you have here with DocumentID.?
    Thanks in advance and let me read from you.

  12. Christos K says:

    Nice! Thank you.

  13. redlegcraig says:

    Hello is there a manual way to refresh/reload (like Update Fields) to bring back the ID information?
    I have a library where users will be saving documents in from other places, and the close/reopen requirement seems silly. I’m guessing the other option would be to load the macro into another template (perhaps in the startup folder) and give them a button to use it? Thanks,

    • The document id is generated on the server on save, so you will have to find some way to get it back to the client. For me, the close and re-open macro was good enough, but please do let me know if you find a better way 🙂

  14. Shafaqat Ali says:

    Hi
    Works well for me if i select a document template but doesn’t work if i upload a document and select my content type(my content type has labels enabled, and uses custom template).
    so this approach will not work if i upload document, right?

  15. Dan Smith says:

    ConnectUNow.com has a sharepoint doc id for word install. It’s worked well for me. Their site does not have it up yet but I was one of their beta testers. I think you guys can get a good deal before they release it 🙂 You get a ribbon with a few buttons Save to sharepoint – insert document ID – reload document

  16. Rob Sleking says:

    Thanks for the useful workaround! First time right!

  17. Mick M says:

    Hi,
    Does anyone know if it is possible to get a similar workaround working in 2013 Foundation? The content type hub isn’t available in Foundation. I only really need the built in “ID” field to be inserted into the footer of my Word docs. There’s not an option for me to chose “Document ID Value” from in the quick parts. I have the version number and approved date working as a quick part thanks to a codeplex solution, I just need the ID to complete my requirements, Thanks.

Leave a reply to Øystein Garnes Cancel reply