Iterate over Umbraco getAllTagsInGroup result

978 views Asked by At

I'm trying to get a list of tags from a particular tag group in Umbraco (v4.0.2.1) using the following code:

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

What I want to do is just output a list of those tags. However, if I output the variable 'tags' it just outputs a list of all tags in a string. I want to split each tag onto a new line.

When I check the datatype of the 'tags' variable:

string tagType = tags.GetType().ToString();

...it outputs MS.Internal.Xml.XPath.XPathSelectionIterator.

So question is, how do I get the individual tags out of the 'tags' variable? How do I work with a variable of this data type? I can find examples of how to do it by loading an actual XML file, but I don't have an actual XML file - just the 'tags' variable to work with.

Thanks very much for any help!

EDIT1: I guess what I'm asking is, how do I loop through the nodes returned by an XPathSelectionIterator data type?

EDIT2: I've found this code, which almost does what I need:

        XPathDocument document = new XPathDocument("file.xml");
        XPathNavigator navigator = document.CreateNavigator();

        XPathNodeIterator nodes = navigator.Select("/tags/tag");
        nodes.MoveNext();
        XPathNavigator nodesNavigator = nodes.Current;

        XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

        while (nodesText.MoveNext())
            debugString += nodesText.Current.Value.ToString();

...but it expects the URL of an actual XML file to load into the first line. My XML file is essentially the 'tags' variable, not an actual XML file. So when I replace:

XPathDocument document = new XPathDocument("file.xml");

...with:

XPathDocument document = new XPathDocument(tags);

...it just errors.

2

There are 2 answers

8
Tomalak On BEST ANSWER

Since it is an Iterator, I would suggest you iterate it. ;-)

var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");

foreach (XPathNavigator tag in tags) {
  // handle current tag
}
0
Revolution42 On

I think this does the trick a little better.

The problem is that getAllTagsInGroup returns the container for all tags, you need to get its children.

foreach( var tag in umbraco.editorControls.tags.library.getAllTagsInGroup("category").Current.Select("/tags/tag") )
{
     /// Your Code
}