Friday, September 5, 2014

Working with Sitecore Link Fields

General Link Field / General Link Search Field

Sitecore.Data.Fields.LinkField class is used to access the data template fields of type general link. 














                                                            

Access a link type field 

Sitecore.Data.Fields.LinkField lnkField = current.Fields ["General Link Field"];
        if (lnkField! = null)
        {
            if (lnkField.LinkType == "external" || lnkField.LinkType == "anchor" || lnkField.LinkType == "mailto" || lnkField.LinkType == "javascript")
            {
                HyperLink1.Text = lnkField.Text;
                HyperLink1.NavigateUrl = lnkField.Url;
            }
            if (lnkField.LinkType == "internal")
            {
                HyperLink1.Text = lnkField.Text;
                HyperLink1.NavigateUrl = Sitecore.Links.LinkManager.GetItemUrl (lnkField.TargetItem);
            }
            else if (lnkField.LinkType == "media")
            {
               Image1.ImageUrl = Sitecore.StringUtil.EnsurePrefix ('/',   Sitecore.Resources.Media.MediaManager.GetMediaUrl (lnkField.TargetItem));
          }
        }

Edit Link Field – External

using (new Sitecore.SecurityModel.SecurityDisabler ())
        {
            Database master = Sitecore.Configuration.Factory.GetDatabase ("master");
            Sitecore.Data.Items.Item current = master.GetItem (Sitecore.Context.Item.ID);
            current.Editing.BeginEdit ();
            Sitecore.Data.Fields.LinkField lnkField = current.Fields["General Link Field"];
            if (lnkField != null)
            {
                lnkField.LinkType = "external";
                lnkField.Url = "http://sitecore.com";
            }
            current.Editing.EndEdit ();
        }

Edit Link Field – Internal

using (new Sitecore.SecurityModel.SecurityDisabler ())
        {
            Database master = Sitecore.Configuration.Factory.GetDatabase ("master");
            Sitecore.Data.Items.Item current = master.GetItem (Sitecore.Context.Item.ID);
            Sitecore.Data.Items.Item editItem = master.GetItem ("/sitecore/content/Home/Page1");
            current.Editing.BeginEdit ();
            Sitecore.Data.Fields.LinkField lnkField = current.Fields ["General Link Field"];
            if (lnkField!= null)
            {
                lnkField.Clear ();
                lnkField.LinkType = "internal";
                Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions ();
                urlOptions.AlwaysIncludeServerUrl = false;
                lnkField.Url = Sitecore.Links.LinkManager.GetItemUrl (editItem, urlOptions);
                lnkField.TargetID = editItem.ID;
            }
            current.Editing.EndEdit ();
        }


Edit Link Field – Media

using (new Sitecore.SecurityModel.SecurityDisabler ())
        {
            Database master = Sitecore.Configuration.Factory.GetDatabase ("master");
            Sitecore.Data.Items.Item current = master.GetItem (Sitecore.Context.Item.ID);
            Sitecore.Data.Items.Item editItem = master.GetItem ("/sitecore/media library/Images/Translate");
            current.Editing.BeginEdit ();
            Sitecore.Data.Fields.LinkField lnkField = current.Fields ["General Link Field"];
            if (lnkField != null)
            {
                lnkField.Clear ();
                lnkField.LinkType = "media";
                lnkField.Url = editItem.Paths.MediaPath;
                lnkField.TargetID = editItem.ID;
            }
            current.Editing.EndEdit ();
        }



1 comment: