Surendra Sharma

Surendra Sharma

Search This Blog

Thursday, April 30, 2015

How to add HTML page in Sitecore?

My client requested to add HTML page in Sitecore project and link it to one of the item in footer. I asked to myself WTF, what kind of requirement is this? How to do this?

But finally I completed this. Here are the quick steps.

1. Add HTML page in any folder of your .NET project. Let’s say it is "/HTMLPages/faq.htm"
2. Add complete path of this HTML page in “IgnoreUrlPrefixes“ in web.config file so that Sitecore should not process this page request and ignore it. Let’s ASP.NET should serve this page request without using Sitecore.

<setting name="IgnoreUrlPrefixes" value="/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/HTMLPages/faq.htm"/>

3.    Specify this page in General Link field of Sitecore item. But the problem is it’s neither the internal link nor the external link. So how to provide the link so that it should take protocol and domain name automatically.

Here I use the Insert JavaScript option of General Link field as

javascript:window.open(window.location.protocol + "//" + window.location.host   + '/HTMLPages/faq.htm');

Here is screenshot of the General link field setting



Now when it renders, the item open this page in new window and take this page as a part of current website.

Sometimes client requirement plays a major role to improve our skills.

Please leave your comments or share this trick if it’s useful for you.

Friday, April 24, 2015

How to create Page Not Found in Sitecore

In every Sitecore application, if end user type some page name that is not exist then we need to redirect user to some custom page which inform user that requested page is not available.

Here are the steps for it.

·         Create template, layout and content item for Page not found.
·         Write below code in your project

namespace MyProject.CodeFiles
{
    /// <summary>
    /// 404 class
    /// </summary>
    public class Page404Resolver : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
    {
        public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
        {
            if (Sitecore.Context.Item != null || Sitecore.Context.Site == null || Sitecore.Context.Database == null)
            {
                return;
            }

            // If current item not available in Sitecore, then
            if (Sitecore.Context.Item == null)
            {
                Sitecore.Diagnostics.Log.Info("Page404Resolver=" + System.Web.HttpContext.Current.Request.Url, this);

                // Find an error-page item and set it to context Item
                Item item404 = args.GetItem(new ID("{XXXXX1D2-8CDB-4461-93CE-501905DDSSSS}"));
                if (item404 != null)
                {
                    Sitecore.Context.Item = item404;
                    Sitecore.Context.Items["is404"] = "true";
                    Sitecore.Diagnostics.Log.Info("CUSTOM ERROR HANDLER: 404 :  Item is not null - Setting Context item to 404 page", this);
                }
            }
        }
    }
}


·         Open web.config file and add this class just below ItemResolver line of pipeline section

<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="MyProject.Web.Present.CodeFiles.Page404Resolver, MyProject.Web.UI"/>

·         Implement below code in page load section of File Not Found page. Keep response status as 404 and also set status of response.

    public partial class NotFound : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                if (Sitecore.Context.Items["is404"].Equals("true"))
                {
                    try
                    {
                        Response.StatusCode = 404;
                        Response.TrySkipIisCustomErrors = true;
                        Response.StatusDescription = "File not found";
                        //Response.End();
                    }
                    catch (Exception ex)
                    {
                        // Log error
                    }
                }
            }
        }

Please leave your comments or share this code if it’s useful for you.

Thursday, April 23, 2015

How to create error page in Sitecore


In every Sitecore application, you have to create error page. Error can be occurred at any time anywhere in application [We developers are always right. Errors are always due to incorrect contents entered by content editor J].

Anyway here are the steps to implement it

·         Create template, layout and content item for error page. I am creating only two fields in template here – Error Title, Error Message
·         On error page, I am displaying information of both these fields entered by content tree.
·         Generally I am creating single layout for all the contents. So I implemented the Page_Error event to capture all the error generated at layout page level.
·         Here is code

/// <summary>
/// page-level exception handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Page_Error(object sender, EventArgs e)
{
    // retrieve the last exception
    Exception exception = HttpContext.Current.Server.GetLastError();

    // handle any exception
    if (exception != null)
    {
        string error = exception.ToString();
        HttpContext.Current.Server.ClearError();
        ErrorLog(error);
    }
}

/// <summary>
/// Log and redirect to error page
/// </summary>
/// <param name="error"></param>
private void ErrorLog(string error)
{
    // log and clear the exception
    Sitecore.Diagnostics.Log.Error(error, this);
    // Pass the error on to the Generic Error page   
    string errorPageURL = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Database.Items.GetItem(new ID("{SSSS778C-JKHJ-SKSK-A01C-411E2028KKKK}")));
    Response.Redirect(errorPageURL);
}


Please leave your comments or share this code if it’s useful for you.