Monday 22 October 2012

Get list of publications from Tridion sorted using Linq


Sometimes you need to get a list of child publications in to a dropdown etc, this is an extension to the code from http://stringwriter.com/tag/tridion/



//Call like this  GetChildPublications("tcm:0-5-1");     
private string GetChildPublications(string parentTCM)
    {
        TDSE tdse = new TDSE();
        tdse.Initialize();

        //gets current publication object
        Publication pub = tdse.GetPublication(parentTCM);
        //gets row filter object
        ListRowFilter filter = tdse.CreateListRowFilter();
        //filter only publication type
        filter.SetCondition("ItemType", 1);
        //returns using publications as xml

        string temp = "";

        XDocument xmlPublications = XDocument.Parse(pub.Info.GetListUsingItems(ListColumnFilter.XMLListIDAndTitle, filter));
        XmlNamespaceManager NS = new XmlNamespaceManager(new NameTable());
        NS.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");

        ListItem li;
        foreach (XElement PubNode in xmlPublications.XPathSelectElements("/tcm:ListUsingItems/tcm:Item", NS).OrderBy(s => (string)s.Attribute("Title")))
        {
            li = new ListItem();
            li.Text = PubNode.Attribute("Title").Value;
            li.Value = PubNode.Attribute("ID").Value;
            GlobalPublicationID.Items.Add(li);
        }

        return temp;
    }

No comments:

Post a Comment