Access general link fields

Abstract

How to access general link fields using the Sitecore.Data.Fields.LinkField class.

You can use the Sitecore.Data.Fields.LinkField class to access data template fields of type General Link. Depending on the type of link in the field, you can use the following properties of the Sitecore.Data.Fields.LinkField class:

Property

Value

Anchor

The name attribute of the HTML <a> element, without the leading hash character (“#”).

Class

The class attribute of the HTML <a> element.

IsInternal

True for an internal link, False for media and other types of links.

IsMediaLink

True for media links.

LinkType

A token identifying the type of link (internal, media, external, mailto, anchor, or javascript).

QueryString

Query string parameters to add to the URL.

Target

The target attribute of the HTML <a> element.

TargetID

The ID of item specified d by internal or media link.

TargetItem

The Sitecore.Data.Items.Item specified by an internal or media link.

Text

The text content of the HTML <a> element.

Title

The title attribute of the HTML <a> element.

Url

The URL of the link, except for media items, for which the Url property contains the path to the media item relative to /Sitecore/Media Library.

To determine the URL in the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");
Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

string url = String.Empty;

switch(linkField.LinkType)
{
    case "internal":
    case "external":
    case "mailto":
    case "anchor":
    case "javascript":
        url = linkField.Url;
        break;
    case "media":
        Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaIte(linkField.TargetItem);
        url = Sitecore.StringUtil.EnsurePrefix('/',
        Sitecore.Resources.Media.MediaManager.GetMediaUrl(media));
        break;
    case "":
        break;
    default:
        string message = String.Format("{0} : Unknown link type {1} in {2}", this.GetType(), linkField.LinkType, home.Paths.FullPath); Sitecore.Diagnostics.Log.Error(message,this);
    break;
}

You can use the Sitecore.Data.Fields.LinkField.Clear method to remove the data from a field of type General Link. For example, to remove any link from the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

home.Editing.BeginEdit();
linkField.Clear();
home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to the /Sitecore/Content/Home/Sample item:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

Sitecore.Data.Items.Item sample = master.GetItem("/sitecore/content/home/sample");

home.Editing.BeginEdit();

linkField.Clear();

linkField.LinkType = "internal";

Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();

urlOptions.AlwaysIncludeServerUrl = false;

linkField.Url = Sitecore.Links.LinkManager.GetItemUrl(sample,urlOptions);

linkField.TargetID = sample.ID;

home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to the /Sitecore/Media Library/Files/Sample media item:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

Sitecore.Data.Items.Item sample = master.GetItem("/sitecore/media library/files/sample");

home.Editing.BeginEdit();

linkField.Clear();

linkField.LinkType = "media";

linkField.Url = sample.Paths.MediaPath;

linkField.TargetID = sample.ID;

home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to the external URL http://sitecore.net:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

home.Editing.BeginEdit();

linkField.Clear();

linkField.LinkType = "external";

linkField.Url = "http://sitecore.net";

home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to the anchor namedAnchor:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

home.Editing.BeginEdit();

linkField.Clear();

linkField.LinkType = "anchor";

linkField.Url = "namedAnchor";

home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to the email address email@domain.tld:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

home.Editing.BeginEdit();

linkField.Clear();

linkField.LinkType = "mailto";

linkField.Url = "mailto:email@domain.tld";

home.Editing.EndEdit();

To update the General Link field named GeneralLinkField in the /Sitecore/Content/Home item in the Master database to a JavaScript function:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.LinkField linkField = home.Fields["generallinkfield"];

home.Editing.BeginEdit();

linkField.Clear();

linkField.Text = "";

linkField.LinkType = "javascript";

linkField.Url = @"javascript:alert('javascript')";

home.Editing.EndEdit();