Accessing secondary datasource through code - InfoPath Dev
in

InfoPath Dev

Accessing secondary datasource through code

Last post 06-20-2007 01:09 AM by spudy. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 05-17-2007 02:46 AM

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Accessing secondary datasource through code

    I found a very useful post from Matt Faus -

    http://www.infopathdev.com/blogs/matt/archive/2006/02/02/Setup-Namespaces-for-a-Secondary-Data-Source.aspx

    I have mapped the namespace for a secondary datasource into my form. There are no errors displayed. I then added some code to count the number of items in the datasource (I know the number but need to use the number for looping purposes)

    The code I used is:

    var oNodeList;

    oNodeList = XDocument.GetDOM("User List").selectNodes("/my:Users/my:Details" );

    var oIndex = oNodeList.length ;

    XDocument.DOM.selectSingleNode("//my:NumberofUsers").nodeTypedValue = oIndex;

    The number is returned as 0 which is not what I expect. Is there something obvious missing here? Do I need to intialize the secondary datasource somehow before the data is accessible?

    Regards Spudy 

  • 05-20-2007 02:37 AM In reply to

    • chang
    • Not Ranked
    • Joined on 03-29-2007
    • Posts 23

    Re: Accessing secondary datasource through code

    When adding the secondary data source to your form, did you select to automatically retrieve data when the form is loaded? If not, you'll need to query the data source before trying to use data that it might contain.

  • 05-25-2007 06:39 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Accessing secondary datasource through code

    Chang, 

    Thanks for your response.  I did select this option. Any other ideas?

    Thanks

  • 06-02-2007 02:42 AM In reply to

    • chang
    • Not Ranked
    • Joined on 03-29-2007
    • Posts 23

    Re: Accessing secondary datasource through code

    Double-check the XPath expression you are using to retrieve the nodes; it might be incorrect.
  • 06-04-2007 01:15 AM In reply to

    Re: Accessing secondary datasource through code

    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-19-2007 11:50 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Accessing secondary datasource through code

    Matt, that is the example I have been working from. I have double checked my xpath but it all looks ok. I am presuming there is something obvious I am missing here. My form has very little to it, just the code I posted originally.

    Any help on this one will be most appreciated.

  • 06-19-2007 12:09 PM In reply to

    Re: Accessing secondary datasource through code

    Hi Spudy

    Look at silly me posting something you've already seen, haha!  I really think this is an XPath issue.  Are you sure that your "User List" DOM is also using the my namespace? If it is from another form, it might be using this same namespace, but with a different value.  You should check this  by double-clicking the nodes and looking at the Details tab, as demonstrated in the tutorial.  You will have to explicitly state the namespace from that tab in your query.  If the URI is different than the Main DOM, I would suggest using a different prefix than "my", to lessen confusion.

    If you are still having trouble, please provide some screenshots from your data source taskpane and the Details tab of Properties on the the /my:User/my:Details node.

    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-19-2007 02:26 PM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Accessing secondary datasource through code

    Matt,

    Thanks for responding so fast. I had already seen it but I did revisit in case I missed something. My secondary datasource doesn't have anything showing under the details tab. To get the string I extracted the files and opened in notepad the xml file and took the detail from there. It is probably worth mentioning at this stage that the data contained within the secondary datasource was originally imported from an excel spreadsheet into XML document.

    Could this have caused an iffy xml document in some way that will not work with infopath? At times infopath seems rather sensitive. I am now wondering if I should get the data in SQL database and use the XML output function to create a clean XML document.

    Thanks again, Spudy.

  • 06-19-2007 04:19 PM In reply to

    Re: Accessing secondary datasource through code

    Hi Spudy,

    From what you're telling me, it seems like your XML does not have a namespace at all.  Try the XPath "/Util/Details" (without namespace prefixes).  I think that should work.  If that still doesn't work, could you paste an example of the XML that your form is trying to query against?

    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-20-2007 01:09 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Accessing secondary datasource through code

    Matt, You are right about the namespace. After my last post I decided to create a new datasource (from an infopath form) and then set this up as a secondary datasource. My code then did retrieve the correct count. I then compared the two XML files - and there was a very obvious difference. The root of my original file had a namespace, but none of the elements did.

    I am presuming now that I will be able to search this secondary datasource using an xpath filter and then drop results into a group as part of the main datasource to enable the use of a master detail table. But first I need to clean my datasource......

    Many thanks for your help.......

Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.