>
Blog
Book
Portfolio
Search

7/25/2006

5311 Views // 0 Comments // Not Rated

InfoPathology - Part 8 - Auto Populating Data From ASP.NET Pages

Although SharePoint Form Libraries are the natural home for published InfoPath forms, they are not the only home. As long as the form is located in a routable location on a web server, we can directly link to them from an ASP.NET Web Form and populate them with user data.

The "accepted" way to do this is to create cryptic code that involves creating on-the-fly InfoPath DOM and application objects, linking schemas, and manually binding the controls. I took one look at the examples I found in MSDN, and immediately considered a career change. Regardless of not necessarily being difficult or confusing, it simply did not seem like much fun. So here is the way I implemented this, taking full advantage of my managed code.

  1. First, we need the code that creates the cookie:

    Code Listing 1

    1. using System.Web;
    2. using System.Text;
    3. ...
    4. //initialization
    5. StringBuilder sbXML = new StringBuilder();
    6. HTTPCookie ipCookie = new HTTPCookie();
    7. //create xml
    8. sbXML.Append(...);
    9. sbXML.Append("xml markup");
    10. sbXML.Append(...);
    11. //prepare xml for InfoPath
    12. ipCookie.Value = sbXML.ToString()
    13. ipCookie.Value = ipCookie.Value.ToString().Replace("&", " and ")
    14. //load form
    15. ipCookie.Expires = Now.AddMinutes(1);
    16. ipCookie.Save();
  2. Create a private method in your InfoPath form that reads the data from the cookie.  This method first goes into the registry and determines the location of the Temporary Internet Files folder on the client's machine.  We then load the latest cookie file from this folder, since this is going to be our data.  Finally, we wrap this data up and return it as an XmlDocument object.

    Code Listing 2

    1. using System.IO;
    2. using System.Xml;
    3. using Microsoft.Win32;
    4. ...
    5. private XmlDocument GetFormDataFromCookie();
    6. {
    7. //initialization
    8. DateTime newestFileTime = DateTime.Now;
    9. string cookieFilePath = string.Empty;
    10. XmlDocument xmlDoc = new XmlDocument();
    11. //get path to cookies
    12. RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders");
    13. string path = key.GetValue("Cookies");
    14. string[] files = Directory.GetFiles(path);
    15. //get the first cookie
    16. newestFileTime = File.GetCreationTime(files[0]);
    17. cookieFilePath = files[0];
    18. //loop through all cookies
    19. foreach (string fileName in files)
    20. {
    21. //get the newest cookie
    22. if (File.GetCreationTime(fileName) > newestFileTime && fileName.IndexOf("index.dat") = -1)
    23. {
    24. //get the cookie's file
    25. newestFileTime = File.GetCreationTime(fileName);
    26. cookieFilePath = fleName;
    27. }
    28. }
    29. //get raw data from file
    30. using (StreamReader sr = new StreamReader(cookieFilePath))
    31. {
    32. //get xml from raw data
    33. string data = sr.ReadToEnd();
    34. xmlDoc.LoadXml(data);
    35. }
    36. //return
    37. return xmlDoc;
    38. }
  3. Create an OnLoad event handler for your InfoPath form, and place code here that first calls the above function, then parses the XML doc to set the control values, as follows:

    Code Listing 3

    1. try
    2. {
    3. //load value from cookie
    4. XmlDocument xmlDoc = this.GetFormDataFromCookie();
    5. thisXDocument.DOM.selectSingleNode("/my:myFields/my:txtValue1").text = xmlDoc.GetElementsByTagName("value1").ItemOf[0].InnerText;
    6. }
    7. catch (Exception ex)
    8. {
    9. //error message
    10. MessageBox.Show("Error loading data: " + ex.Message);
    11. }

Placing the code in the OnLoad method into a Try...Catch construct ensures us that if there are any problems, the form will load normally and set the control's values to their defaults. We could also add logic that only does this one time, so that forms that have workflow, for example, will only preload once.

No Tags

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...