Within EpiServer edit how can I get the full path URL for an edited page from an Initialization Module?


I'm trying to extract the full URL for an edited page when a page gets changed by a site administrator in the edit mode. We are using EpiServer version 8 at this time.

Presently I have a ContentEventInitializer that executes code when a CheckedInContent event or a PublishedContent event occurs. This piece works, and now I need to extract the full URL, visible to end users, for the page that is being edited when these events occur. I saw the following post:


But when I attempted to apply this code from that post, a warning appeared regardingEPiServer.Configuration.Settings.Instance.SiteUrl being deprecated. And the suggested solution in Visual Studio of using EPiServer.Web.SiteDefinition breaks in the UriBuilder. You can see my attempt commented out below.

public static string ExternalURL(this PageData p)
    UrlBuilder pageURLBuilder = new UrlBuilder(p.LinkURL);
    Global.UrlRewriteProvider.ConvertToExternal(pageURLBuilder, p.PageLink, UTF8Encoding.UTF8);
    string pageURL = pageURLBuilder.ToString();
    UriBuilder uriBuilder = new UriBuilder(EPiServer.Configuration.Settings.Instance.SiteUrl);
    //UriBuilder uriBuilder = new UriBuilder(EPiServer.Web.SiteDefinition);
    uriBuilder.Path = pageURL;
    return uriBuilder.Uri.AbsoluteUri;

Also, given that the context this is executing in,within an InitializationModule, it's unclear to me how to handle the PageData parameter of that method. That's of course if indeed this approach is a viable way to get the friendly URL within an Initialization Module.

Thanks for your help.

Oct 07, 2016 19:39

UrlResolver class is your new Episerver 7+ friend for getting external urls. :)

Time for cheese and wine. Cheers:)

Oct 07, 2016 20:58

Thanks for the help.  I just returned from an extended weekend, so my apologies for the delayed reply.  The link to the ContentExternalUrl method gets me closer to the answer.  Right now it seems I'm struggling with the context of being in an event initializer.  Here are some of the moving parts from the helper class I've created.

public class ContentEventInitializer : IInitializableModule
    public void Initialize(InitializationEngine initializationEngine)
        if (IsMonitorNotificationEnabled)
            var events = ServiceLocator.Current.GetInstance<IContentEvents>();
            events.CheckedInContent += ExtendEvent.checkedInContent;
            events.PublishedContent += ExtendEvent.publishedContent;

That PublishedContent event enables me to intercept immediate publish events.  So when an admin edits a page, I'm able to successfully execute code within this method:

private static class ExtendEvent
    public static void publishedContent(object sender, ContentEventArgs contentEventArgs)
        // Get the full URL path for the page we just edited.
        // Send notification.

For clarity, a lot of code is stripped out of this. But you can see the moving parts, and the dilemma in my comment "Get the full URL path..."   I'm able to send a notification to the JSON service.  My problem now is just getting that full path URL for the page which an administrator just edited.  The contentEventArgs object in this publishedContent method doesn't contain a ContentReference object.  Inspecting the "sender" object that gets sent to this method, I'm not seeing a ContentReference object either.

So I'm guessing part of the answer resides upstream, within Initialize. But it's still unclear to me how I can extract the full path URL here.  Thanks for your patience, and for helping me out.

Edited, Oct 11, 2016 18:11


You have ContentLink and Content in contentEventArgs.

Oct 11, 2016 19:00

ContentEventArgs should have a ContentLink property. 


You can also cast that content event args to a child class to get some more information.

Then you can use UrlResolver class to get URL from content reference.


Edited, Oct 11, 2016 19:01

That makes sense now.  I was seeing ContentLink, which has a portion of the URL, and was under the mistaken impression that ContentEventArgs would return more of the URL than the view path.  But looking at the method from http://dodavinkeln.se/post/how-to-get-the-external-url-to-content, I see you're performing additional work to construct the full URI.

Johan, the method you wrote works like a champ.  I utilized most of it, with minor changes for this purpose.  In this case, I'm just passing ContentEventArgs.  And then adjusted these two lines as shown:

string result = ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl(contentEventArgs.ContentLink);
var hosts = siteDefinition.GetHosts(ContentLanguage.PreferredCulture, true).ToList();

Thank you both for all your help!

Oct 11, 2016 19:48

Great! Remember that not all content will have URL as well. Blocks, Content folders etc :)

So handle the null case as well...

Happy coding!

Oct 11, 2016 19:54
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.