SharePoint Blog Post redirect fails with 404

05/08/2014

Todays adventure in SharePoint land was a strange one. The client I’m working for at the moment are having webs created from both English and Danish webtemplates in the same site collection. They also have some custom page layouts and custom code that lets the user decide if they want to see a particular page in either English or Danish, when the user changes languages it sticks to their session by saving their choice in a cookie.

The problem that surfaced today, was that people with the English language selected would get redirected to a 404 Page Not found when they were trying to access a blog post linked to from e.g. the search results, when the blog post was located on a blog created with the danish localized blog web template. (The blog template is the standard one you get if you install the danish language pack).

I’m not sure if the problem only exists in SharePoint 2013, or if it affects previously versions, but it might very well be in older versions too.

The redirect flow is under normal circumstances to a blogpost is pretty peculiar, so just for reference I’m going to list it here.

The link to the blog post when found in a search result looks like this

da-dk http:///blog/Lists/Blogmeddelelser/Viewpost.aspx?ID=1
EN-us http:///blog/Lists/Posts/Viewpost.aspx?ID=1

Once this link is clicked your get redirect to a page with the following url

http:///blog/_layouts/15/listform.aspx?PageType=4&ListID={LISTID}&ID=1&ContentTypeID={CONTENTTYPEID}

The PageType define whether it’s the display or editform for the blog post we are gonna get redirected too, 4 means display form. One would think that the ListID and ID (ItemId) would be enough to find the correct blog post, so why the ContentTypeID is in there is a mystery. But the ContentTypeID is actually the reason for the error in the first place.

What the listform.aspx page does (I checked the code with a reflector) is it reads the ContentTypeID, and then looks up the content type from the list and from the contenttype definitions finds the property DisplayFormUrl which is used for showing the blog post.

The error happens when the Danish or another localized blog site is created by an user running using English, because then the Post content-type attached to the list has a DisplayFormUrl that points to Lists/Posts/Post.aspx but the url for the list on a Danish blog site is called “Blogmeddelelser” so the url is pointing to something that doesn’t exists.

The way to correct is is to update the contenttype which can be done with the following powershell:
[powershell]
param($blogweb, $blogListName = "Blogmeddelelser")

$web = get-spweb $blogweb

$list = $web.Lists[$blogListName]

$ct = $list.ContentTypes["Post"]
$ct.DisplayFormUrl = "Lists/$blogListName/Post.aspx"
$ct.Update()
[/powershell]