Register Code Behind InfoPath Form for Web Browsing

This post describes how to deploy a web browser compatible InfoPath form, with code-behind, through a SharePoint feature.

Installing and registering an InfoPath form, along with a custom content type is pretty straight forward once you know the required steps.

Start by creating a content type that inherits from the Forms type. The following is a sample content type’s markup.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x0101010076AEDEA7CA2A44B59ECB3D491618E052"
        Name="Custom Form CT"
        Group="Custom"
        Inherits="TRUE"
        RequireClientRenderingOnNew="FALSE"
        Version="0">
    <FieldRefs>
    </FieldRefs>
    <DocumentTemplate TargetName="/FormServerTemplates/CustomForm.xsn" />
  </ContentType> 
</Elements>

Continue reading

Posted in InfoPath, SharePoint | Tagged , | Leave a comment

InfoPath Object Reference not set Error

After designing an InfoPath form and as you try to upload and register the form to SharePoint, through the Central Administration site for example, you may see the following error:

Object reference not set to an instance of an object

You may also see the following error when previewing the form within InfoPath Designer:

InfoPath cannot open the selected form. There was an error while loading the XML Schema for the form.

Continue reading

Posted in InfoPath | Tagged | 1 Comment

Sharing WebPart and User Control Properties

When creating a Visual WebPart, sometimes you want the main functionality to be contained within the user control and not the WebPart, so that, for example, you can use the WebPart on multiple pages, one where the WebPart is hosted and users can edit the properties and another where only the user control is loaded and no WebPart manager, or WebPart zones, are available to allow editing of the properties.

In the later case, the properties defined in the WebPart would either be set to default values or be retrieved from the query string.

Now, how to setup the user control to receive the WebPart’s property values when contained in a WebPart?
Continue reading

Posted in SharePoint | Tagged , , | 2 Comments

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)
	{
		protectedDocElement.Remove();
	}
 
	wordDocument.MainDocumentPart.Document.Save();
}

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

Posted in Office, SharePoint | Tagged , , | Leave a comment

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:

SPWebService.ContentService.WebConfigModifications.Add(authorizedTypeActivity);
SPWebService.ContentService.Update(true);
SPWebService.ContentService.ApplyWebConfigModifications();

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

SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
 
SPWebService.ContentService.WebApplications[webApplication.Id].WebConfigModifications.Add(authorizedTypeActivity);
SPWebService.ContentService.WebApplications[webApplication.Id].Update(true);
SPWebService.ContentService.ApplyWebConfigModifications();
Posted in Configuration, SharePoint | Tagged , | Leave a comment