Pages

Tuesday, January 3, 2012

How to Read Document Properties Programmatically


This was the one of good learning while working with MS Office Add-In using visual studio so thought to share.
Each document ships with some kind of metadata with it, and to them we call as properties. For word documents there are three types of properties available (As per my knowledge). And here are those
(Examples / APIs below are accessible when you create new application level add in, reference links are from MSDN pages where excel is considered as MS Office Application)

1.    Built In Properties / Default Properties – These are set of properties which are available with any word document, example of these can be – Title, Description, Keywords. More Information/Reference - Here
You can programmatically deal with these properties as –
DocumentProperties builtInDocProperties = (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties;
foreach (DocumentProperty prop in builtInDocProperties)
{
   try
   {
     //Console.WriteLine(prop.Name + " - " + prop.Value);
   }
   catch { }
}

2.   Custom Properties – More Information / Reference - Here
DocumentProperties customDocProperties =  (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

foreach (DocumentProperty prop in customDocProperties)
{
   try
   {
//Console.WriteLine(prop.Name + " - " + prop.Value);
   }
   catch { }
}

3.    Content Type Properties – These are the set of properties which are as the site columns / fields    in content type of the document library. In short these are server side properties of the document when document is opened from the document library. You won’t be able to access these properties programmatically when document is opened locally on machine. i.e. unless and until document is not opened from SharePoint document library , these properties will not be available to do to get/set operations. More Information Here / Reference - Here

MetaProperties _ctProperties = (MetaProperties)Globals.ThisAddIn.Application.ActiveDocument.ContentTypeProperties;
foreach (MetaProperty _ctp in _ctProperties)
{
   try
   {
       //Console.WriteLine(ctp.Name + " - " + ctp.Value);
   }
   catch { }
}

Noticed catch blocks while iterating through for each loop? Yes, these are required because few properties throws exception and reason is unknown to me. For example, in content type properties whenever people and group type of property is accessed, exception is thrown. You can read more on the MSDN Forums about this issue. – Here is the link.
More ever you can set the server side properties programmatically using same API like –
MetaProperties _ctProperties = (MetaProperties)Globals.ThisAddIn.Application.ActiveDocument.ContentTypeProperties;
_ctProperties ["Title"].Value = "TestTitle";

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

No comments:

Post a Comment