How to add “SharePoint Document ID” in a library documents footer?

Standard

Document ID Service is enabled in SharePoint for assigning Unique Identity for each Document, uploading in SharePoint Document libraries . Users are generating documents on their Personal systems and once finalized they upload it in SharePoint document libraries for utilizing the SharePoint Search capabilities. The Challenge was to insert SharePoint generated Document ID into the Footer of each document (docx, .xls, .pdf, .jpg, .txt and others).

After some R&D, I came up with the below solution. here I am posting the Code for .docx

Requirement: 

Document ID should be added in footer of the document when user uploading  in SharePoint document library.

Steps:

1 – Enable Document ID service in SharePoint

2 – Install XML2.0 on machine

3 – Write a custom code and make wsp solution

4 – Run the .wsp solution at farm level using Power Shell

This solution will add the “Document ID” on the footer of the library document in a web application. For more details on custom code for others, please contact 🙂

Below is the Code for .docx files. Enjoy it!!!!!

You also need to make a change in Element.xml of this event receiver so that code execute Synchronous:

  •  <Synchronization>Synchronous</Synchronization>

ItemAdded Event Receiver of SharePoint


Option Explicit On

Option Strict On

Imports System
Imports System.Security.Permissions
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Security
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.SharePoint.Workflow
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging
Imports System.Linq

Public Class DocumentUpload
Inherits SPItemEventReceiver

”’ <summary>
”’ An item was added.
”’ </summary>
”’
Public Overrides Sub ItemAdded(ByVal properties As SPItemEventProperties)
MyBase.ItemAdded(properties)

Dim fp As FooterPart
Dim fr As FooterReference
Dim rID, DocID As String
Dim addedList As SPListItem = properties.ListItem
Dim addedFile As SPFile = addedList.File
Dim mem As New System.IO.MemoryStream
Dim b As Byte() = addedFile.OpenBinary

mem.Write(b, 0, b.Length)

Me.EventFiringEnabled = False
addedList.SystemUpdate(False)

Dim doc As WordprocessingDocument = WordprocessingDocument.Open(mem, True)
Dim mdp As MainDocumentPart = doc.MainDocumentPart
Dim xe As OpenXmlElement = doc.MainDocumentPart.DocumentSettingsPart.Settings.FirstOrDefault(Function(el) el.XmlQualifiedName.Name.Equals(“documentProtection”))

If Not xe Is Nothing Then
xe.Remove()
End If

If mdp.FooterParts.Count > 0 Then
fp = mdp.FooterParts.FirstOrDefault
Else
fp = mdp.AddNewPart(Of FooterPart)()
End If

rID = mdp.GetIdOfPart(fp)
DocID = addedList.Properties(“_dlc_DocId”).ToString

GenerateFooter(DocID).Save(fp)

For Each sc As SectionProperties In mdp.Document.Body.Elements(Of SectionProperties)()
fr = New FooterReference()
fr.Id = rID
fr.Type = HeaderFooterValues.Default

sc.RemoveAllChildren(Of FooterReference)()
sc.PrependChild(fr)
Next

mdp.Document.Save()
doc.Close()
addedFile.SaveBinary(mem)
Me.EventFiringEnabled = True
End Sub

Private Function GenerateFooter(ByVal DocumentID As String) As Footer
Dim f As Footer
Dim ps As New ParagraphStyleId
ps.Val = “Footer”

f = New Footer(New Paragraph(New ParagraphProperties(ps), New Run(New Text(DocumentID))))
Return f
End Function
End Class

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s