Monday 22 October 2012

How to get the contents of a field out of a component on the page in Tridion

This will allow you to pass in a Page TCM, and get back the summary from the first component based on the General schema, if not it will return a default string

This also limits to 
  • Pages ending with .aspx
  • not in a Structure group of "so"
  • not in a structure group of "so2010"
  • Published to the Publish target specified





int Days = Convert.ToInt32(ConfigurationManager.AppSettings["Days"]);
string CMSUrl = Convert.ToString(ConfigurationManager.AppSettings["CMSUrl"]);
string GlobalSiteURL = Convert.ToString(ConfigurationManager.AppSettings["GlobalSiteURL"]);
int GlobalPublicationID = Convert.ToInt32(ConfigurationManager.AppSettings["GlobalPublicationID"]);
string PublishTarget = Convert.ToString(ConfigurationManager.AppSettings["PublishTarget"]); 

private string GetPageSummary(string Pagetcm)
    {
        TDSE tdse = new TDSE();
        tdse.Initialize();

        Tridion.ContentManager.Interop.TDS.Page page = (Tridion.ContentManager.Interop.TDS.Page)tdse.GetObject(Pagetcm, EnumOpenMode.OpenModeView, "tcm:0-0-0", XMLReadFilter.XMLReadAll);
        if (!page.Info.PublishLocationUrl.Contains("/so/") && !page.Info.PublishLocationUrl.Contains("/so2010/") && page.Info.PublishLocationUrl.EndsWith(".aspx") && (page.IsPublishedTo(PublishTarget)))
        {
            string summary = "";
            if (page.ComponentPresentations.Count > 0)
            {
                foreach (ComponentPresentation cp in page.ComponentPresentations)
                {
                    if (cp.Component.Schema.Title == "General")
                    {
                        summary += "<div style=\"background-color:#EAEAAE;padding:5px;\">" + cp.Component.Fields["summary"].value[1].ToString() + "</div>";
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(summary))
            {
                summary += "No Summary available<br>";
            }
            summary += "<a href = \"" + CMSUrl + "Default.asp?URI=" + Pagetcm + "&CONTEXTURI=&FILTER=Data&ITEMTYPE=64&MODE=OpenModeEditWithFallback\" target = \"blank\">Edit page in Tridion</a>";
            summary += "<br><a href = \"" + GlobalSiteURL + page.Info.PublishLocationUrl + "\" target = \"blank\">View Page on website</a><br>";

            return summary;
        }
        return string.Empty;
    }

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;
    }

Thursday 22 March 2012

Tridion Loop through multiple embedded fields


Schema


Embeddable Schema

Field1 and Field2 both optional and multi value.



Component




Component Template
[%
For Each value in Component.Fields.Item("EmbeddedFields").Value
                For Each value2 in value.Item("Field1").Value
                                WriteOut "Field1: " & value2 & "<br>"
                Next
                For Each value2 in value.Item("Field2").Value
                                WriteOut "Field2: " & value2 & "<br>"
                Next
Next
%]


The outcome