Mcbeev.com

Taking it one line of code at a time

I’ve been pretty surprised at the traffic, mention, and email feedback that my last post, 7 Things you might want to check after launching a new Kentico CMS website, has received. In fact I was presented with a very good question in my inbox today about a detail from the fourth item in that list.

Pretty much the question boiled down to, why after submitting the Google site map URL to Google’s Webmaster tools, was the specified website’s pages not showing up in the resulting index or when viewed at the sites ~/CMSPages/GoogleSiteMap.aspx page.

Since the question came in from a friend, I decided to dig deeper and lend a hand. As soon as I logged into the site’s CMSDesk I quickly noticed what was up. Most of the content pages were using Custom Document Types. I was actually impressed to see this because it is sort of an advanced feature to use inside Kentico and normally an under utilized feature as well.

The output of the GoogleSiteMap.aspx page looked something like this, only the normal Menu Items from the CMS Tree:

 

Kentico CMS Google Site Map default

 

So after some quick research I found the devnet article on how to integrate the Googe site map feature. After reading the second paragraph of that article I knew exactly what was up. Turns out that by default the Google site map feature from Kentico only includes the “Page (menu) item” Document Types. This makes sense for someone that doesn’t have a lot of custom Document Types or dynamic content.

However, if you use custom Document Types, and you should, they rock, then you need to pop open your favorite text editor and add a very small amount of code to make the custom pages show up. Open up ~/CMSPages/GoogleSiteMap.aspx. The server tag in question that we are looking to change is the <cms:GoogleSiteMap> tag.

Default:

 

<cms:GoogleSitemap runat="server" ID="googleSitemap" TransformationName="CMS.Root.GoogleSiteMap" CacheMinutes="0" OrderBy="NodeLevel, NodeOrder, NodeName" /> 

 

Now what we need to do is add in whatever custom Document Types that we have, into a ClassNames property, chances are it is not there already. Let's use BizStream.TeamLeaderProfile in this example.

Change To:

 

<cms:GoogleSitemap run at="server" ID="googleSitemap" TransformationName="CMS.Root.GoogleSiteMap" CacheMinutes="30" OrderBy="NodeLevel, NodeOrder, NodeName" MaxRelativeLevel="-1" ClassNames="CMS.MenuItem;BizStream.TeamLeaderProfile" />

 

Don't forget that the property can accept multiple custom Document Types delimited by a semi-colon. Now save the page and you are all set. All of your pages should show up at the GoogleSiteMap.aspx url. Go ahead and browse to it to check it out.

You should now see the extra URLs part of the page output like:

 

Kentico CMS Google Site Map with Custom Document Type

 

I’d also like to point out that the default control does not look to be using the Cache system provided Kentico, CacheMinutes=0. I would recommend upping this value from 0 to at least 30 minutes if not more. There is a small chance that 0 could mean inherit from the top level tree or site, but I haven’t been able to verify this or not.

Note: if your curious about what kinds of pages that a custom Document Type can generate, check out the About page over at BizStream.com. That’s one example of how BizStream uses custom Document Types.

One last tip, like the devnet article mentions you don’t have to use that path to submit to Webmaster tools with, it is kind of long. You can change it to whatever you want by going to CMSSiteManager -> Settings -> (global) in list -> URLs -> Google sitemap URL.

I hope you enjoyed this small article about using the Google site map feature of Kentico CMS. As always leave feedback and opinions in the comments or send them to me email.


(Note: this post assumes that you read the primer post on URL Rewriting & Aliasing in Kentico CMS)

 

In my last post I described what it takes to use the URL Rewriting and Document Aliasing capabilities of Kentico. I also promised a twist to those who made it through the entire blog post, and here it is. So without further ado, I now present my solution for creating a Custom Document Alias in Kentico that is QueryString aware.

Let’s get started. Now that you understand more about URL Rewriting say you had a URL like this:

 

http://mcbeev.com/products/item.aspx?type=Chips&name=Baked Lays

 

Pretend for a minute that your store also sold candy bars, and we decided to reuse the same old page to display both types of products. We would still have a URL like:

 

http://mcbeev.com/products/item.aspx?type=Candy Bars&name=Snickers

 

Our example translated into a standard Kentico setup:

 

url_tree_2

 

And here ladies and gentlemen we hit our first snag with the Kentico CMS out of the box and aliasing. You can URL Rewrite and even Document Alias any node in the content tree. However, the one small gotcha is that you can not create an Alias that looks at QueryString values, or anything after the question mark.

The default Kentico engine will just replace any special character with a dash in the in the URL Path or URL extension field of the Document Alias, therefore wiping out the important part of the QueryString that our example URL above is relying on.

