Pages

Tuesday, August 6, 2013

Change MS Office file properties Programmatically (Colligo issues with iPad – SharePoint)

“Colligo briefcase” is a product used for synching/accessing SharePoint documents using iPad. All works well, as long as we use Out-of-the-box libraries. I recently came across a portal which showed a completely different display in iPad. The document Name looks completely different from its web view. After some research we realized that the problem occurs whenever there is a custom implementation (with custom I mean it has Custom List definition, which was used to create a Library instance). But how does the document name change? Or from where is this name picked from?

The answer for this question is: it picks up the document’s “Title” property to display the name in iPad. I am not sure, why Colligo shows it differently for different definition of libraries. I have no idea what makes Colligo decide to take the title property for the display.

Document’s Title property can be viewed by clicking on File Tab (or right click on the document and click on properties , visit Details tab to view the properties):

How do we solve this problem. Below are 2 options which maybe used until Colligo releases an upgrade version with resolution to this problem.
  1. Do not create List definition if we do not really need it. Instead create library instances from the default Library template (we can do this programmatically as well).
  2. Create an event handler to update the “Title” property of the document after upload. We can have this event handler associated with content type so that we need not add it to every single library. Associating and dissociating of the event handler can be done on feature activation and deactivation respectively. This would enable us to remove the event handler without having to deploy the code if Colligo comes up with a solution for this problem.
Below is the code block (console application) to be used to update the “Title” and “Subject”

static void Main(string[] args)
        {
             SPSite site = new SPSite(http://YourSiteURL;);
            SPWeb web = site.OpenWeb();
            string filePath = “path of the file to be updated”;
            SPFile file = web.GetFile(filePath);
            string itemTitle = file.Title;
            Stream streamItemDoc = file.OpenBinaryStream();
            Package packageDoc = Package.Open(streamItemDoc, FileMode.Open, FileAccess.ReadWrite);
            packageDoc.PackageProperties.Title = file.Name.Substring(0, file.Name.LastIndexOf(‘.’));
            packageDoc.PackageProperties.Subject = “Sample Subject”;
             PackagePartCollection packageCollection = packageDoc.GetParts();
            PackagePart part = packageCollection.FirstOrDefault();
            Uri uriProps = part.Uri;
             XmlDocument xmlDocPP = null;
            PackagePart curPP = packageDoc.GetPart(uriProps);
            using (Stream streamProps = curPP.GetStream())
            {
                xmlDocPP = new XmlDocument();
                xmlDocPP.Load(streamProps);
            }
             xmlDocPP.Save(packageDoc.GetPart(uriProps).GetStream(FileMode.Create, FileAccess.Write));
            packageDoc.Flush();
            file.SaveBinary(streamItemDoc);
            file.Properties["vti_title"] = itemTitle;
            file.Update();
         }

Points to remember:
  1. Whenever, we update the package Title property it updates the List Item Title as well and therefore in the penultimate line, the title is updated back to what it was originally.
  2. Windows.Base is the reference required to be added (maybe required only for client projects such as Console Application, Windows Application)