Get Shared Fields

Get Shared Fields

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.StringUtil
  • Sitecore.TemplateIDs
  • System.Linq
  • System.String

Shared Fields C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item templateRoot = db.GetItem(Sitecore.ItemIDs.TemplateRoot);
if (templateRoot == null) return;
string templateRootPath = templateRoot.Paths.Path;

string query = System.String.Format(templateRootPath + "//*[@@templateid='{0}']", Sitecore.TemplateIDs.Template);
Sitecore.Data.Items.Item[] templates = db.SelectItems(query);
if (templates == null) return;

var sortedList = (from entry in templates orderby entry.Paths.Path ascending select entry);

System.Text.StringBuilder builder1 = new System.Text.StringBuilder();
System.Text.StringBuilder builder2 = new System.Text.StringBuilder();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

int i = 0, j = 0;
foreach (Sitecore.Data.Items.Item template in sortedList)
{
    Sitecore.Data.Items.TemplateItem templateItem = new Sitecore.Data.Items.TemplateItem(template);
    Sitecore.Data.Items.Item stndValues = templateItem.StandardValues;
    if (stndValues != null)
    {
        Sitecore.Collections.FieldCollection svFields = stndValues.Fields;
        if (svFields != null && svFields.Count > 0)
        {
foreach (Sitecore.Data.Fields.Field field in svFields)
{
    if (field.Shared)
    {
        if(i < 10)
        {
i++;
builder1.Append(template.Paths.Path + "&#58;&#160;<b>" + field.DisplayName + "</b><br/>");
        }
    }
    else
    {
        if (field.Unversioned)
        {
if (j < 10)
{
    j++;
    builder2.Append(template.Paths.Path + "&#58;&#160;<b>" + field.DisplayName + "</b><br/>");
}
        }
    }
}
        }
    }
    if (i >= 10 || j >= 10) break;
}
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "First ten data templates that contain shared fields");
output.RenderEndTag(); //P

if (i == 0)
    output.Write("{0}", "There are no templates that contain shared fields");
else
    output.Write("{0}", builder1.ToString());

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Filtered data templates that contain just unversioned fields");
output.RenderEndTag(); //P

if (j == 0)
    output.Write("{0}", "There are no templates that contain just unversioned fields");
else
    output.Write("{0}", builder2.ToString());

output.RenderEndTag(); //Div


Shared Fields C# Web Control Output

First ten data templates that contain shared fields

/sitecore/templates/Common/Folder: Editors
/sitecore/templates/Common/Folder: Insert Options
/sitecore/templates/Sample/Sample Item: Insert Options
/sitecore/templates/Sample/Sample Item: Default workflow
/sitecore/templates/Sample/Sample Item: Renderings
/sitecore/templates/System/Analytics/Campaign: Insert Options
/sitecore/templates/System/Analytics/Campaign: Default workflow
/sitecore/templates/System/Analytics/Campaign: Cost per Day
/sitecore/templates/System/Analytics/Campaign: Title
/sitecore/templates/System/Analytics/Campaign: Change Traffic Type

Filtered data templates that contain just unversioned fields

There are no templates that contain just unversioned fields