Get Link Refs

Get Link Refs

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Link Refs C# Web Control Code

 Sitecore.Data.Items.Item item = Sitecore.Context.Item.Parent;
if (item == null) return;

Sitecore.Links.LinkDatabase linkDb = Sitecore.Globals.LinkDatabase;
Sitecore.Links.ItemLink[] references = linkDb.GetReferences(item);
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();

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}", "Parent Item", item.DisplayName);
output.RenderEndTag(); //P

if (references != null)
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    output.Write("{0}", "Link References");
    output.RenderEndTag(); //P
    foreach (Sitecore.Links.ItemLink link in references)
    {
        //Sitecore.Data.Items.Item linkedItem = db.Items[link.SourceItemID];
        output.Write("{0}&#58;&#160;{1}", "Target Item", "<b>" + link.GetTargetItem().DisplayName + "</b><br/>");
    }
}
else
    output.Write("{0}", "<b>This item does not contain any link references</b>");

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)
    {
        output.Write("{0}&#58;&#160;{1}", "Source Item", "<b>" + link.GetSourceItem().DisplayName + "</b><br/>");
    }
}
else
    output.Write("{0}", "<b>This item does not contain any link referrers</b>");

output.RenderEndTag(); //Div


Link Refs C# Web Control Output

Parent Item: Item Management

Link References

Target Item: ItemManagement
Target Item: Developers
Target Item: BeachScene31
Target Item: Focus
Target Item: Function
Target Item: Role
Target Item: Persona
Target Item: Score
Target Item: Default
Target Item: TICBrowserLayout
Target Item: SLHeader
Target Item: ShareThis
Target Item: SLLeftContentRight
Target Item: SideMenuSimple
Target Item: Snippets
Target Item: Legend
Target Item: BottomFooter
Target Item: Print
Target Item: TICPrinterLayout
Target Item: Content2Rows
Target Item: TitleSubtitleOnly
Target Item: Snippets
Target Item: TIC-PermBuilderPage

Link Referrers

Source Item: Item Management