Sitecore 6 Link Management

For the current edition of this blog post, see:

http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/Link-Management-with-Sitecore.aspx

The Sitecore 6 Web Content Management System (WCM or CMS) provides new link management features to prevent broken links and images, which detract from the user experience and can affect search engine ranking.

A typical HTML Web page may consist of a number of references to other resources, which may or may not exist, possibly on a different Web server. These elements including anchor (<a>) elements that represent links and <img> elements that reference the URLs of images. In addition to anchor and image elements in content maintained by CMS users, presentation components such as Web controls generate links, images, and references to other resources dynamically, such as to provide data-driven navigation. Sitecore can only manage links in the CMS; developers are responsible for the references generated by the presentation components they develop.

Any of these elements can reference a resource that does not exist. Broken references in CMS content typically result from:

  • Moving, renaming, or deleting a Web resource.
  • Using relative paths instead of using absolute paths, such as when copying an HTML file to a parent directory.
  • A typo in the URL of a Web resource.

You can use validation to trap broken external links while the user edits content, or periodically generate a report of broken external links. The CMS can help to manage internal links when a user moves, renames, or deletes a resource in the CMS repository. When a user deletes a resource referenced by another resource, Sitecore prompts the user what to do with those references: break them, remove them, or update them to reference another resource. Sitecore automatically assigns each resource in the CMS repository both a friendly URL and a Global Unique Identifier (GUID). Resources reference other resources using GUIDs instead of paths whenever possible. When a user renames or moves a resource, no other data changes, as the GUID does not change. Sitecore does not need to prompt the user in this condition, as references by GUID do not break. Presentation components transform these GUID references into friendly URLs sent in the response to the Web client. For example, the raw value of a Rich Text Editor (RTE) field containing an image appears as follows:

<img src="/~/media/3C6C861508D44B899DE08DC9B84AFF13.ashx?w=200&h=230&as=1" width="200" height="230" alt=""/ >

The raw value of a RTE field containing a link to another content item appears as follows:

<a href="~/link.aspx?_id=0B16893D260041838FAD668AAA551E21&_z=z">Click here</a>

The raw value of an RTE field containing a link to a media item appears as follows:

<a href="~/media/87A923D5B58641FEB1949F9ED79E79C3.ashx">Click here</a>

XSL extension controls such as <sc:html>, <sc:link>, and <sc:image> automatically transform all GUID references to friendly paths. The sc:field() XSL extension method transforms GUID references to friendly paths, while the sc:fld() extension method returns the raw field value, which may include GUID references.

Sitecore .NET developers use various methods to transform GUID references to friendly URLs. The static method Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Data.Items.Item) returns the friendly URL of a content item.

For example, to retrieve the URL of the content item requested by the browser:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
string url = Sitecore.Links.LinkManager.GetItemUrl(item);

The static method Sitecore.Resources.Media.MediaManager.GetMediaUrl(Sitecore.Data.Items.MediaItem) returns the friendly URL of a media item.

For example, to generate a series of <img> elements based on the images selected in the TreelistEx field named FieldName:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
Sitecore.Data.Fields.MultilistField images = item.Fields["FieldName"];

foreach (Sitecore.Data.Items.Item pointer in images.GetItems())
{
  Sitecore.Data.Items.MediaItem media 
    = new Sitecore.Data.Items.MediaItem(item);
  string url = Sitecore.StringUtil.EnsurePrefix('/', 
    Sitecore.Resources.Media.MediaManager.GetMediaUrl(media));
}

Note that Sitecore does not include the leading slash character ("/"),resulting in URLs that can exceed browser, Web server, or operating system limits for path lengths. Always ensure media URLs sent to the browser begin with the slash character.

The static method Sitecore.Links.LinkManager.ExpandDynamicLinks(string) transforms GUID references in RTE field values to friendly URLs. For example, to transform all references in the field named text in the context item:

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
string before = item.Fields[FieldName].Value;
string after = Sitecore.Links.LinkManager.ExpandDynamicLinks(before);

To configure how Sitecore generates friendly URLs, edit the attributes of the /configuration/sitecore/linkManager element in web.config. The comments above this element document the purpose of each attribute. For example, to cause content item URLs for a multilingual site to always include the language code (/lang/page.aspx), set languageEmbedding to always. You could also configure this element to use a class other than the default Sitecore.Links.LinkProvider to generate URLs.

This entry was posted in Obsolete. Bookmark the permalink.

Leave a comment