I have actually run into this in my real world projects where I am upgrading a pre existing site that has lots of old links from it’s website as well as other websites that match the URL format in our above example.

Bummer.

To fix this we have to get a little creative and write some code. This is a two part solution. First, we need to make a generic redirector page with a Document Alias of the first part of the URL above.

I created a new page called RedirectItems at the root of my Kentico site and then added a Document Alias like so:

 

item_Redir

 

The purpose of this page will be catch all the incoming requests regardless of QueryString value, and hand it off to the right place after running a bit of code on that page, or Web part.

The next step we need to do is create a new custom Web part. The purpose of this new Web part will be to to grab the information out of the the request that we need. To do so I used these instructions from Kentico on how to create a new custom Web part.

 

WebPart_Redir

 

The key here is that we create the new Web part called Redirector (or whatever you like) and then give it one Property called RedirectToPaths. This is where you will set your URL templates for how we can handle the QueryString redirects.

 

WebPart_Redir_Properties

 

At this point you are safe to click save on the Create Web part Screen. Next we need to add in some code to our Web Part.

Once the new Web Part is added in the CMSSiteManager and the file is in ~/CMSWebParts/Your Category Name/ you can open up the source code of Redirector.ascx.cs with your favorite Text Editor or Visual Studio.  And add the following OnPreRender method.

 

protected override void OnPreRender(EventArgs e)
{
	base.OnPreRender(e);

	if (this.StopProcessing)
	{
		// Do nothing
	}
	else
	{
		//Collect the URL information from the current Request
		string currURL = HttpContext.Current.Request.RawUrl;
		string queryString = string.Empty;
		string newURL = string.Empty;

		//Check to make sure some query string variables exist
		int iqs = currURL.IndexOf('?');

		//QueryString variables exist, put them in a string.
		if (iqs >= 0)
		{
			queryString = (iqs < currURL.Length - 1) ? currURL.Substring(iqs + 1) : string.Empty;
		}

		if(queryString.Length > 0)
		{
			//Parse the querystring		
			NameValueCollection qsKeys = HttpUtility.ParseQueryString(queryString);

			//Pull in the Path Expressions from the Web Part property called RedirectToPaths
			string redirectToPaths = (string) this.GetValue("RedirectToPaths");

			//If the property is empty do nothing
			if(redirectToPaths.Length > 0)
			{

				//Split out the property value by a comma in case there are multiple
				foreach(string p in redirectToPaths.Split(','))
				{
					string path = p;
					bool allKeysInPath = true;

					//Iterate through the keys and replace the key with the value
					foreach (string key in qsKeys.AllKeys)
					{

						if(path.Contains("{"+ key + "}"))
						{
							path = path.Replace("{"+ key + "}", qsKeys[key]);	
						}
						else
						{
							allKeysInPath = false;
						}
					}

					//Handle whitespace
					path = path.Replace(" ", "-");

					//Make sure we got a redirectToPath that handles all the keys
					if(allKeysInPath)
					{
						newURL = path;
						break;
					}

				}

				//Check if we generated a new URL
				if(newURL.Length > 0)
				{
					//Redirect to our new path
					Response.Redirect(newURL);
				}

			}

		}
	}
}

 

 

 

That’s all the code we need to touch, so close and save the file. Now let’s add our new Redirector Web Part to our site. Open up the CMSDesk again and go to where you created the page called RedirectItems from above. Click on the Design tab of the page and add in new Redirector Web Part that we just created. Then click configure. You should be at a modal window that looks like this:

 

WebPart_Redir_Filled

 

Notice our RedirectToPath property shows up and I have typed in two URL templates. Go ahead and copy the second string /Products/{type}/{name} into your Web part. Then click OK. I think you can probably guess how that maps to our original problematic URL of:

 

http://mcbeev.com/products/item.aspx?type=chips&name=Baked Lays

 

Basically the code will replace anything inside the curly brackets that matches the name of the parameter with the value of the parameter. So for the preceding example, we will end up being redirected to the correct location of:

 

http://mcbeev.com/Products/Chips/Baked-Lays

 

So there you have it, this custom Web part will now give us a small enhancement to the built in ability of Document Aliasing for Kentico CMS. The code for the Web part can be found at the link below.

I hope this idea helps you maintain working links when upgrading a pre existing site the way it did for me. As always please feel free to leave feedback for me in the comments.

 

Web part code download


Kentico CMS is extremely powerful when it comes to controlling the URL of a website. Right out of the box, you get the power of aliasing whatever URL you want to whatever document or resource you have in the content tree.

Heck since Kentico 4.x you can even mask/change the extension or just get rid of it all together, to get more control of your site’s URLs.

