Issue retreiving data from SP list - InfoPath Dev
in

InfoPath Dev

Issue retreiving data from SP list

Last post 07-13-2011 04:16 PM by crb2011. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 07-13-2011 04:16 PM

    • crb2011
    • Not Ranked
    • Joined on 07-13-2011
    • NZ
    • Posts 1

    Issue retreiving data from SP list

    Hi,

    I'm new to this forum so hello. I've been using Nintex/Infopath on SP2010 for about 2 weeks now (previous experience was in PHP so quite a change for me). Was getting along OK until I came across this issue.

    Here is the problem code, it is supposed to retreive data from a SP2010 list and store it in a dictionary. Apologies, I can't see how to copy the code across so that it retains its formatting?

    public static Dictionary<int,Dictionary<string,string>> GetSPList(DataSource FormDataSource, string CnxnXPath, XmlNamespaceManager FormNamespaceMngr, string QueryString)

    {

     

    // Gets the contents of a SharePoint list and returns a dictionary containing a dictionary for each

    // row, with items stored as a key/value pair

    // 'FormDatasource' is defined as 'this.DataSources["DataSourceName"]'

    // 'CnxnXPath' can be found by right-clicking on the group name in the 'Fields' explorer window

    // under myFields > datafields and clicking 'Copy XPath'

    // 'FormNamespaceMngr' is usually 'this.NamespaceManager'

    // 'QueryString' specifies which data to include. Use '*' for everything

    // Changed:

    // 2011-07-14 CRB

    // 2011-07-12 CRB Created

     

     

    // Declarations

    Dictionary<int,Dictionary<string,string>> ContentDict = new Dictionary<int,Dictionary<string,string>>();

    string ThisItemKey, ThisItemValue = "";

    Dictionary<string, string> RowDictionary = new Dictionary<string, string>();int RowCount = 0;

     

    // Create Navigator for Data Source

    XPathNavigator MyNavigator = FormDataSource.CreateNavigator().SelectSingleNode(CnxnXPath, FormNamespaceMngr);

    // Iterate through all nodes in Navigator

    while (MyNavigator.MoveToNext())

    {

    // Create Iterator to iterate through Navigator node

    XPathNodeIterator MyIterator = MyNavigator.CreateNavigator().Select(QueryString, FormNamespaceMngr);

    // Examine each item(node) in Iterator

    foreach (XPathNavigator Item in MyIterator)

    {

    // Assign item key and value to appropriate variables

    ThisItemKey = Item.Name.ToString();

    ThisItemValue = Item.ToString();

    // Add key and value to dictionary

    RowDictionary.Add(ThisItemKey, ThisItemValue);

     

    // Clear item variables

    ThisItemKey = "";ThisItemValue = "";

    }

    // Add RowDictionary to ContentDict

    ContentDict.Add(RowCount, RowDictionary);

    // Increment RowCount

    RowCount = RowCount + 1;

    // Clear items from RowDictionary

    RowDictionary.Clear();

    }

     

    // Return Dictionary

    return ContentDict;

    }

    This would then be consumed by something like the following (note this is not the final code, just a way of testing the class above.

     public void InternalStartup()

    {

    // Declarations

    string MyLF = MyNamespace.TextTools.LineFeed();
    string AllData = "";

    // Get Sharepoint list data

    Dictionary<int,Dictionary<string,string>> ContentDict = Massey.DataXfer.GetSPList(this.DataSources["BuySomeStuff"],

    "/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW",NamespaceManager,"*");

    int RowCount = 0;

    // Iterate through each 'row' in the dictionary

    foreach(KeyValuePair<int,Dictionary<string,string>> RowDict in ContentDict)

    {

    AllData = AllData + RowCount +
    ": ";

    // Iterate through each key/value pair in the 'row'

    foreach(KeyValuePair<String,String> Item in RowDict.Value)

    {

    AllData = AllData +
    " {" + Item.Key + "," + Item.Value + "}";

    }

    RowCount = RowCount + 1;

    AllData = AllData + MyLF;

    }

    // Show results

    MyNamespace.FieldTools.UpdateField("/my:myFields/my:field1", AllData, this.MainDataSource, this.NamespaceManager);

     

    }

    This returns the following:

    0:
    1:

    It should return 3 rows (there are 3 in the SP list) with the list data included.

    I know the form code works because I tested it with the following, mimicing the dictionary I am trying to create in the first examnple:

    public static Dictionary<int, Dictionary<string, string>> TestGetSPList()

    {

    // Declarations

    Dictionary<int, Dictionary<string, string>> ContentDict = new Dictionary<int, Dictionary<string, string>>(); Dictionary<string, string> RowDictionary = new Dictionary<string, string>();

    // Create first RowDictionary

    RowDictionary.Add("DeliveryLocation", "Wellington");

    RowDictionary.Add("DeliveryDate", "14/07/2011");

    RowDictionary.Add("HappyWithStuff", "Yes");

    RowDictionary.Add("ProductDescription", "Laptop");

    RowDictionary.Add("ID", "193");

    // Add RowDictionary to ContentDict

    ContentDict.Add(1, RowDictionary);

    // Clear RowDictionary

    RowDictionary.Clear();

    // Create second RowDictionary

    RowDictionary.Add("DeliveryLocation", "Auckland");

    RowDictionary.Add("DeliveryDate", "15/07/2011");

    RowDictionary.Add("HappyWithStuff", "No");

    RowDictionary.Add("ProductDescription", "Desktop");

    RowDictionary.Add("ID", "234");

    // Add RowDictionary to ContentDict

    ContentDict.Add(2, RowDictionary);

    // Clear RowDictionary

    RowDictionary.Clear();

    // Create third RowDictionary

    RowDictionary.Add("DeliveryLocation", "New Plymouth");

    RowDictionary.Add("DeliveryDate", "18/07/2011");

    RowDictionary.Add("HappyWithStuff", "Yes");

    RowDictionary.Add("ProductDescription", "Monitor");

    RowDictionary.Add("ID", "332");

    // Add RowDictionary to ContentDict

    ContentDict.Add(3, RowDictionary);

    return ContentDict;

    }

    If anyone has any suggestions, I would be very grateful, I have tried numerous ways of fixing it but can't seem to make it work and deadlines are looming :(

     

    Many thanks
    Chris

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