How to Read Document Properties Programmatically

n
n

nThis was the one of good learning while workingnwith MS Office Add-In using visual studio so thought to share.

n

nEach document ships with some kind of metadatanwith it, and to them we call as properties. For word documents there are threentypes of properties available (As per my knowledge). And here are those

n

n(Examples / APIs below are accessible when youncreate new application level add in, reference links are from MSDN pages wherenexcel is considered as MS Office Application)
n

n

n1.   nBuiltnIn Properties / Default Properties – These are set ofnproperties which are available with any word document, example of these can ben– Title, Description, Keywords. More Information/Reference – Here

n

nYouncan programmatically deal with these properties as –

n

nDocumentProperties builtInDocProperties = (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties;

n

nforeach (DocumentProperty prop innbuiltInDocProperties)

n

n{

n

n   try

n

n   {

n

n     //Console.WriteLine(prop.Namen+ ” – ” + prop.Value);

n

n   }

n

n   catch { }

n

n}

n

n

n

n2.   Custom Properties – More Information / Referencen- Here

n

nDocumentProperties customDocProperties = n(DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

n

n

n

nforeach (DocumentProperty prop inncustomDocProperties)

n

n{

n

n   try

n

n   {

n

n//Console.WriteLine(prop.Name + ” -n” + prop.Value);

n

n   }

n

n   catch { }

n

n}

n

n

n

n3.    Content Type Properties – Thesenare the set of properties which are as the site columns / fields    in content type of the document library. Innshort these are server side properties of the document when document is openednfrom the document library. You won’t be able to access these properties programmaticallynwhen document is opened locally on machine. i.e. unless and until document isnnot opened from SharePoint document library , these properties will not benavailable to do to get/set operations. More Information Here / Reference – Here

n

n

n

nMetaProperties _ctProperties = (MetaProperties)Globals.ThisAddIn.Application.ActiveDocument.ContentTypeProperties;

n

nforeach (MetaPropertyn_ctp in _ctProperties)

n

n{

n

n   try

n

n   {

n

n      n//Console.WriteLine(ctp.Name + ” – ” + ctp.Value);

n

n   }

n

n   catch { }

n

n}

n

n

n

nNoticed catch blocks while iterating through forneach loop? Yes, these are required because few properties throws exception andnreason is unknown to me. For example, in content type properties whenevernpeople and group type of property is accessed, exception is thrown. You cannread more on the MSDN Forums about this issue. – Herenis the link.

n

nMore ever you can set the server side propertiesnprogrammatically using same API like –

n

nMetaProperties _ctProperties = (MetaProperties)Globals.ThisAddIn.Application.ActiveDocument.ContentTypeProperties;

n

n_ctProperties [“Title”].Valuen= “TestTitle”;

n

n

n

nYou just need to call the save method of thendocument instance to get these properties reflected in document library.

n

Leave a Comment