2

I am looking for the option to maintain external links in common location and refer through out the pages.

For example if my site uses link www.xyz.com/redirect in 100 pages , I will declare it in one master/common location and refer it to that 100 places. In future , If I change this URL to www.abc.com/redirect it must impact those 100 places. I shall not go and edit in 100 places.

Unfortunately, I could not find any thing as such.. Please let me know if it is possible as similar to Image library.

I have added the custom library class as attached in the screen shot, and my sitecore.config code added is

 <linkManager defaultProvider="custom"><providers>
      <add name="custom" type="Indivirtual.Shared.Utilities.CommonUtil.CustomLinkProvider,Indivirtual.Shared.Utilities.CommonUtil"  addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageLocation="filePath" shortenUrls="true" useDisplayName="false" lowercaseUrls="true" languageEmbedding="never"/>
    </providers>
  </linkManager>

getting the error as : Could not resolve type name: Indivirtual.Shared.Utilities.CommonUtil.CustomLinkProvider,Indivirtual.Shared.Utilities.CommonUtil (method: Sitecore.Configuration.Factory.CreateFromTypeName(XmlNode configNode, String[] parameters, Boolean assert)). Custom library class added as shown

2
  • Create a template with general link field and add the item of that template in global section and use that global item in all the places where you want that link. – Mahesh Raghupathi Mar 16 at 7:49
  • Do you have SXA installed? – Peter Procházka Mar 16 at 9:46
2

One solution that wouldn't require customizing any sitecore dialogs would be to create all the external links in a common folder so that the editors can select them from rich text editor or general links by using Insert Internal Link. For this you would create a new template that contains a Link field: enter image description here

This way you'll also know in the content editor where each of your external links are referenced:

enter image description here

The next step would be to create a custom link provider by overriding the GetItemUrl method and you would generate the links only for the external link items created in Global/External Links

namespace CustomLibrary.Links
{
   public class CustomLinkProvider : Sitecore.Links.LinkProvider
   {
      public override string GetItemUrl(Sitecore.Data.Items.Item item,
         Sitecore.Links.UrlOptions options)
      {
         if (item.TemplateName == "Link Item")
        {
            Sitecore.Data.Fields.LinkField linkField = item.Fields["Link"];

            if (linkField != null)
            {
                return linkField.Url;
            }
        }
         /*else return the default url generated by Sitecore*/
         return base.GetItemUrl(item, options);
      }
   }
}

The above code will work for versions until 9.3. In later versions you will have to create a CustomItemUrlBuilder class and override the Build method, please take a look at the examples posted https://cmsclub9.wordpress.com/2020/12/15/take-control-over-sitecore-links/ and https://adapty.com/blog/sitecore-9-3-custom-link-provider/

You can use this config to apply your custom provider and set the appropriate properties to match your current linkprovider settings:

<linkManager defaultProvider="custom">
   <providers>
      <add name="custom" type="CustomLibrary.Links.CustomLinkProvider, CustomLibrary"  addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageLocation="filePath" shortenUrls="true" useDisplayName="false" lowercaseUrls="true" languageEmbedding="never"/>
   </providers>
</linkManager>
14
  • Thanks @Cristi for the answer, I am using Sitecore.NET 8.1 (rev. 160519), I could not find an option to add external links. As per your reply , I understood it should be available in my version. Can you share the screen shot or steps to the external links under the Global folder ? – Mohd Ilyas Apr 5 at 4:59
  • The external links are basically simple sitecore items that contain a link field and the link provider will read the external link from that field. I updated the answer to include the Link template and also the code for the link provider. – Cristi Vulturar Apr 5 at 8:11
  • hi @Cristi thanks for clarification, I tried , but my breakpoint is not hitting the CustomLinkProvider class. Do I need to do any additional configuration – Mohd Ilyas Apr 12 at 8:05
  • Could you add some details on how you have patched the CustomLinkProvider in your config? – Cristi Vulturar Apr 12 at 9:40
  • my sitecore.config section will look like below after patching: – Mohd Ilyas Apr 12 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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