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

No comments:

Post a Comment