I can’t stress enough how beneficial this feature is when it comes to creating SEO friendly links, maintaining out of date website paths/structure, or just making it easier on your visitors to reach the pages that they need to get to.

Let’s get started with an example. For instance let’s say your website had a products section that sold different types of potato chips. My favorite potato chips are Baked Lays. So we will go with that for the example.

Way back when developers didn’t do much to make the URL SEO or user friendly, you would have a typical URL like these:


http://mcbeev.com/products/chips.aspx?name=Baked Lays

Google and other search engines see this only as one URL, …/chips.aspx, which doesn’t do us very good.

Now let’s say you had this same website in Kentico. It might look something like this:

 

url_tree

 

There a few things to notice, first if you navigate to the Baked Lays page your URL would be:


http://mcbeev.com/Products/Baked-Lays.aspx

This is nice right? No crazy question mark in the QueryString or namevalue pairs. This is one example of URL Rewriting. Behind the scenes there is no physical file on the file system that IIS is serving out. The Kentico 404 HTTPHandler is intercepting the request and and feeding it through the content in the database of the site you have, and serving out the correct page based on that.

The second thing to notice is the .aspx extension. I mentioned above that you can mask that extension or even get rid of it. We see it here because by default in Kentico Extensionless URLs are turned off. If you check the Friendly URL extension field in the CMSSiteManager –> Settings –> URLs and SEO section, you will see .aspx value. Just clear this field out and click save and now URLs will work as:


http://mcbeev.com/Products/Baked-Lays

As you can see your end result is even nicer now.

(Note: You may need to configure II6 or II7 to handle Extensionless URLs as well, please check out the Kentico Documentation for further info on that)

So for instance, if you also sold Doritos, your more savvy users might be able to just look at the URL in the address bar and guess what they should type in to get to a different product page. Maybe:


http://mcbeev.com/Products/Doritos

And it would just work. This really just brings a nice and logical experience to your users, and Kentico does a great job of it.

One more point on Document Aliasing. An alias allows you to make up a URL that can be completely different than what the name of the document is in the Tree. To add an Alias go to the CMSDesk –> Select your Page –> Properties –> URLs subtab:

 

alias

 

You can see that I added a Document Alias with a URL Path of /Products/Best-Chips-in-the-World. If you copy that into your browser that will lead you to the exact same page as /Products/Baked-Lays. You can have as many of these Document Aliases as you want on each document.

Now I know what you are thinking if you are a long time user of Kentico, and that probably is: Brian why are you writing a blog post on such old features like this ?

And the sixty four thousand dollar answer is… I want to make sure that you have a good understanding of URL Rewriting and Document Aliasing, because in my next post I am going to throw a twist into the the whole situation and solve one shortcoming that Kentico has on this very topic.

Check back next time!

Update: See the next post in the series here.


Don't forget the simple things when it comes to Kentico website launches

Kentico_2D_CMS_width200px

You have just finished importing your export file onto the live server, generated that fresh new license file and installed it, double checked your server's host headers, and fired up your browser of choice to http://www.somesite.com. Now wipe that smile away from your face, you still have work to do.

 

1. Minimize any JavaScript files that you can.

Strip out those white spaces people, it will make your site appear faster to load and execute to your users. If you don’t have a tool that you already use, or don’t know exactly what I’m talking about check out JSmin from Douglas Crockford.

 

2. Disable ViewState on controls and widgets that don't need it.

Yes we all love webforms development and it's ever present ViewState, but you know what, most of the time we don't need it on a Kentico site, or page that doesn't need to do a PostBack to the server. Let's disable it when we have the chance. This will reduce the size of ViewState and make that page download even faster.

Kentico CMS WebPart - Disable ViewState

 

3. Setup Google Analytics.

One of the keys to maintaining a website is knowing what pages get used the most and what pages don’t get used at all. This is true for both big and small websites. Instead of guessing at design decisions on where you think certain elements or functionality should go, place them where they will get used the most by looking at some analytical data.

Sign in to Google Analytics

 

4. Setup Google Webmaster tools.

One of the more impressive, and often least known built in functionality of Kentico is the Google Site Map generator given to you right out of the box. On any Kentico CMS 4.x+ site, hit http://www.somesite.com/cmspages/googlesitemap.aspx. The Kentico system will automatically spit out the correct xml formed document that you can submit to the sitemap section in Google WebMaster tools. This will help you on your Google Search Result ranking.

Sign in to Google WebMaster Tools

 

5. View the Source and Critique it.

This is a big one, and probably should be the most important tip. It will give you some clues on a few things. If you see a big chunk of ViewState please refer to number 2 in this list. Again this probably means that a control or widget did not get set to Disable ViewState.

