Surendra Sharma

Surendra Sharma

Search This Blog

Sunday, March 29, 2015

How to specify location of images in aspx page for Sitecore

I was clueless while displaying search image in all the aspx pages and ascx web control of Sitecore. As on some pages I was getting my image but while on some pages I was not missing the image.

I tried different path as below but all was wrong

<img src="../images/search.png" alt="search image" />
<img src="~/images/search.png" alt="search image" />
<img src="images/search.png" alt="search image" />

Correct one is to use path that starts with “/” as it automatically resolve its path as below

<img src="/images/search.png" alt="search image" />

Final note is - Never specify "../" for any image as for any web request IIS resolve it accordingly. So always write root path as "/" + physical image path.

Hope it helps someone in anyway.


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

Saturday, March 28, 2015

How to repeat particular string or char into certain number of times

I was writing a program for showing a tree structure in textbox. I was trying to concatenate tabs and spaces in string. Soon I realize that it’s a very tedious way to achieve the result. I thought that .NET is the Ocean and there must be some way to repeat the particular string or character certain number of times. For example if I have character ‘0’ and I want to display it 5 times on screen. 

How to do it?

FOR and FOR EACH Loops are common, but I need some more smart way do it.
Here are some ways that I found

int count = 5;
string str = "PEACE ";
char c = '0';

Console.WriteLine(String.Empty.PadLeft(count, c));     //Output- 00000

string result = new String(c, count);
Console.WriteLine(result); //Output- 00000

Console.WriteLine(new StringBuilder().Insert(0, str, count).ToString());   //Output- PEACE PEACE PEACE PEACE PEACE

First two only repeat characters while last way is to repeat string. Hope this help someone.

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

Friday, March 27, 2015

How to include or put or embed curly braces in string.Format in C#

I was writing a HTML style within my repeater from code behind page as below

string s = string.Format("<style>.splashImg{0} { background-image:url({1}  );}</style>", 3, " myimage.jpg");

So that my final output should be like

<style>.splashImg3{ background-image:url(myimage.jpg);} </style>

And OMG I received error “Unrecognized escape sequence”. My first reaction was WTF what’s wrong with this code line. After careful look I come to know that it’s a problem of curly braces “{ }“.

