El Blanco's Office 2007 Blog

Thursday, September 13, 2007

Programmatically Activating an InfoPath Form Template

A colleague today asked me whether it was possible to programatically activate an InfoPath form template from the form templates store in Central Administration to a site collection. After much digging around in the MOSS SDK I couldn't really find anything until I stumbled across the forms services SDK online. Using a combination of the two I came up with the following which shows how to do this:

Firstly you'll need to add a reference to the Microsoft.Office.InfoPath.Server.dll assembly, which can be found at the following location:

C:\Program Files\Microsoft Office Servers\12.0\Bin

After adding a reference to this assembly you can use the following code fragment to achieve the requirement:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.InfoPath.Server.Administration;

SPSite siteCollection = ...
SPServiceCollection services = SPFarm.Local.Services;

foreach (SPService svc in services)
{
    FormsService fs = svc as FormsService;
    if (fs != null)
    {
        foreach (FormTemplate ft in fs.FormTemplates)
        {
            if (ft.Name == "<the name you want>")
            {
                ft.Activate(siteCollection)
                break;
            }
        }
        break;
    }
}

Hope this helps !

1 Comments:

  • Great Stuff! dude...I appreciate your great work.

    By Anonymous Anonymous, at 3:01 am  

Post a Comment

<< Home