1
votes

I'm having a problem getting XML documents that I am creating programmatically and adding to a form library in SharePoint to open with an InfoPath template instead of just opening as plain XML documents in Internet Explorer.

Basically, I have a web service that checks a database to see whether or not certain conditions are true. If they are, the web service programmatically creates an XML document that has the same XML schema as the files created by the InfoPath template mentioned earlier. The usual process is that a real user opens up this InfoPath template, fills out the data and clicks the SUBMIT button, which creates the XML document behind the scenes and saves it in the form library. When the user goes into the form library to view that XML document later, the document automatically opens with the InfoPath template (instead of just opening as an XML document in Internet Explorer).

I'm trying to track down why my XML documents generated from the web service are behaving differently in that regard than the XML documents generated from InfoPath. Here is the snippet of code I'm using to create the XML document inside the web service:

        //specify path to the SharePoint site and the form library
        var clientContext = new ClientContext("http://urlToSiteContainingTheFormLibrary");
        var site = clientContext.Web;
        clientContext.Load(site);
        var folder = site.GetFolderByServerRelativeUrl("FormLibraryNameHere");
        clientContext.Load(folder);            
        clientContext.ExecuteQuery();

        //create new file to add to the form library
        var fci = new FileCreationInformation();
        var encoding = new System.Text.UTF8Encoding();

        //load the InfoPath document's XML schema from resources file
        var baseXml = XElement.Load(new StringReader(Properties.Resources.BaseXml));
        //fill out the appropriate elements and attributes of the XML here
        //......
        //

        //set remaining properties of the new FileCreationInformation object
        byte[] array = encoding.GetBytes(baseXml.ToString());

        fci.Content = array;
        fci.Overwrite = true;
        fci.Url = "DocumentName"            

        //add the new file to the form library
        var newFile = folder.Files.Add(fci);
        clientContext.Load(newFile);
        var item = newFile.ListItemAllFields;
        clientContext.Load(item);

        //set metadata for the xml file here
        item["HTML_x0020_File_x0020_Type"] = "InfoPath.Document.3";
        item["TemplateUrl"] =
            "http://templateUrlHere/template.xsn?openin=preferclient&noredirect=true&xsnlocation=/templateLocation/template.xsn";
        //set remaining item properties......

        //update changes to the item
        item.Update();

        //commit the changes
        clientContext.ExecuteQuery();

At this point, my XML document is successfully submitted to my form library, but when I go to open it, it opens as plain XML inside Internet Explorer instead of using the template I specified. I would assume the HTML_x0020_File_x0020_Type and TemplateUrl properties of the item would specify enough information for SharePoint to know it needs to open this file using the InfoPath template, but maybe there is some other property specifically that I need to set. Has anyone had any experience with a similar issue? At first I thought the URL I was using for my template was wrong, but I copied it directly from an existing XML document that was created via the InfoPath template so I don't think that is the issue (I've left out any real URLs and filenames in my example code above, so please ignore the fact that those URLs are incorrect). I am continuing to troubleshoot this, but in the meantime I thought someone might have solved this problem before and may be able to provide some insight.

Thank you in advance for any answers, I appreciate any help that is offered.

1
Did you ever figure this out? I am working on a similar issue, but using the SharePoint object model. My current attempt is to set the content type, but it seems like it is getting the correct content type...Sam Sussman

1 Answers

0
votes

I know it has been a while since this question was asked, but I think I found the solution, or at least the problem.

I recently solved the same issue.

When downloading one of the files, which were displayed as xml, when they were actually InfoPath, opening it in notepad showed that there was at least the equal number of null garbage characters as real characters.

It turns out that I was using the XmlTextWriter to write to a MemoryStream. In the XmlTextWriter I am using Encoding.UTF8. Upon changing it to Encoding.Default, the files were correctly saved and displayed as InfoPath.

So to anyone with a similar issue, check the value in the xml file, try deleting the nulls if there are any and reuploading. Then check the code creating the document and if it is writing as UTF8, try a different encoding, like UTF16.