Surendra Sharma

Surendra Sharma

Search This Blog

Tuesday, October 13, 2015

How to create custom Sitecore config file and access its key’s value in C#



I am damn sure that you must sometimes face this problem during reading, finding or modifying any entry in Sitecore web.config file.

The config file that I have contains 3950 lines. WTF how to work with such a large file. Is there any way to break it so that I can make my own settings value in other config file.

Fortunetly YES. Here is the trick.

·         Create one config file like "Sample.config" in "Website\App_Config\Include" folder and make keys entries in <settings> </settings> section as

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>    
       <setting name="EnableLogin" value="true"/>
<setting name="mykeyName" value="KeyValue"/>
    </settings>
  </sitecore>
  <system.web>
  </system.web>
</configuration>

At runtime Sitecore merge all these config files and treat them as a single file and work smartly. 

And now the million dollar question, How to access it in C# code?

Simply refer GetSetting() method of Settings class as below

string strKeyValue = Sitecore.Configuration.Settings.GetSetting("mykeyName");
 
Please leave your comments or share this trick if it’s useful for you.

Saturday, September 12, 2015

How to implement 301 redirect with IIS URL rewrite module in ASP.NET



If 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 your client website is already running and you have developed it again with same or different technology.

Once the coding and testing of this new website is complete. What next?

Deploy on server. Make it LIVE.

So there may be some old URL or pages which should be redirected to new website pages.
Good example is if somebody bookmarked the old URL then instead of page not found it should be redirected to new pages.

In simple terms it’s called 301 redirect.

How to achieve 301 redirect without writing code in ASP.NET?

You must have URL Rewrite module in IIS. If you have not then download and install from http://www.iis.net/downloads/microsoft/url-rewrite

Now make entries in web.config between <system.webServer> … </system.webServer> tag by specifying old URL pages mapping with new pages as below.

Suppose you old page URL is http://www.example.com/old.php and you want to redirect it to new page as http://www.example.com/new.aspx. Finally your config file entry looks like



<rewrite>
  <rules>

      <rule name="Rewrite Rule1" stopProcessing="true">
          <match url="world/new-zealand.php" />
          <action type="Redirect" url="world/east/country1.aspx" />
      </rule>
      <rule name="Rewrite Rule2" stopProcessing="true">
          <match url="old.php" />
          <action type="Redirect" url="new.aspx" />
      </rule>
      <rule name="Rewrite Rule3" stopProcessing="true">
          <match url="oldfile.pdf" />
          <action type="Redirect" url="newfile.pdf" />
      </rule>
      <rule name="Rewrite Rule4" stopProcessing="true">
          <match url="policy.pdf" />
          <action type="Redirect" url="/" />
      </rule>
      <rule name="Rewrite Rule5" stopProcessing="true">
          <match url="world/bahamas.php" />
          <action type="Redirect" url="bahamasnew.aspx" />
      </rule>
  </rules>
</rewrite>  

Here we are redirecting from request for any php page, pdf file to index page, new aspx pages.
You can specify any numbers of pages here. But this is good for limited pages. My suggestion is that its limit should be upto 50 pages.

If you have more than 50 pages then write the custom redirecting code.

You can do all these activities from IIS as well and IIS will make entry in web.config for you.

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