Get Referrers

Get Referrers

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database db
  • Sitecore.Data.Items.Item
  • Sitecore.Globals
  • Sitecore.Links.LinkDatabase
  • Sitecore.Links.ItemLink

Referrers C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Links.LinkDatabase linkDb = Sitecore.Globals.LinkDatabase;
Sitecore.Links.ItemLink[] referrers = linkDb.GetReferrers(item);

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(); //Div

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

if (referrers != null)
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    output.Write("{0}", "Link Referrers");
    output.RenderEndTag(); //P
    foreach (Sitecore.Links.ItemLink link in referrers)
    {
        Sitecore.Data.Items.Item linkedItem = db.Items[link.SourceItemID];
        output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + linkedItem.Name + "</b><br/>");
        output.Write("{0}&#58;&#160;{1}", "Display Name", "<b>" + linkedItem.DisplayName + "</b><br/>");
        output.Write("{0}&#58;&#160;{1}", "Template Name", "<b>" + linkedItem.TemplateName + "</b><br/>");
        output.Write("{0}&#58;&#160;{1}", "Path", "<b>" + linkedItem.Paths.Path + "</b><br/>");
        output.Write("{0}&#58;&#160;{1}", "URI", "<b>" + linkedItem.Uri + "</b>");
        output.Write("<hr/>");
    }
}
else
    output.Write("{0}", "<b>This item does not contain any inbound link referrers</b>");

output.RenderEndTag(); //Div

C# Web Control Real-time Output

Item: Get Referrers

Link Referrers