How to Auto-fill umbracoUrlAlias with Friendly URL
Hi there everyone.
I've got a situation where I need to auto-fill the umbracoUrlAlias field with the Page Name turned friendly URL.
The purpose being my client does not like the page URLs following the node path, no matter how deep the page is he wants it to render like "http://sitename.com/page-name".
I've found/implemented a way to change all the links to the alias once it's set via this post.
Now all I need is to auto-fill the alias field so any help with that would be appreciated, or if you know a better way of accomplishing this task I'm all ears.
I'm new to Umbraco, so be easy on me if I'm blundering around. :)
You can use the Umbraco events to automatically populate the umbracoUrlAlias property value when the document is saved.
This thread shows an example of setting the pageTitle property on the ContentCreated event. Take a look at the Saving event documentation too.
This isn't exactly what you are after but you can also override the default url segment (not the whole url) with umbracoUrlName.
The magic property names work and are simple to implement but for full control you should look at implementing a custom ContentFinder and UrlProvider. You can use these to override the Umbraco routing. You could implement the custom UrlProvider to return only the page name (excluding all parent nodes) and the custom ContentFinder to find the page based on the url.
I'm an Umbraco beginner, so there will be a better way, but when we wanted to ensure that all our pages could be accessed via a short URL, we wrote the following into App_Code/SetUmbracoUrlAlias.cs
using System;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using umbraco.cms.businesslogic.web;
namespace Torbay.UmbracoUrlAlias
{
public class MySaveHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
// call our extra function when saving items
ContentService.Saving += ContentServiceSaving;
}
private void ContentServiceSaving(IContentService sender, SaveEventArgs<IContent> args)
{
foreach (var node in args.SavedEntities)
{
if (node.HasProperty("umbracoUrlAlias"))
{
// have an umbracoAlias property so check it is filled to enable short URLs
string n = node.Name.ToLower().Replace(" ", "-").Replace("(", "").Replace(")", "").Replace("?", "").Replace("'", "").Replace("‘", "").Replace("’", "").Replace("/", "").Replace("@", "-").Replace(",", "").Replace(":", "").Replace("+", "plus");
string uua = node.GetValue<string>("umbracoUrlAlias");
if (String.IsNullOrEmpty(uua))
{
// umbracoUrlAlias empty so just store the current page name
node.SetValue("umbracoUrlAlias", n);
}
else if (uua.Contains(","))
{
// umbracoUrlAlias has more than one entry, so check them all, and if the current one is not already there then append it
string[] uuas = uua.Split(",".ToCharArray());
bool alreadyThere = false;
foreach (string u in uuas)
{
if (u.Equals(n))
{
alreadyThere = true;
break;
}
}
if (!alreadyThere)
{
node.SetValue("umbracoUrlAlias", uua + "," + n);
}
}
else
{
// umbracoUrlAlias has just one entry, so check it, and if it's different then append the current page name
if (!(uua.Equals(n)))
{
node.SetValue("umbracoUrlAlias", uua + "," + n);
}
}
}
}
}
}
}
Our property umbracoUrlAlias, label "Short URL(s)", description "Short URLs in lower case (each one will become a working short URL via Umbraco's umbracoUrlAlias feature)
so users don't need the full URL and we can match old Livelink URLs" is of our datatype tby.ShortUrls which uses the Tags property editor (alias Umbraco.Tags), tag group "ShortUrl", storage type "Csv".
And we added a link rel="canonical" href="http://server.com/preferredurl/" tag in the head to help our and other search engines tell that the URLs were the same page
How to Auto-fill umbracoUrlAlias with Friendly URL
Hi there everyone.
I've got a situation where I need to auto-fill the umbracoUrlAlias field with the Page Name turned friendly URL.
The purpose being my client does not like the page URLs following the node path, no matter how deep the page is he wants it to render like "http://sitename.com/page-name".
I've found/implemented a way to change all the links to the alias once it's set via this post.
Now all I need is to auto-fill the alias field so any help with that would be appreciated, or if you know a better way of accomplishing this task I'm all ears.
I'm new to Umbraco, so be easy on me if I'm blundering around. :)
Thanks,
-Elijah
Hi Elijah
You can use the Umbraco events to automatically populate the umbracoUrlAlias property value when the document is saved.
This thread shows an example of setting the pageTitle property on the ContentCreated event. Take a look at the Saving event documentation too.
This isn't exactly what you are after but you can also override the default url segment (not the whole url) with umbracoUrlName.
The magic property names work and are simple to implement but for full control you should look at implementing a custom ContentFinder and UrlProvider. You can use these to override the Umbraco routing. You could implement the custom UrlProvider to return only the page name (excluding all parent nodes) and the custom ContentFinder to find the page based on the url.
Dallas
I'm an Umbraco beginner, so there will be a better way, but when we wanted to ensure that all our pages could be accessed via a short URL, we wrote the following into App_Code/SetUmbracoUrlAlias.cs
Our property umbracoUrlAlias, label "Short URL(s)", description "Short URLs in lower case (each one will become a working short URL via Umbraco's umbracoUrlAlias feature) so users don't need the full URL and we can match old Livelink URLs" is of our datatype tby.ShortUrls which uses the Tags property editor (alias Umbraco.Tags), tag group "ShortUrl", storage type "Csv".
And we added a link rel="canonical" href="http://server.com/preferredurl/" tag in the head to help our and other search engines tell that the URLs were the same page
is working on a reply...