Paul's profilePaul Galvin's SharePoint...BlogListsSkyDrive Tools Help

Blog


    October 27

    Quick Hit: Reading InfoPath XML Directly From an SPListItem in SharePoint

    I’m been working on a project where I need to extract attachments from an InfoPath form.  There are some good resources for parsing InfoPath forms (which are just XML files, so it’s actually quite easy). 

    While I was building up the project, I started by downloading an InfoPath form and saving it to my local hard drive.  My c# code was reading directly from that instance.  However, the InfoPath forms are really living inside a SharePoint forms library.  I did a little half hearted searching to find out how to read it directly from the library and almost gave up, in which case I would have saved the form to a local temp directory and read it from there.  However, there’s no need to go through those hoops as you can read it directly from the library.  This little snippet shows how:

    /// Class definition stuff here, including:
    private SPFile mySharePointFile; /* Part of an SPList */
    // More code goes here and inside a method of the class we have:
    XmlTextReader textReader;
    textReader = new XmlTextReader(mySharePointFile.OpenBinaryStream());
    
    textReader.WhitespaceHandling = WhitespaceHandling.None;
    
    textReader.Read();
    
    // If the node has value
    
    while (textReader.Read())
    {
    
      … and so on and so forth …

     

    They key bit above is that we can read the InfoPath directly via the OpenBinaryStream() method call on the SPFile as a parameter to the constructor on XmlTextReader.  It works great.

    </end>

    Subscribe to my blog.

    Follow me on Twitter at http://www.twitter.com/pagalvin

    Technorati Tags:

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Paul Galvin has turned off comments on this page.

    Trackbacks

    The trackback URL for this entry is:
    http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!4936.trak
    Weblogs that reference this entry
    • None