PublishedContent event and UrlResolver problem

Vote:
 

I have a problem with the PublishedContent event and UrlResolver. I have added a method to the PublishedContent event to handle a page of type container page. I the container page has an updated url segment I want to get all the new urls for the child pages. However when i try urlResolver. GetUrl(child) the returned string till has the old container page urlsegment in it.

Example:

ContainerPage url segment from "container1" to "container2" -> publish

in method urlResolver.GetUrl(child) then returns "container1/child" instead of "container2/child".

If I try urlResolver.GetUrl(childpage) later the returned string is correct so im guessing this have to do with the url segment not updating untill after the publishedContent event is done. 

Has anyone hade experience of this and found a solution?

Any help would be most appreciated :)

#145578
Mar 07, 2016 14:53
Vote:
 

Tried reacting to the CreatedUrlSegment event?

Set up in initializable module.. 

public void Initialize(InitializationEngine context)
{
UrlSegment.CreatedUrlSegment += UrlSegmentOnCreatedUrlSegment;
}

private void UrlSegmentOnCreatedUrlSegment(
object sender, UrlSegmentEventArgs urlSegmentEventArgs)
{
var segment = urlSegmentEventArgs.RoutingSegment.RouteSegment;
...kill hyphens, shorten the urlsegment, to lower etc...
}

If that doesn't work let me know and I'll check some source code where I solved that one a few years ago.. 

#145582
Mar 07, 2016 15:56
Vote:
 

Thanks for the advice. Unfortunatly if im not mistaken the CreatedUrlSegment event is only fired when the segment is created the first time. I also need to act on urlsegment changes and if the url segement of the page is changed the event is not fired. 

Please check if you can find anything or if you have any other idea. 

Tahnks for the help!

#145586
Mar 07, 2016 17:59
Vote:
 

The evicting of cache entries for url segments are also triggered by events from IContentEvents. It appears that your event handler is registered before the event handler that evicts cache entries. To ensure your cache handler is registered after the one eveicting from cache a suggestion is to hook up your eventhandler inside an IInitializableModule.Initialize method that has a ModuleDependency attribute set to EPiServer.Web.InitializationModule.

#145615
Mar 08, 2016 11:44
Vote:
 

...or move logic to publishing event and check the updated item vs the one that already exist

private void InstancePublishingPage(object sender, PageEventArgs e)
{

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var contentReference = new ContentReference(e.Page.ContentLink.ID);
if (!ContentReference.IsNullOrEmpty(contentReference))
{
var languageSelector = new LanguageSelector(e.Page.LanguageID);
var page = contentLoader.Get<PageData>(contentReference, languageSelector);
if (page != null && page.URLSegment != e.Page.URLSegment)
{
var pages = _contentService.GetDescendents(page.PageLink, true, languageSelector);

...

...or install SEO Manager that will automatically create 301s if you do something like that. :)

#145617
Edited, Mar 08, 2016 11:59
Vote:
 

Thanks alot Johan that seems to work! 

You are right Daniel this should hopefully never happen but you never know with editors :)

#145636
Mar 08, 2016 16:53
Vote:
 

More likely they will move stuff around in the tree which will be the same problem from a SEO perspective.

#145641
Mar 08, 2016 17:53
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.