If you see URL's that are not user or SEO friendly, you might  want to go back into the CMSDesk and verify the links are going to the correct place. I’m not a huge fan of the getdoc method of making links, so generally if I see that then I try to make it into a more friendly link with a URL Alias.

If you see large chunks of inline JavaScript or common JavaScript on multiple pages, that can be moved out to a separate file and minimized the by all means do it. Don’t get too crazy with external files though, if you can get it all into one that is minimized that would be the best.

If you see huge amounts of line breaks or tabs than go back and find out what control is causing it, and remove the whitespace. Templates and/or Transformations can sometimes add extra whitespace, that's a good place to start looking on this one.

 

6. Check your Smart Search Index / Build Status.

If your site uses the Smart Search feature of Kentico, then be aware you need to have the index created. The Kentico documentation is pretty good at laying out what you need to do to make this work.

The button to rebuild a Smart Search Index is located under CMSSiteManager –> Administration –> Smart Search

SmartSearch

 

7. Use the Debug Modes to Check Caching and SQL Queries

The Debug Mode found at CMSSiteManager –> Administration –> System –> Debug, can be invaluable in learning what is getting cached and what is not. An Object or DataSet that is not getting cached is one of the biggest causes of slowness of a Kentico site. Martin Hejtmanek  the CTO of Kentico has a great presentation on caching in Kentico and blog post on debugging in Kentico.

Please remember to turn this off when you are done! 

 

That's all for now

I hope some of these tips will help you out with your Kentico CMS projects and sites. I’d love to hear some feedback on this blog post because it’s my first one on the topic of Kentico, and honestly the first blog post in a long while on any topic for me.


IE 7 bug on these forums

I think i just found a bug that in IE 7 you cant post to these forums. doooh. Time to upgrade. Oh well I think Im only about 3 versions behind the latest heh.


PEBKAC ...no PEBKAF

Usually PEBKAC (Problem Exists Between Keyboard And Chair) is one of my favorite sayings when it comes to work.  Well today I can modify that statement to PEBKAF. (Problem Exists Between Keyboard And Floor).

While coding away, like the monkey I am, at my current client's, multiple users came up to me and complained of a slow network or no network at all. Thinking that I could save the day with a great explanation of what it means to have a cord properly in place here, or a setting properly configured here, which 9 times out of 10 fixes a problem when it comes to computer, I went to go check it out.

Well 3 hours later, I was standing in a network closet weeding my way through more cabling that I thought could fit in one small room. Yes the whole building was down. We tried everything we could think of to figure out the problem, ping's, tracert's, packet captures, switch maintenance, power cycles, even a phone call to Dell gold support, until finally we had to go through every single port, one a time.

Usually thats not too bad, but in this case there were something like 96 ports when you figured the building switch plus the layer 3 device. I dont know how many we got through, but luckilly we found the right one. If you would unplug it the network traffic worked great, but plug it back in and more UDP packets than you could shake a stick at would flood the network in no time.

So after searching the building for the correctly labeled jack we found the offending device, or should I say devices. Yes devices. You see someone had plugged a linksys switch into another linksys switch beneath a desk and a tangle of cat 5 cable. They had of course plugged it into the wrong port that was labeled Uplink.

So there you have it. Problem Exists Between Keyboard And Floor.

The lesson here is that even a 20 dollar Best Buy special can bring down the most powerful network.


Error ASAP !!!

Ok, seriously, I know Visual Studio 2005 is a "beta" product still, but it still makes me shake my head sometimes. Take this error message for example -Restart your application as soon as possible ?!?! What was the developer late for a beer meeting after work or something ? There's nothing like a little urgency in an error message to really get you worried. Jeeesh.


About Me

I have over 11 years of experience doing this crazy web development thing mainly at BizStream. I'm passionate about software platforms and technologies that can help solve real world problems. I enjoy hanging out with my wife, chasing around my three children and vigorously rooting for the Michigan State Spartans and Detroit Lions. (I know, right? Who still roots for the Lions?)

View Brian McKeiver's profile on LinkedIn

 

PhotoStream

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

last.fm

Kings of LeonCloserabout 26 days ago
.38 SpecialFantasy Girlabout 26 days ago
Dirty Pretty ThingsGin & Milkabout 26 days ago
Little Man TateThis Must Be Loveabout 26 days ago
MilburnBrewsterabout 26 days ago
The HollowaysFit For A Fortnightabout 26 days ago
Kings of LeonI Want Youabout 26 days ago
Hard-FiSuburban Knightsabout 26 days ago
AerosmithJust Push Playabout 26 days ago
DispatchMissionabout 26 days ago

Xbox GamerCard

Offline 9/6/2010 9:47:26 PM Last seen 09/07/10 playing Xbox 360 Dashboard