Skip to content

Instantly share code, notes, and snippets.

@JitendraSitecore
Last active June 7, 2020 11:01
Show Gist options
  • Save JitendraSitecore/52c740b8807707ae699d3882d4dabf66 to your computer and use it in GitHub Desktop.
Save JitendraSitecore/52c740b8807707ae699d3882d4dabf66 to your computer and use it in GitHub Desktop.
Item Name Sitecore event handler
protected void HandleItemName(object sender, EventArgs args)
{
try
{
InitializeConfiguration();
Item item = Event.ExtractParameter(args, 0) as Item;
if (PageTemplates.Contains(item.TemplateID.ToString()) &&
item.Database.Name == this.ItemHandlerDatabase &&
item.Paths.Path.StartsWith(this.ItemHandlerParentItemPath) &&
!item.Name.Equals(item.Name.ToLower().Replace(" ", SEOFriendlyCharacter))
)
{
string displalyName = item.Appearance.DisplayName;
if (string.IsNullOrEmpty(displalyName))
{
displalyName = item.Name;
}
using (new SecurityDisabler())
{
item.Editing.BeginEdit();
item.Appearance.DisplayName = displalyName;
item.Name = displalyName.ToLower().Replace(" ", SEOFriendlyCharacter);
item.Editing.EndEdit();
}
}
}
catch (Exception ex)
{
Log.Error("ItemnameHandlerandleItemName :", ex, this);
}
}
cd 'master:/content/Home'
Get-ChildItem -Recurse . | ForEach-Object{
if($_.Fields["__Display name"].Value -eq "")
{
$originalName = $_.Name
}
else
{
$originalName = $_.Fields["__Display name"].Value
}
$newName = $originalName.Replace(" ", "-")
$newName = $newName.ToLower()
Write-Host "Renaming item from " -nonewline;
Write-Host $originalName -f Yellow -nonewline;
Write-Host " to " -nonewline;
Write-Host $newName -f Green;
if($newName -ne "")
{
$_.Editing.BeginEdit()
$_.Name = $newName;
$_.Fields["__Display name"].Value = $originalName;
$_.Editing.EndEdit()
""
}
else
{
Write-Host $orignnalName
}
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set">
<sitecore>
<events>
<event name="item:added">
<handler type="XXXX.Events.ItemnameHandler, XXXX" method="HandleItemName" />
</event>
<event name="item:renamed">
<handler type="XXXX.Events.ItemnameHandler, XXXX" method="HandleItemName" />
</event>
</events>
<settings>
<setting name="Page_Template_Ids"
value="{4EBC52AA-AE5B-4099-96BD-EC631CBDFB93},{00931377-84E1-4061-9A64-AB98EB09B863},
{7D6FF33A-C873-4F85-BA14-9DAB6E729BA2}" />
<setting name="PageTemplate_Ids_Delimeter" value="," />
<setting name="ItemHandler_Database" value="master" />
<setting name="SEO_Friendly_Character" value="-" />
<setting name="ItemHandler_ParentItemPath" value="/sitecore/content/Home" />
</settings>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment