Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James 6 posts 26 karma points
    Jan 12, 2012 @ 16:05
    James
    0

    Using GetPreValues

    Hi,

    I was trying to get some prevalues that I created as a drop down list datatype. I had a look through the example that is on the library section of this website (http://our.umbraco.org/wiki/reference/umbracolibrary/getprevaluesbut came across a problem . The XPathNodeIterator doesn't find my prevalues as they don't appear in the xml. I thought they would automatically be put into the xml when you create a datatype.

    Any ideas would be very appreciated.

     

  • Sharon 16 posts 37 karma points
    Jan 13, 2012 @ 00:39
    Sharon
    0

    Check out this code I use to build dropdownlists from datatypes. It gets the prevalues from the database, not the XML.

     

    public static HtmlSelect test(int DataTypeID, string defaultValue) 

            {

                HtmlSelect ddl = new HtmlSelect();

                if (!string.IsNullOrEmpty(defaultValue))

                {

                    ddl.Items.Add(new ListItem(defaultValue, "0", true));

                }

                XPathNodeIterator preValueIterator = umbraco.library.GetPreValues(DataTypeID);

                preValueIterator.MoveNext();

                XPathNodeIterator ValueIterator = preValueIterator.Current.SelectChildren("preValue", "");

                while (ValueIterator.MoveNext())

                {

                    if (string.IsNullOrEmpty(umbraco.library.GetDictionaryItem(ValueIterator.Current.Value)))

                    {

                        ddl.Items.Add(new ListItem(ValueIterator.Current.Value, ValueIterator.Current.GetAttribute("id", ""), true));

                    }

                    else

                    {

                        ddl.Items.Add(new ListItem(library.GetDictionaryItem(ValueIterator.Current.Value), ValueIterator.Current.GetAttribute("id", ""), true));

                    }

                }

                return ddl;

            }

     

    Hope this helps

  • James 6 posts 26 karma points
    Jan 13, 2012 @ 08:39
    James
    0

    Thanks for the reply. That's how I tried to do it myself but XPathNodeIterator preValueIterator = umbraco.library.GetPreValues(DataTypeID) doesn't bring anything back. It's looking at the xml rather than the database.

  • James 6 posts 26 karma points
    Jan 13, 2012 @ 08:42
    James
    0

    This was how I tried to do it (unsuccessfully)

    int categoryDataTypeNodeID =1210;
     

     
    Document dataType =newDocument(categoryDataTypeNodeID);

     
    XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(1210);
      preValueRootElementIterator
    .MoveNext(); 

     
    XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue","");

     
    ListItemCollection categoryListItems =newListItemCollection();

     
    while(preValueIterator.MoveNext())
     
    {
       categoryListItems
    .Add(newListItem(" "+ preValueIterator.Current.Value, preValueIterator.Current.GetAttribute("id","")));
     
    }
     
     
    //Bind data to ddllist
     
    CategoryDDList.DataSource= categoryListItems;
      CategoryDDList.DataTextField="Text";
      CategoryDDList.DataValueField="Value";
      CategoryDDList.DataBind();
  • James 6 posts 26 karma points
    Jan 13, 2012 @ 08:55
    James
    0

    Got it working, it was my mistake, I had the wrong id in XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(1210);

  • Sharon 16 posts 37 karma points
    Jan 13, 2012 @ 09:04
    Sharon
    0

    Yeh, that would do it :)

  • James 6 posts 26 karma points
    Jan 18, 2012 @ 10:14
    James
    0

    Is it possible to get the pre values not using the data type id but name? The reason being is that we have numerous Umbraco databases and if the ID of the data type is out of sync it will not work having directly used the id.

  • Sharon 16 posts 37 karma points
    Jan 18, 2012 @ 10:54
    Sharon
    0

    I'm afraid not. At least that I know of.

    If you do find a way, please share. I have exactly the same situation.

     

    Cheers

  • Heather Floyd 605 posts 1004 karma points MVP 5x c-trib
    Feb 04, 2014 @ 19:24
    Heather Floyd
    0

    I found this code here, which allows you to specify the datatype name and get the ID:

    int DataTypeId = DataTypeDefinition.GetAll().First(dtDef => dtDef.Text.ToString() == "My Data Type").Id;
    

    Then you can use this in the iterator code:

    XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(DataTypeId);
    

    ~Heather

Please Sign in or register to post replies

Write your reply to:

Draft