First let me explain the problem I'm trying to solve (My environment is SharePoint 2010 and I'm using Infopath 2007 template and InfoPath Forms Services)

I have a infopath forms template(.XSN) in a SharePoint Document Library. I want to programmatically upload these forms to Central Admin (manually we can do it using Upload form template from central admin). I have found(as of now) two approaches to upload the forms to forms server. One is using FormsServices class (code sample below, Microsoft.Office.InfoPath.Server.dll ) and other is running a sharepoint power shell script from c# code.

Below is the code for using FormsService class. This works perfectly if I run it as a console application. But when I run the application from SharePoint Context (through a webpart) I'm getting

access denied

error when invoking UploadFormTemplate method. I'm running the code with elevated priveleges but still no good. I'm not sure what security priveleges are required to upload the form to central administration programmatically. Basically I'm copying the file to C:\Users\Administrator\AppData\Local\Temp and then uploading the file to Central admin because UploadFormTemplate method requires a physical path.

FormsService localFormsService;
SPFarm localFarm = SPFarm.Local;
Int16 formTemps;

try
{
FormTemplate temp = new FormTemplate();
localFormsService = localFarm.Services.GetValue<FormsService> (FormsService.ServiceName);
            localFormsService.FormTemplates.UploadFormTemplate
(@"C:\Users\Administrator\AppData\Local\Temp\expensereport.xsn");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.Write("Press Enter to Continue");
Console.ReadLine();
}

I also tired the following power shell. This doesn't work in SharePoint Context but works well if I run it as a console application.

 Runspace runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault());
 runspace.Open();

 string script = @"Install-SPInfoPathFormTemplate -Path 
 C:\Users\Administrator\AppData\Local\Temp\EF001_v4_1_0.xsn";
 PowerShell powerShellCommand = PowerShell.Create();
 powerShellCommand.Runspace = runspace;
 powerShellCommand.AddScript("Add-PsSnapin
 Microsoft.SharePoint.PowerShell");            
 powerShellCommand.AddScript(script);
 foreach (string result in powerShellCommand.Invoke<string>())
 {
     Console.WriteLine(result);
 }
 runspace.Close();
 Console.ReadLine();
link|improve this question

55% accept rate
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.