10 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. 
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. 

  protected override bool DoProcessRequest(HttpContext context) {
	Assert.ArgumentNotNull(context, "context");
	MediaRequest mediaRequest = MediaManager.ParseMediaRequest(context.Request);
	if (mediaRequest == null) {
		return false;
	Media media = MediaManager.GetMedia(mediaRequest.MediaUri);
	if (media == null) {
		using(new SecurityDisabler()) {
			media = MediaManager.GetMedia(mediaRequest.MediaUri);
		string text;
		if (media == null) {
			text = Sitecore.Configuration.Settings.ItemNotFoundUrl;
		else {
			Assert.IsNotNull(Context.Site, "site");
			text = ((Context.Site.LoginPage != string.Empty) 
                        ? Context.Site.LoginPage: Sitecore.Configuration.Settings.NoAccessUrl);
		//Setup redirect url - Start
		UrlString urlString = new UrlString(text);
		if (string.IsNullOrEmpty(urlString["returnUrl"])) {
			urlString["returnUrl"] = WebUtil.GetRawUrl();
		//Setup redirect url - End
		WebUtil.Redirect(urlString.ToString(), false);

		return true;
	return this.DoProcessRequest(context, mediaRequest, media);
In the web.config, <handler> tag has to be updated with new class and assembly name. 
<add verb="*" path="sitecore_media.ashx" type="CustomHandler.MediaRequestHandler, CustomHandler" 
name="Sitecore.MediaRequestHandler" />

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. 

Let's say we have the below HTML content. 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.
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 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>
To generate the dynamic url, we can use the below method. These dynamic URLs will be rendered with friendly URL by Sitecore. 

For Sitecore Item:
LinkUrlOptions options = new LinkUrlOptions();
var dynamicUrl = LinkManager.GetDynamicUrl(item, options);
For Media Item:
var dynamicMediaUrl = MediaManager.GetMediaUrl(item, MediaUrlOptions.GetShellOptions());
<a title="centuries" href="~/link.aspx?_id=33B86674A94E4F2BA8B4576E606AFB45&amp;_z=z">centuries</a>
<a title="Image" href="-/media/dba85a49980e4e33916acbf798ce21fc.ashx">Image</a>

03 April, 2017

Sitecore - Get fallback language in code

Language Fallback in Sitecore 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(
blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }