Monday, October 10, 2011

Show Items structure in Sitecore

The following example demonstrates how to get the information about a node on the back side of a node by giving the item.ID. You also could do it easier by having the full path instead of creating an ID.
It is important to do all this inside a SecurityDisabler. You might have some nodes that are not accessible to anonymous user, if the code is running for without proper security.

    protected void StartToAnalyse(object sender, EventArgs e)    {
        Sitecore.Data.Database master =
            Sitecore.Configuration.Factory.GetDatabase("master");
        using (new Sitecore.SecurityModel.SecurityDisabler())
        {
             ProcessItem(master.GetItem(new Sitecore.Data.ID(textboxRoot.Text)));
        }
    }
    private void ShowTechnicalDetails(Sitecore.Data.Items.Item item)
    {
        string pageName = item.DisplayName;
        
        var template = item.Template;
        //LogLine(" Template : " + item.TemplateName);
        // LogLine(" Uri : " + item.Uri.Path);
        // string rend = Sitecore.Context.Item.Fields["Renderings"].Value;
        Sitecore.Data.Items.DeviceItem defDevice = Sitecore.Data.Items.DeviceItem.ResolveDevice(Sitecore.Configuration.Factory.GetDatabase("master"));
        var layout = item.Visualization.GetLayout(defDevice);
        if (layout != null)
        {
            LogLine(" Layout : <u> [" + layout.DisplayName + "]</u>" );
        //   //  LogLine(" File : " + layout.FilePath);
        }
        var renderings = item.Visualization.GetRenderings(defDevice, false);
        //LogLine("Renderings:<ul>");
        foreach(var rend in renderings)
        {
            //LogLine(" Layout : " + rend.RenderingItem.DisplayName);
            LogItem(pageName + ";" + rend.RenderingItem.InnerItem["Path"]);
        }
    }
    private void ProcessItem(Sitecore.Data.Items.Item item)
    {
        if (item == null)
        {
            LogLine("Item not found.");
            return;
        }
        
        ShowTechnicalDetails(item);
        foreach (Sitecore.Data.Items.Item child in item.Children)
        {
            ProcessItem(child);
        }
    }