Get Page Links

Get Page Links

Links

  • Fields that contain the ID of another item

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Page Links C# Web Control Code

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

Sitecore.Links.LinkDatabase linkDb = Sitecore.Globals.LinkDatabase;
int referenceCount = linkDb.GetReferenceCount(item);
Sitecore.Links.ItemLink[] references = linkDb.GetReferences(item);
if (references == null) return;

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);
output.Write("Item: " + item.DisplayName);
output.RenderEndTag(); //P

           
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}&#58;&#160;{1}", "Reference Count", "<b>" + referenceCount.ToString() + "</b>");
output.RenderEndTag(); //P

foreach (Sitecore.Links.ItemLink refLink in references)
    output.Write("{0}", "<b>" + refLink.GetTargetItem().DisplayName + "</b><br/>");

int referrerCount = linkDb.GetReferrerCount(item);
Sitecore.Links.ItemLink[] referrers = linkDb.GetReferrers(item);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}&#58;&#160;{1}", "Referrer Count", "<b>" + referrerCount.ToString() + "</b>");
output.RenderEndTag(); //P

foreach (Sitecore.Links.ItemLink referrerLink in referrers)
    output.Write("{0}", "<b>" + referrerLink.GetTargetItem().DisplayName + "</b><br/>");
output.RenderEndTag(); //Div


Page Links C# Web Control Output

Reference Count: 22

TIC-TacticalPage
ContentDisplay
Developers
CruiseShip3a
Focus
Function
Role
Persona
Score
Internet
Default
TICBrowserLayout
SLHeader
SLLeftContent
SideMenuSimple
Teasers
BottomFooter
Print
TICPrinterLayout
Content2Rows
TitleSubtitleOnly
InformationOnly

Referrer Count: 1

ContentDisplay

Sources