As we are specifying curly braces for placeholder like {0} {1}, compiler is confused with other braces like “splashImg3{“.

So simplest solution is just write double braces for non-placeholder braces like “{{“ as shown in yellow color. So final code looks like

string s = string.Format("<style>.splashImg{0} {{ background-image:url( {1}  );}}</style>", 2, "myimage.jpg");


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

Thursday, March 19, 2015

How to apply workflow to all the items of content tree in Sitecore

Its common scenario that, if you created items first and later on creating workflow then you have to manually apply those workflow on template’s standard values and content items.

If there are few items then it fine to do it  manually. But what if there are hundreds, thousands content item.

Best way is to apply them programmatically.

Here is a code to do it. Just put Home item ID or start item

var FirstItem = Sitecore.Context.Database.GetItem(new ID("{A00A11B6-E6DB-45AB-8B54-636FEC3B5523}")) ;
PrintName(FirstItem , 1);

Rest of the item should be iterate by following recursive function and apply workflow on all the items except folder item.

private Item PrintName(Item mainItem, int icounter)
{
    string f = new string('-', icounter);
    TextBox1.Text += f + mainItem.DisplayName + System.Environment.NewLine;

    try
    {
        if (!mainItem.DisplayName.Equals("Homepage"))
        {
            //Put Workflow ID here
            var workflow = Factory.GetDatabase("master").WorkflowProvider.GetWorkflow("{7E9BC450-18EE-401E-892A-1CEF27BF8D9B}");
            workflow.Start(mainItem);
        }
    }
    catch (Exception)
    {
        TextBox1.Text += "Error " + mainItem.DisplayName + System.Environment.NewLine;
    }

    if (mainItem.HasChildren)
    {
        icounter++;
        foreach (var myInnerItem in mainItem.Children.ToList())
        {
            PrintName(myInnerItem, icounter);
        }
    }

    icounter--;
    return null;
}

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

Wednesday, March 18, 2015

How to remove .aspx extension from URL in Sitecore website

Sometimes client requirement is to remove extension like .aspx from their website URL. It’s a good practice as all search engine provide better result without extension.

How to do it in Sitecore with .NET?

Sitecore is smartly provide this facility by changing just one entry in web.config file. If you want to remove extension then keep addAspxExtension="false" from <linkManager defaultProvider="sitecore"> section in web.config file as highlighted below

    <linkManager defaultProvider="sitecore">
      <providers>
        <clear/>
        <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="asNeeded" languageLocation="filePath" lowercaseUrls="true" shortenUrls="true" useDisplayName="false"/>
      </providers>
    </linkManager>

Woooowww. No programming required J

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

Monday, March 16, 2015

How to programmatically iterate all the items of content tree in Sitecore

Do you want to iterate through all the items of Content tree in Sitecore using code?

Here is a way to do it. Just put Home item ID or any start item ID

var FirstItem = Sitecore.Context.Database.GetItem(new ID("{A00A11B6-E6DB-45AB-8B54-636FEC3B5523}")) ;
PrintName(FirstItem , 1);

Rest of the items should be iterate by following recursive function

private Item PrintName(Item mainItem, int icounter)
{
    string f = new string('-', icounter);
    TextBox1.Text += f + mainItem.DisplayName + System.Environment.NewLine;

    if (mainItem.HasChildren)
    {
        icounter++;
        foreach (var myInnerItem in mainItem.Children.ToList())
        {
            PrintName(myInnerItem, icounter);
        }
    }

    icounter--;
    return null;
}


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

Sunday, March 8, 2015

Dynamic Binding and Static Binding in Sitecore

You can present you data in Sitecore layouts in two ways by using binding. There are two types of binding technics in Sitecore

·         Dynamic Binding
·         Static Binding

If you are binding anything in placeholder from Sitecore then it’s called Dynamic Binding. Here you need to map placeholder and control from Sitecore Layout details section. For example below 3 placeholders are example of dynamic binding.

<sc:placeholder runat="server" key="HeadSection" />
<sc:placeholder runat="server" key="MidSection" />
<sc:placeholder runat="server" key="TailSection" />

If you are specifying sublayout of controls then its called Static binding. Here you don't need to map placeholder and control from Sitecore Layout details section. For example below 3 placeholders are example of static binding.   

<sc:Sublayout ID="scHeadSection" Path="~/Common/HeadSection.ascx" runat="server" />
<sc:Sublayout ID="scMidSection" Path="~/Common/MidSection.ascx" runat="server" />
<sc:Sublayout ID="scTailSection" Path="~/Common/TailSection.ascx" runat="server" />


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

Saturday, March 7, 2015

You should purchase the right of using the "UseLocalMTA" setting first

If you are receiving Sitecore ECM (E-mail Campaign Manager ) error message You should purchase the right of using the "UseLocalMTA" setting first , the here is a way to understand its meaning with suggested trick to fix it.

ECM offers two MTA mode – Sitecore MTA and custom MTA.

I suppose Sitecore MTA is using Sitecore App center which is based on pricing model.

But in development and testing environment, we only need Custom MTA which is free and you can test in your local environment.

You can configure ECM to use your local mail server by changing a setting in ECM's config file "Sitecore.EmailCampaign.config". 

<setting name="UseLocalMTA" value="true" />

If your license file does not have the "Sitecore.AnyMTA" permission, the value of the "UseLocalMTA" setting will be considered as “False” even though you set it to “True”.

You can check the license file for “Sitecore.AnyMTA” key by explicitly opening the license.XML file Internet Explorer browser. As an alternative, you can log in to Sitecore Content Editor, click the Sitecore button on the top left corner, and select Licenses option.

If "Sitecore.AnyMTA" key is not present in your license - I suggest you contact your Sitecore sales representative to get the one that includes the required key.

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

How to find any word in string using Jquery

This is simple but very useful method in Jquery to find any word or presence of any substring in any string.

if ($('#txtSearch').val().indexOf('?') < 0)

Here if “?” is present in textbox value then indexIf() method return the position of word in string otherwise return -1 in case of not present.


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

Friday, March 6, 2015

How to access the current URL using JavaScript

Many times we need to access current URL of browser using Javascript. Suppose if there is URL like http://www.example.com/guest/index.aspx , now you can access this URL in different pieces as below

alert(window.location.protocol); // display "http:"
alert(window.location.host); // display “www.example.com"
alert(window.location.pathname); // display "guest/example.aspx"

You can get the complete and full URL by concatenating all these different parts

var currentURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;


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

Thursday, March 5, 2015

The Sitecore App Center endpoint is not set.

If you are working with Sitecore ECM (Email Campaign Manager) and getting error The Sitecore App Center endpoint is not set. Then follow below trick to solve it.

Open page http://<SitecoreInstanceName>/sitecore/admin/SetSACEndpoint.aspx and set https://apps.sitecore.net/ in Endpoint URI textbox which is nothing but Sitecore App Center endpoint and save it.

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

Tuesday, March 3, 2015

URL and query string http%3A%2F%2F converting into http%253A%252F%252

If you have encoded your URL and query string and still its not pointing to right direction i.e. http%3A%2F%2F resolving into http%253A%252F%252 then simple reason is that % sign in “http%3A” is encoding into %25 as hexa value of % is 0x25.

The solution to resolve it is don't encode your URL in this case. Directly pass URL like https://www.mysit.com


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