Parse "PreferredName" from innerXML - InfoPath Dev
in

InfoPath Dev

Parse "PreferredName" from innerXML

Last post 12-02-2014 06:11 AM by arkainus. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 12-01-2014 03:53 PM

    Parse "PreferredName" from innerXML

    I have a DataConnection that queries the UserProfile service from sharepoint, which I want to use to populate a field in my infopath form. I'm sure everyone here who has entertained such a need by now has seen a post similar to this for a codeless solution and used it. When my form requirements were simple, that was perfect for my needs. As my form has grown more complex, I need to see if there's a way I can do this programmatically, because I need to create form load conditions to switch views based on the status of the form and who loads it up, and to do so with rules would be VERY cumbersome due to the various conditions. I would keep the rule to retrieve the username value at loading, and then use code based off of that, but unfortunately in the browser the code runs first for loading, then the loading rules are ran next, so I wouldn't have a value in my "CurrentUser" field, so... I'm out of luck there.

    Playing around with the code, I've done this so far, which admittedly isn't much:

    DataSource userName = this.DataSources["GetUserProfileByName"];
    XPathNavigator MainDS = MainDataSource.CreateNavigator();
    XPathNavigator SetValue = MainDS.SelectSingleNode("/my:myFields/my:testest", NamespaceManager);
    SetValue.SetValue(userName.CreateNavigator().InnerXml.ToString());

    Using that, I can see the entire XML that is returned by the secondary data source. It is an incredibly long string, but I do see the PreferredName element in that ginormous string, and it contains the value I want. Does anyone know how I can parse this string and just retrieve the value of the preferredname element? Or, is there a better method of doing this programmatically that I'm unaware of? My searching for a solution on performing this seems to be everyone talking up their no code solution to this issue, or people talking about looping through a secondary data source to populate a repeating table.

    For reference, I attached a text file with a portion of the xml string, so you can see what it looks like. I was going to put it in the post, but I couldn't figure out how to properly format the post with the xml in there. Thanks in advance for any help you may be able to offer here.

  • 12-01-2014 04:22 PM In reply to

    Re: Parse "PreferredName" from innerXML

    string preferredName = userName.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserProfileByNameResponse/tns:GetUserProfileByNameResult/tns:PropertyData/tns:Values/tns:ValueData/tns:Value[../../../tns:Name = 'PreferredName']", NamespaceManager).Value;

    Your XPath may vary a little - your data connection in the form may not be using "tns" for the namespace prefix - so you'll need to right click the "Value" field in the data source task pane and copy the XPath. And this code doesn't handle for the node not being present - you may want to make sure the returned node is not null prior to trying to get its value. :)

    Hilary Stoupa

  • 12-02-2014 06:11 AM In reply to

    Re: Parse "PreferredName" from innerXML

    Perfect! Thank you SOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO much!
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.