Archive for January 16, 2013

Working with Word and OpenXML

When working with Word documents using the OpenXML SDK, it’s not obvious how to remove document protection. The following code shows you how:

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open("[path to Word document]", true))
	DocumentFormat.OpenXml.OpenXmlElement protectedDocElement = wordDocument.MainDocumentPart.DocumentSettingsPart.ChildElements.FirstOrDefault(el => el.XmlQualifiedName.Name.Equals("documentProtection"));
	if (protectedDocElement != null)

Following on from that, it’s also straight forward enough to remove macros from a document (or template):
Read more

Web Config Updates

The SPWebConfigModification class for SharePoint provides an easy way of updating the web config for a web application. Sometimes, however, I’ve seen it used incorrectly.

For example, it’s not advisable to try and write to the web config on Web or Site feature activation as you will more than likely receive an access denied error. Even running the code with elevated permissions will not resolve this as the majority of the time the executing application pool user will not have sufficient access to the web config file, certainly not in a production environment.

The ideal place to host this type of call is in a Web Application or Farm scoped feature’s FeatureActivated method. Unless the farm has been locked down, this will run with the required permissions to be able to update the web config file(s).

An easy way to update the configurations for all web applications hosting content is to use the following from a Farm scoped feature:


Or, to update a specific web application from a Web Application scoped feature:

SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

Get every new post delivered to your Inbox

Join other followers: