Create Link to Sitecore Item Programmatically using Link Manager

As a Sitecore developer, we would always need to Link to different Sitecore Items, Media Items. So How Does Sitecore handles the Internal and External Link and How do we resolve the correct URL Programmatically? This article explains on How to Create Link to Sitecore Item Programmatically using Link Manager

Create Link to Sitecore Item Programmatically using Link Manager and MediaManager

Create a Link to Sitecore Item

By Using LinkManager we will be able to resolve a URL to Sitecore Item. The First step is to get the Sitecore item and Pass it to the Method ResolveSitecoreURL(item) and pass the item to it.

How to Get Absolute URL of an Item in Sitecore

If you need the Absolute URL or a fully qualified URL including the domain name then Sitecore LinkManager provides overloading of URLOptions.

Change the Default Setting for all the Link in Config

You could control the behaviour of resolving the Links in Sitecore by making few changes in the web.config file. Change the alwaysIncludeServerUrl value to true if you need the absolute URL and make it false if you need only relative URL.

Create a Link to Sitecore Media Item

You will not be able to fetch the Media Item using the above LinkManager.GetItemUrl() Method. Sitecore has a separate API to fetch the Media URLs.

Like LinkManager had the URLOptions MediaManager comes with a MediaUrlOptions where you can control the absolute and relative URLs.

Working with Sitecore LinkFields

If you are working with the LinkField then you could create the function as shown below and resolve the correct URLs automatically.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *