November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
Have a look at:
IUrlSegmentGenerator
https://world.episerver.com/CsClassLibraries/cms/EPiServer.Web.IUrlSegmentGenerator?version=11
I think you will have to handle duplicates yourself. Something like this:
var urlSegmentGenerator = ServiceLocator.Current.GetInstance<IUrlSegmentGenerator>();
var generatedUrlSegment = urlSegmentGenerator.Create("existing-name");
// Handle duplicate URL-segments among siblings
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoeader>();
var contentWithSameUrlSegment = contentLoader.GetBySegment(ParentLink, generatedUrlSegment, Language);
var suffix = 1;
while (contentWithSameUrlSegment != null && contentWithSameUrlSegment.ContentLink.ID != ContentLink.ID)
{
generatedUrlSegment = generatedUrlSegment + suffix++;
contentWithSameUrlSegment = contentLoader.GetBySegment(ParentLink, URLSegment, Language);
}
If possible, use constructor injection instead of servicelocator.
Hi Frans,
You can modify the product's content route segment on the CreatedContent event similar to below e.g.
Note: You need to pass your product variation type instead of the ShirtVartion type.
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class UpdateCatalogRouteSegmentInitializationModule : IInitializableModule
{
private IContentEvents _contentEvents;
public void Initialize(InitializationEngine context)
{
if (_contentEvents == null)
{
_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
}
_contentEvents.CreatedContent += ContentEvents_CreatedContent;
}
private void ContentEvents_CreatedContent(object sender, ContentEventArgs e)
{
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
if (contentLoader.Get<ShirtVariation>(e.ContentLink).CreateWritableClone() is ShirtVariation
generatedVariation)
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var suffix = generatedVariation.RouteSegment.Split('-')?.LastOrDefault();
var newSegment = suffix != null ? generatedVariation.RouteSegment.Replace(suffix, "") : generatedVariation.RouteSegment;
var count = 1;
if (suffix != null && int.TryParse(suffix, out int num))
{
count = num + 1;
}
generatedVariation.RouteSegment = newSegment + count;
contentRepository.Save(generatedVariation, SaveAction.Publish, AccessLevel.NoAccess);
}
}
public void Uninitialize(InitializationEngine context)
{
if (_contentEvents == null)
{
_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
}
_contentEvents.CreatedContent -= ContentEvents_CreatedContent;
}
}
I tried combining your answers but in the CreatedContent event the name with the randomly generated string is already set. Is there an event that occurs before the name is generated?
Hi!
If I create a product in the CMS with an already existing name the resulting URL becomes something like "existing-name-e1a22eda" where "e1a22eda" part is dynamically generated.
How would I go about modifying the logic behind the url generation? If I instead would like the resulting url to be "existing-name-1" and if I add yet another it becomes "existing-name-2" etc.?