How to read repeating fields from Info Path request form

Posted on January 8, 2010. Filed under: Code Snippets | Tags: , , , |

This custom method can be used to read a single field or repeating fields of a Info Path request form from a sharepoint workflow.

//xpathNode – The actual Xpath node value in the datasource of your info path form
//mutiplenodes – a bool variable that defines whether you are parsing a single node or trying to parse subsequent child nodes under the parent “xpathNode”
//childNode – repeating childnode xpath value under the parent “xpathNode”  in the info path request form.
  
private string parseInfoPathRequestForm(string xpathNode, bool mutlipleNodes, string childNode)
        {
            SPFile requestForm = workflowProperties.Item.File;
            byte[] requestFormData = requestForm.OpenBinary();
            XPathDocument inForm = null;
            string parseNode = xpathNode;
            string parseNodeValue = string.Empty;

            try
            {
                using (MemoryStream ms = new MemoryStream(requestFormData))
                {
                    inForm = new XPathDocument(ms);
                    ms.Close();
                }
                XPathNavigator inFormNav = inForm.CreateNavigator();
                inFormNav.MoveToFollowing(XPathNodeType.Element);
                XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());

                foreach (KeyValuePair<string, string> ns in inFormNav.GetNamespacesInScope(XmlNamespaceScope.All))
                {
                    if (ns.Key == String.Empty)
                    {
                        nsManager.AddNamespace(“def”, ns.Value);
                    }
                    else
                    {
                        nsManager.AddNamespace(ns.Key, ns.Value);
                    }
                }

                if (!mutlipleNodes)
                {
                    XPathNavigator nodeNav = inFormNav.SelectSingleNode(parseNode, nsManager);
                    if (nodeNav != null)
                        parseNodeValue = nodeNav.Value;
                }
                else if (mutlipleNodes)
                {
                    XPathNodeIterator reviewers = inFormNav.Select(parseNode, nsManager);
                    foreach (XPathNavigator person in reviewers)
                    {
                        parseNodeValue = parseNodeValue + “;” + person.SelectSingleNode(childNode, nsManager).Value;
                    }
                }
            }
            catch (Exception ex)
            {
                //Write the Exception to the Portal Log.
               
            }

            return (parseNodeValue);
        }

Usage :
//Repeating section-
 _RequestReviewers = parseInfoPathRequestForm(“//my:ptFields//my:authorDetails//my:pickReviewers”, true, “my:reviewerSelector//my:contactSelector//my:Person//my:AccountId”); < Here pickReviewers is a repeating section with a contact selector (reviewerSelector) placed in that.>
//Single field –
_PublicationType = parseInfoPathRequestForm(“//my:ptFields//my:authorDetails//my:publicationType”, false, “”);

Advertisements
Read Full Post | Make a Comment ( 2 so far )

    About

    Xperiments n Xperiences. For the Last 5 years I have been providing variuos portal solutions (SharePoint focused) to my clients and now I intend to publish few of those solutions 'Not As It Is', which might help you. – Mash

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS

    Meta

Liked it here?
Why not try sites on the blogroll...