Saturday, June 29, 2013

Sitecore How-To: Sitecore Country and Region Dropdowns

We had a couple people in the Guild ask about country and region DropDownLists so thought Dennis Augustine and I thought we would write an article on it.  

Problem: In our application we require a country and region picker.  When someone chooses a company we need to filter the region picker.

Solution: This can be accomplished by adding two DropDownList controls to your SubLayout. Call one ddlCountry and the other ddlRegion.  

In the content tree under a shared lookup area create a folder called "Country"  under this folder add the countries with key as country code and phrase as country name.  Next under each country create a folder called "Regions" store all the regions in this folder.  I use DictionaryEntry as my Sitecore Data Template for the country and regions.

On Page_load you would call GetCountries() and assign it to the ddlCountries.DataSource Property.

    protected override void OnLoad(EventArgs e)
    {
       ddlCountry.DataSource = GetCountries(); 
       ddlCountry.DataValueField = "Key"; 
       ddlCountry.DataTextField = "Phrase"; 
       ddlCountry.DataBind(); 
   }

Next trap the change in country using the following event:

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
            ddlRegion.DataSource = GetRegions(ddlCountry.SelectedValue); 
            ddlRegion.DataValueField = "Key"; 
            ddlRegion.DataTextField = "Phrase";  
           ddlRegion.DataBind(); 
    }

Next add the following code to the sublayout. Ideally later you would store these in a library for use late in other parts of your code. Also note that you would need to change "SiteSettings.LookupPath" to the path where you are storing your countries in the tree.  This setting occurs a couple times in my code below.

///<summary> 
/// Getting children items by parent item path 
///</summary> 
/// <param name="itemPath"></param> 
/// <returns>List<LookupItem></returns> 
public static List<LookupItem> GetLookupItemChildren(string itemPath) 

Item item = Sitecore.Context.Database.Items[itemPath]; 
List<LookupItem> lst = new List<LookupItem>(); 

if (item != null) 

foreach (Item child in item.Children) 

lst.Add( new LookupItem(child["Key"], child["Phrase"],child["AlternatePhrase"])); 



return lst; 


///<summary> 
/// Getting Countries by Countries 
///</summary> 
public static List<LookupItem> GetCountries() 

return Lookups.GetLookupItemChildren(SiteSettings.LookupsPath + "/Country"); 


///<summary> 
/// Getting Regions By Country KEY 
///</summary> 
public static List<LookupItem> GetRegions(string countryKey) 

Item item = Lookups.GetItemByKey(SiteSettings.LookupsPath + "/Country", countryKey);
return Lookups.GetLookupItemChildren(SiteSettings.LookupsPath + "/Country/" + item.Name + "/Regions"); 


In Sitecore 7 you can replace these item lookups with LINQ statement.

Dennis Augustine has now created and uploaded the Country Region Dropdown Sitecore Module based on the blog post and it is now available in the Sitecore MarketPlace as a Development Accelerator.

If you have any questions or any tips, tricks or resources you would like to share with the Guild please email me at chris.williams@threepointturn.com or dennis.augustine@threepointturn.com 

Sunday, June 9, 2013

Google Chrome and the 301 Redirect Cache or Why are my 301 Redirects not hitting my server

301 Redirects are important to maintain search rankings and help users with browser bookmarks still find the right location on your site.
                 
This article helps you build your own 301 redirect handler for Sitecore: Create the ultimate 301 Redirect Handler for Sitecore

PROBLEM: Recently while building it myself I found an interesting but/feature of chrome.  When I was debugging my 301 redirect handler the first time I hit the URL it hit my breakpoint and I could debug the update however when I hit it again it redirected without hitting my handler. It turns out this is a feature of chrome.  Since the 301 is a permanent redirect, Chrome remembers the redirect and caches it.
Here is an article that explains it more: Google Chrome Caches 301 Redirects

SOLUTION: There are actually two possible solutions:
  1. Debug in Internet Explorer and FireFox and once working use Chrome once to ensure it redirects.
  2. Chrome has a way of turning this feature off How Can I Make Chrome Stop Caching Redirects
Hope it helps someone else in the Guild avoid the same issue.

If you have a tip, trick or resource you would like to share with the guild please email me at chris.williams@threepointturn.com

Tuesday, June 4, 2013

Toronto Sitecore User Group June 19,2013 will be be DMS 101

If you are in the Toronto area on June 19th, you may wish to register for the Toronto Sitecore User Group.
The topic will be DMS 101.  

Register at: http://toronto-sitecore-user-group.eventbrite.ca/#

Look forward to seeing the Guild Members there.