Get Broken Links

Get Broken Links

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Broken Links C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Links.LinkDatabase linkDb = Sitecore.Globals.LinkDatabase;
if (linkDb == null) return;

Sitecore.Links.ItemLink[] brokenLinks = linkDb.GetBrokenLinks(db);

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

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Broken Links Count: <b>" + brokenLinks.Length.ToString() + "</b><br/>");
output.RenderEndTag(); //P

if (brokenLinks == null)
    output.Write("{0}", "<b>There are no broken links</b><br/>");
else
{
    foreach (Sitecore.Links.ItemLink itemLink in brokenLinks)
    {
        if (itemLink != null)
        {
Sitecore.Data.Items.Item sourceItem = itemLink.GetSourceItem();
output.Write("{0}&#58;&#160;{1}", "Link Source", "<b>" + sourceItem.DisplayName + "</b><br/>");
Sitecore.Data.Items.Item targetItem = itemLink.GetTargetItem();
if (targetItem != null)
{
    output.Write("{0}&#58;&#160;{1}", "Link Target", "<b>" + targetItem.DisplayName + "</b><br/>");
}  
        }
    }
}
output.RenderEndTag(); //Div


Broken Links C# Web Control Output

Broken Links Count: 41

Link Source: FormID
Link Source: LinkedIn
Link Source: WebEdit
Link Source: Insert Rules
Link Source: Field Link
Link Source: Create an Account
Link Source: System
Link Source: __Standard Values
Link Source: User Profile
Link Source: Leave a message
Link Source: Show Parent Fields
Link Source: Test
Link Source: Test
Link Source: Get Fields
Link Source: Link Display
Link Source: Link Display
Link Source: Check Actions
Link Source: WebMarketing
Link Source: VideoPlayers
Link Source: Using FieldRenderer
Link Source: Using FieldRenderer
Link Source: Using FieldRenderer
Link Source: Save Actions
Link Source: Show Rich Text
Link Source: Show Rich Text
Link Source: Show Image Field
Link Source: Get Our Newsletter
Link Source: Sample
Link Source: __Standard Values
Link Source: Desktop Recording
Link Source: DesktopRecording
Link Source: CampaignTypes
Link Source: __Standard Values
Link Source: Theme
Link Source: Tell a Friend
Link Source: Color
Link Source: If Multivariate Testing Enabled
Link Source: SlideShare
Link Source: Demo form
Link Source: Demo form
Link Source: __Standard Values