1

I am having issues when trying to get the pageMeta from the page directly. Below is my sample code. can you please advice me if i am doing it wrong.

Error Message: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at IPageMeta pageMeta = (IPageMeta)result[0];

public partial class ABCuserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Use PageMetaFactory and retrieve the metadata of the page located at /us/potential-at-work/application-leaders/article.aspx
        string pubID = "tcm:0-12-1";
        string url = HttpContext.Current.Request.Url.AbsoluteUri;

        PageMetaFactory pageMetaFactory = new PageMetaFactory(pubID);      

        ComponentPresentationFactory cpFactory = new ComponentPresentationFactory(pubID);
        ComponentMetaFactory compMetaFactory = new ComponentMetaFactory(pubID);

        IList result = pageMetaFactory.GetMetaByUrl(url);
        IPageMeta pageMeta = (IPageMeta)result[0];

        //Get component from Page metadata
        foreach (ComponentPresentationMeta cpmeta in pageMeta.ComponentPresentationMeta)
        {
            ComponentPresentation dcp = cpFactory.GetComponentPresentationWithHighestPriority(cpmeta.ComponentId); //PaW Article Detail CT was set with the High priority
            //Get the custom metadata from the Article component in this CP
            if (dcp != null)
            {
                IComponentMeta compMeta = compMetaFactory.GetMeta(dcp.ComponentId);
                CustomMeta compCustomMeta = compMeta.CustomMeta;

                if (compCustomMeta != null)
                {
                    //Looping through to display the list of all custom meta fields
                    foreach (DictionaryEntry item in compCustomMeta.NameValues)
                    {
                        Response.Write(string.Format("{0}: {1} ({2})<br/>", item.Key, ((NameValuePair)item.Value).Value, ((NameValuePair)item.Value).Name));
                    }
                    //Get just one field
                    Response.Write(compCustomMeta.GetValue("edition").ToString());
                }
            }
        }

    }
}
Vandana
  • 11
  • 1
  • I'd recommend moving your question to http://tridion.stackexchange.com – johnwinter Nov 27 '13 at 00:04
  • When you post it on tridion.stackexchange.com, you can simplify the problem quite a bit by removing everything from the `foreach` down. The error message already says that it fails on `IPageMeta pageMeta = (IPageMeta)result[0];` – Frank van Puffelen Nov 27 '13 at 22:36

0 Answers0