I would like to add Infopath form, that is a new item in the Form library programmatically. This would be using any event handler.

Please guide.


See this link, which solves the same problem:


I copy here the text from the article in case it goes down in the future:

InfoPath item creation has always been a bit tricky since there is a dependency with the template.xml inside the MyForm.xsn.

What we essentially did was automatically extract this template file from the published .xsn form. Allowing the form to be updated without causing issues to with code.

There is a great CabExtractor that i found on CodeProjects and Stackoverflow: https://stackoverflow.com/questions/8533105/minimum-c-sharp-code-to-extract-from-cab-archives-or-infopath-xsn-files-in-mem

This CabExtractor allows us to open up the published xsn file and extract the template.xml from the compressed file.

public static void ExtractTemplateFile(string xsnPath) {
    byte[] data = null;
    var fileStream = SPContext.Current.Web.GetFile(xsnPath).OpenBinaryStream();
    try {
        data = new byte[fileStream.Length];
        fileStream.Read(data, 0, data.Length);
    } finally {
    byte[] buffer;
    int bufferLength;
    using(CabExtract extractor = new CabExtract(data)) {
        extractor.ExtractFile("template.xml", out buffer, out bufferLength);
    using(MemoryStream ms = new MemoryStream()) {
        string content = "";
        using(MemoryStream templateStream = new MemoryStream(buffer)) {
            using(StreamReader reading = new StreamReader(templateStream)) {
                content = reading.ReadToEnd();
                content = content.Replace("solutionVersion = \", "href = \"" + SPContext.CurrentWeb.Url + " / MyLibrary / Forms / template.xsn\"solutionVersion = \"");
        using(StreamWriter writer = new StreamWriter(ms)) {
        SPFile templateFile = SPContext.Current.Web.Files.Add(" / SiteAssets / template.xml", ms.ToArray());

Once we’ve extracted the file we can now create a new file in the form library using the data from the template.xml and then populating the promoted properties on that content type.

public static void CreateNewFormItem(MyDTO data) {
    SPList list = SPContext.Current.Web.Lists.TryGetList("MyLibrary");
    SPFile template = SPContext.Current.Web.GetFile(" / SiteAssets / template.xml");
    byte[] fileData = template.OpenBinary();
    SPFile newFile = list.RootFolder.Files.Add(list.RootFolder.Url + " / " + data.Title + ".xml", fileData);
    // Populate item with data from DTO
    SPListItem item = list.GetItemById(file.Item.ID);
    item["Somefield"] = data.SomeField;

Something to take note on while updating the SPListItem object from the form library is that some fields might be set to ReadOnly. One way to handle this to set the fields that you will need to populate to not be read only in code for example in a Feature. Or a class that temporarily set the fields to be editable and the switch them back to read only mode once the item has been updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.