views:

1223

answers:

4

Hi, I have a form library in my share point site. Programmatically i need to fill some fields. Can i do that? If any one know please provide me some sample code. First i need to retrieve the infopath document and then i need to fill the fields. Please help me.

Thanks in advance.

A: 

The Infopath document is just a regular XML file, the structure of which matches the data sources you defined in the Infopath form.

You just need to access the file via the SharePoint object model, modify it using standard methods (XmlDocument API) and then write it back to the SharePoint list. You must be careful to preserve the structure and insert valid data or you won't be able to open the form using Infopath.

You should really check out a book on SharePoint if you plan to do any serious development. Infopath is also a minefield.

Object model usage examples: here, here and here. The ridiculously incomplete MSDN reference documentation is here.

EDIT: here is some example code. I haven't done SharePoint for a while so I'm not sure this is 100% correct, but it should give you enough to get started:

// Open SharePoint site
using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
{
  using (SPWeb web = site.OpenWeb())
  {
    // Get handle for forms library
    SPList formsLib = web.Lists["FormsLib"];
    if (formsLib != null)
    {
      SPListItem itm = formsLib.Items["myform.xml"];

      // Open xml and load it into XML document
      using (Stream s = itm.File.OpenBinary ())
      {
          MemoryStream ms;
          byte[] xmlData;


          XmlDocument xml = new XmlDocument ();
          xml.Load (s);
          s.Close ();  

        // Do your stuff with xml here ...

        // Get binary data for new XML
        xmlData = System.Text.Encoding.UTF8.GetBytes (xml.DocumentElement.OuterXml);

        ms = new MemoryStream (xmlData); 

        // Write data to sharepoint item
        itm.File.SaveBinary (ms);

        ms.Close ();

        itm.Update ();  
      }


    }
    web.Close();
  }
  site.Close();
}
axel_c
can u please explain me with some code. I am unable implement that. How can i get xml data from infopath file.
Susanthi
item.update() is giving me error saying that the file has been modified by SHAREPOINT\system on 28 Dec 2009 ect..Then that infopath file is not opening because of these changes
Susanthi
Try removing the update() call and see if it works. We did something similar to this for a project and it worked, but I don't have the code at hand to look it up.
axel_c
A: 

It depends a bit on your available tool set, skills and exact requirements.

There are 2 main ways of pre populating data inside an InfoPath form.

  1. Export the relevant fields as part of the form's publishing process. The fields will then become columns on the Document / Forms library from where you can manipulate them either manually, via a Workflow or wherever your custom code is located.

  2. Directly manipulate the form using code similar to what was provided by Axel_c previously. The big question here is: what will trigger this code? An event receiver on the Document Library, a SharePoint Designer Workflow, a Visual Studio workflow etc?

If you are trying to do this from a SharePoint Designer workflow then have a look at the Workflow Power Pack for SharePoint. It allows C# and VB code to be embedded directly into the workflow without the need for complex Visual Studio development. An example of how to query InfoPath data from a workflow can be found here. If you have some development skills you should be able to amend it to suit your needs.

I also recommend the site www.infopathdev.com, they have excellent and active forums. You will almost certainly find an answer to your question there.

Muhimbi
A: 

"1.Export the relevant fields as part of the form's publishing process"

How do you do that?

-Frode

Frode
A: 

What axel_c posted is pretty dang close. Here's some cleaned up and verified working code...

public static void ChangeFields()
{
    //Open SharePoint site
    using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //Get handle for forms library
            SPList formsLib = web.Lists["FormsLib"];

            if (formsLib != null)
            {
                foreach (SPListItem item in formsLib.Items)
                {
                    XmlDocument xml = new XmlDocument();

                    //Open XML file and load it into XML document
                    using (Stream s = item.File.OpenBinaryStream())
                    {
                        xml.Load(s);
                    }

                    //Do your stuff with xml here. This is just an example of setting a boolean field to false.
                    XmlNodeList nodes = xml.GetElementsByTagName("my:SomeBooleanField");
                    foreach (XmlNode node in nodes)
                    {
                        node.InnerText = "0";
                    }

                    //Get binary data for new XML
                    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xml.OuterXml);

                    using (MemoryStream ms = new MemoryStream(xmlData))
                    {
                        //Write data to SharePoint XML file
                        item.File.SaveBinary(ms);
                    }
                }
            }
        }
    }
}
Jeff Burt