Sitecore Box

Sitecore web developer at Americaneagle.com

Hide Folder Item Name From URL in Sitecore




In this blog I will explain how we can exclude an item name from the URL programmatically. This can be done by using the out of the box alias feature in Sitecore, so when you create a new item under a specific path, an alias will be created for that item excluding the item you want to hide.


Aliases are based on the System/Alias template and defined under /sitecore/system/Aliases. Each alias name must be unique; the alias template contains a single field in the data section which allows for selection of the target item.

Sitecore determines the default URL for an item based on its path, for example, the URL for /sitecore/content/home/products/product1 is products/product1. Sometimes you need to have a shorter URL which maps to another URL, for example, accessing the above link by this URL /product1; and this what I am going walk you through in this blog.




What we need to do is adding an event on "item:created" to create a new alias whenever we create a new item under “Products” folder as you see in the below screenshot:




Below is the event handler for "item:created":

public class ItemCreatedHandler
{
//This is for creating an alias item to any new item added under this path /sitecore/content/Sanford/ORG/Home/Campaign
public void OnItemCreated(object sender, EventArgs args)
{
try
{
var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
Assert.IsNotNull(createdArgs, "args");
if (createdArgs != null)
{
Assert.IsNotNull(createdArgs.Item, "item");
if (createdArgs.Item != null)
{
Item newItem = createdArgs.Item;
Item aliasParentItem = null;
Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
Item productsFolderItem = masterDb.GetItem("{CB746F1D-4A6F-471A-8E9B-0E5050E07714}");
//if the current item is not a child of products folder /sitecore/content/home/products/ or doesn't have a layout
if (!newItem.Axes.IsDescendantOf(productsFolderItem) || !HasLayout(newItem))
return;
TemplateID aliasTemplateID = new TemplateID(new ID("{54BCFFB7-8F46-4948-AE74-DA5B6B5AFA86}"));
if (aliasTemplateID.ID.IsNull)
return;
using (new SecurityDisabler())
{
if (productsFolderItem == null)
return;
//Get the alias parent path inside the alias folder ex: "/product1/product1-A"
string aliasParentPath = newItem.Parent.Paths.Path.ToLower().Replace(productsFolderItem.Paths.Path.ToLower(), "");
if (newItem.Parent.ID != new ID("{CB746F1D-4A6F-471A-8E9B-0E5050E07714}"))//For the products Folder Descendants
{
//Check if the alias parent exists and if it is pointing to the parent item under the products Folder
if (masterDb.Aliases.Exists(aliasParentPath) && masterDb.Aliases.GetTargetID(aliasParentPath) == newItem.Parent.ID)
{
aliasParentItem = masterDb.GetItem(Constants.Pathes.AliasesItem + aliasParentPath);
}
if (aliasParentItem == null)
return;
}
if (aliasParentItem == null)
{
aliasParentItem = masterDb.GetItem(Constants.IDs.AliasesFolderID);
}
//Create alias
Item aliasItem = aliasParentItem.Add(newItem.Name, aliasTemplateID);
if (aliasItem == null)
return;
LinkField linkField = aliasItem.Fields[Templates.Alias.Fields.LinkedItem];
if (linkField == null)
return;
aliasItem.Editing.BeginEdit();
linkField.LinkType = "internal";
Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
urlOptions.AlwaysIncludeServerUrl = false;
linkField.Url = Sitecore.Links.LinkManager.GetItemUrl(newItem, urlOptions);
linkField.TargetID = newItem.ID;
aliasItem.Editing.EndEdit();
}
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message, ex, this);
return;
}
}
public bool HasLayout(Item item)
{
return item?.Visualization?.Layout != null;
}
}
view raw Create alias hosted with ❤ by GitHub

We need to create a new config file and add it the App_config folder:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="item:created">
<handler type="Common.Foundation.Scaffolding.Handlers.ItemCreatedHandler, Common.Foundation.Scaffolding" method="OnItemCreated" />
</event>
</events>
</sitecore>
</configuration>

1 comments:

Bonus Sepet said...

Bonus Sepet - Anne ve Bebek, Kadın, Erkek, Giyim, İçgiyim, Kişisel Bakım, Elektronik, Hediyelik Eşya, Market Ürünleri satışı yapmaktadır.

Post a Comment