Skip to main content

Posts

Showing posts from April, 2017

Redirect restricted media items with ReturnURL

Sitecore can redirect the user to login page if the requested item is restricted for anonymous users. This can be done in the Site configuration. 

requireLogin="true"
loginPage="/redirectpage"

There are two differences in this redirection. For Sitecore item, it will be redirected with returnURL querystring with source url. For Media item, querystring is not added. 

In one of the project, we had to handle the redirection based on the source url for the Sitecore Media items. To achieve this, MediaRequestHandler can be modified and it will return a query parameter with source URL. Please make sure to update the <handler> with the new class and assembly name. 

        /// <summary>
        /// Performs the actual request processing.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The do process request.
        /// </returns>
        protected…

Sitecore generate dynamic URL

When we migrate a site from legacy platform to Sitecore, there may be cases where Sitecore Rich Text Editor will have links. These html content with links will have relative URL and it may break if we move the page to different folder in Sitecore. 

Sample HTML Content:
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five <a title="centuries" href="/about-us/centuries">centuries</a>, but also the leap into electronic typesetting, remaining essentially unchanged.</p>

In order to set the links with Dynamic URL, we need to read the HTML content (via HtmlaAgilityPack), find the links, generate the dynamic URL and save it. 

To generate the Dynamic URL, we can use the below method. 

For Sitecore Item:
LinkUrlOptions options = new LinkUrlOptions();
var dynamicUrl = LinkManager.GetDynamicUrl(item, options…

Get fallback language in code

With the latest Sitecore version, Language Fallback is a key feature for multilingual website. 

Sitecore does the job to get the fallback version and languages for us. 

If you want to get the Fallback Language object, one of the way is below.


            var fallbackLanguage = Sitecore.Data.Managers.LanguageFallbackManager.GetFallbackLanguage(Sitecore.Context.Language, Sitecore.Context.Database);