Copied to clipboard

Flag this post as spam?

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


  • Mark Arndt Lønquist 20 posts 88 karma points
    May 20, 2016 @ 13:46
    Mark Arndt Lønquist
    0

    Hi

    I seem to have a few difficulties with using "Published" event;

    ContentService.Published += ContentServicePublished;
    

    It seems, that if the entity is new, I'm unable to get the URL of the node. Shouldn't the data be in the cache once Published gets triggered?

    I'm doing it like this:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var contentUrl = umbracoHelper.UrlAbsolute(content.Id);
    

    but contentUrl is empty, if the entity is new.

    Any help is appreciated.

  • MarcC 49 posts 350 karma points
    May 20, 2016 @ 15:10
    MarcC
    0

    Had a quick look and I believe its unable to retrieve the url because it doesnt exist until the item is saved post publish somewhere?

    The node ID is created though.

    Unless you have a url write on it, maybe use this to generate what the url will be with :

    var urlName = umbracoHelper.NiceUrlWithDomain(publishedItem.ParentId) + publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
    

    Probably very hacky until a real solution is found. This may cause an error with new items created at root so retrieving the root url and appending in that case might be best.

    Ill have a further look into it just now and see if I can see a more elegant solution.

  • MarcC 49 posts 350 karma points
    May 20, 2016 @ 16:05
    MarcC
    0

    Created a new class in the app code, this seems to work on the small tests I've done. Hope it might help you out until you find a more elegant (correct) solution.

    public class ContentNewService : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ContentServicePublished;
        }
    
        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
            foreach (var publishedItem in e.PublishedEntities)
            {
                string contentUrl = "";
                string root = HttpContext.Current.Request.Url.ToString();
    
                //check if published before
                if (umbracoHelper.UrlAbsolute(publishedItem.Id) != "#")
                {
                    contentUrl = umbracoHelper.UrlAbsolute(publishedItem.Id);
                }
                else
                {
                    //Check if published at root
                    if (publishedItem.ParentId == -1)
                    {
                        contentUrl = root + publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
                    }
                    else
                    {
                        contentUrl = umbracoHelper.UrlAbsolute(publishedItem.ParentId) +
                                     publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
                    }
                }
    
                //Success message
                e.Messages.Add(new EventMessage("Success", "Url:'" + contentUrl +"'", EventMessageType.Success));
            }
        }
    
    
    }
    
  • Mark Arndt Lønquist 20 posts 88 karma points
    May 21, 2016 @ 14:35
    Mark Arndt Lønquist
    0

    Thanks man! It might be a hacky solution, but it seems to be how I need to do it right now.

  • MarcC 49 posts 350 karma points
    May 23, 2016 @ 07:34
    MarcC
    0

    No worries man, hopefully someone who has a little more experience could chime in with a better solution.

    Atb,

Please Sign in or register to post replies

Write your reply to:

Draft