Try our conversational search powered by Generative AI!

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

Vote:
 

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:

http://world.episerver.com/Forum/Developer-forum/EPiServer-CMS-6-R2/Thread-Container/2012/9/Absolute-external-URL-property/


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.

#161201
Oct 07, 2016 19:39
Vote:
 

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

Time for cheese and wine. Cheers:)

#161204
Oct 07, 2016 20:58
Vote:
 

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.

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEventInitializer : IInitializableModule
{
    public void Initialize(InitializationEngine initializationEngine)
    {
        SetIsMonitorNotificationEnabled();
        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.

#162154
Edited, Oct 11, 2016 18:11
Vote:
 

Hi,

You have ContentLink and Content in contentEventArgs.

#162156
Oct 11, 2016 19:00
Vote:
 

ContentEventArgs should have a ContentLink property. 

http://world.episerver.com/documentation/class-library/?documentId=cms/7/b201d6a5-f503-9263-7ed1-f9624a164f9f

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.

http://world.episerver.com/documentation/Class-library/?documentId=cms/9/1B13DC78

#162158
Edited, Oct 11, 2016 19:01
Vote:
 

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!

#162162
Oct 11, 2016 19:48
Vote:
 

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

So handle the null case as well...

Happy coding!

#162163
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.