January 28th, 2012
Today I migrated my weblog from Sitefinity 3.7 to Sitefinity 4.4. Luckily there is a nice migration manager that comes with Sitefinity 4.x, so it is quite easy to migrate content.
After the migrations were done, I noticed that the blog posts were still holding the old URL names.
For example, this post will have the following URL:
/change-the-url-of-your-blogposts-from-code
Within the old URL structure the URL would look like this:
/change-the-url-of-your-blogposts-from-code.aspx
Notice the page extension on the end.
To change the URLs for all blogposts at once, I came up with the following code:
We loop through all of the blogposts and clear the existing URLs. Then we generate a new UrlName based on the title of the blogpost (with the use of RegEx).
We call the RecompileAndValidateUrls method to regenerate the new URLs for the post and finally we publish and save the post.
This way I recreated all URLs for all posts in just a few seconds.