2

We are getting results through Content Search API and getting the General Link field value into it. We would like to add an anchor link based on a general link field value. Do we have the default feature of Sitecore to create an anchor link in view or do we need to use custom code to create a link?

1 Answer 1

2

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();

You can also check the link type and perform the functionality as required:

 switch (generalLinkfield.LinkType.ToLower())
    {
      case "internal":        
        return generalLinkfield.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(generalLinkfield.TargetItem) : string.Empty;
      case "media":      
        return generalLinkfield.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(generalLinkfield.TargetItem) : string.Empty;
      case "external":      
        return generalLinkfield.Url;
      case "anchor":
        return !string.IsNullOrEmpty(generalLinkfield.Anchor) ? "#" + generalLinkfield.Anchor : string.Empty;
      case "mailto":
        return generalLinkfield.Url;
      case "javascript":
        return generalLinkfield.Url;
      default:
        return generalLinkfield.Url;
    }

for more details please refer Access general link fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.