Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roger Withnell 128 posts 613 karma points
    Jul 03, 2017 @ 13:06
    Roger Withnell
    0

    Problem using NiceUrl

    When a page is published, I'm using contentServicePublish.cs to send an email to a distribution list.

    In the email, I want to give a link back to the page that was updated when it was published.

    In contentServicePublish.cs, I'm getting the node of the page as follows:

        public class contentServicePublish : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ContentServicePublished;
        }
    
        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
        {
            foreach (var node in args.PublishedEntities)
            {
                Node vNode = new Node(node.Id);
    

    And then put the link to the page in the email:

                                    vBody += "<h3><a href=" + vNode.NiceUrl + ">Get more information</a></h3>";
    

    This works most of the time but intermittently fails. When it does fail, the HTML in the email source is:

    <h3 id="m_-1263032811582594055yui_3_16_0_1_1498546437749_52456"><a rel="nofollow" id="m_-1263032811582594055yui_3_16_0_1_1498546437749_52455">Get more information</a></h3>
    

    What am I doing wrong here?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jul 03, 2017 @ 13:16
    Dan Diplo
    1

    What version of Umbraco are you using? The Node class was deprecated a long time ago.

    In Umbraco 7 you should use Umbraco.TypeContent(node.Id) to get an instance of a node and then you can use the extension vNode.UrlAbsolute() to get the full, absolute URL.

    Your intermittent issue, though, may well be because of the lag between a page being published (ie. appearing in the database as published content) and the time it takes to actually generate the XML and refresh the cache. Umbraco uses the XML cache to get Published Content items - if the XML hasn't been generated and the cache updated in time then it won't have a URL.

  • Roger Withnell 128 posts 613 karma points
    Jul 09, 2017 @ 15:42
    Roger Withnell
    0

    Thanks, Dan.

    As a newbie, I'm being a bit dumb but Umbraco.TypeContent(node.Id) is not available on the page.

    What am I doing wrong?

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Jul 09, 2017 @ 17:31
    Marc Goodson
    2

    Hi Roger

    If you were in a template or a class inheriting from a special Umbraco base class (eg SurfaceController, RenderMvcController) you would have access to the Umbraco Helpers that Dan is describing via

    Umbraco.
    

    but as you are writing code in an application event handler you would need to create your own new Umbraco Helper instance:

      var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
       foreach (var node in args.PublishedEntities)
            {                  
      var publishedContent = umbracoHelper.TypedContent(node.Id);
    
                    var strUrl = publishedContent.Url;
    

    regards

    Marc

  • Roger Withnell 128 posts 613 karma points
    Jul 13, 2017 @ 09:49
    Roger Withnell
    100

    Thanks, Marc.

    Used the code as you suggested. Now waiting to see if the intermittent problem, described in the initial post, has been fixed.

Please Sign in or register to post replies

Write your reply to:

Draft