Programmatically upload XSN to SharePoint
I have a bunch of InfoPath form templates (xsn) which I want to upload to a SharePoint list programmatically. My program has to upload these form templates to different lists based on predefined logic. When I upload the browser-enabled form templates (xsn) with my code, the forms do not work:
/// <summary>
/// Uploads a file to the specified sharepoint list
/// </summary>
/// <param name="listName"></param>
/// <param name="fileInfo"></param>
/// <param name="listVersion"></param>
/// <returns></returns>
public static bool UploadFile(string listName, FileInfo fileInfo, string listVersion)
{
WebRequest request = WebRequest.Create(fileInfo.URL);
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
request.Method = "PUT";
byte[] buffer = new byte[1024];
using (Stream stream = request.GetRequestStream())
{
using (MemoryStream ms = new MemoryStream(fileInfo.Bytes))
{
for (int i = ms.Read(buffer, 0, buffer.Length); i > 0; i = ms.Read(buffer, 0, buffer.Length))
stream.Write(buffer, 0, i);
}
}
WebResponse response = request.GetResponse();
response.Close();
var client = new Lists.ListsSoapClient();
var batch = new XElement("Batch",
new XAttribute("OnError", "Continue"),
new XAttribute("ListVersion", listVersion),
new XAttribute("PreCalc", "TRUE"));
var method = new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "Update"),
new XElement("Field",
new XAttribute("Name", "ID")),
开发者_如何学C new XElement("Field",
new XAttribute("Name", "FileRef"),
fileInfo.URL));
foreach (string key in fileInfo.Properties.Keys)
{
object value = fileInfo.Properties[key];
method.Add(new XElement("Field",
new XAttribute("Name", key),
fileInfo.Properties[key]));
}
batch.Add(method);
var element = client.UpdateListItems(listName, batch);
var code = element.Elements().First().Elements().First().Value;
if (code != "0x00000000")
throw new Exception(code);
return true;
}
It seems there is more to be done that just pushing a file stream into the list.
Anyone have an idea how to do this?
EDIT More specifically, the error message I get is: This form template is not enabled for viewing in the browser.
UPDATE When I publish the same form with Microsoft InfoPath it works.
you can use this code to convert your uploaded form in browser enabled form as:
FormsService localFormsService;
SPFarm localFarm = SPFarm.Local;
SPSite localSite = new SPSite("http://ServerName");
SPWeb localWeb = localSite.AllWebs["SiteName"];
try
{
localFormsService = localFarm.Services.GetValue<FormsService>(FormsService.ServiceName);
SPFile localFile = localWeb.GetFile("FormLibrary/Forms/FormTemplate.xsn");
localFormsService.BrowserEnableUserFormTemplate(localFile);
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
or you can use this link for more details as:
http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.server.administration.formsservice.browserenableuserformtemplate.aspx
精彩评论