Copied to clipboard

Flag this post as spam?

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


  • Tommo1977 13 posts 74 karma points
    Feb 16, 2015 @ 14:02
    Tommo1977
    0

    Get Document by Node Id in ContentService.Published

    I have added a application event handler so I can catch when a document is Saved/Published.

    public class UmbracoEvent : ApplicationEventHandler
    {
           protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {
    ContentService.Published += ContentServicePublished;
            }

            private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs args)
            {
    //Here I want to get a Umbraco Field value that exists in the Root document/template.
            }

    }

    How can I get and read the value for the saved Node(s), and get the Umbraco Field value ?  I'm wanting to get the value of the property from the database, not the cache.

  • Bogdan 250 posts 427 karma points
    Feb 16, 2015 @ 14:50
    Bogdan
    100

    Hi Tommo,

    Try:

    private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
       e.PublishedEntities <-- holds the published content
    }
    
  • Tommo1977 13 posts 74 karma points
    Feb 16, 2015 @ 15:02
    Tommo1977
    0

    Thank you.  I managed to access the required property field using

    args.PublishedEntities.First().Properties["hotelId"].Value

  • Mario Patrao 8 posts 28 karma points
    Mar 27, 2015 @ 12:52
    Mario Patrao
    0

    Hi,

    When I try to do e.PublishedEntities.First().Properties["isAChannel"].Value I get the error:

    'System.Collections.Generic.IEnumerable' does not contain a definition  for 'First' and extension method 'First' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could not be found (are you missing a using directive or an assembly reference?)

    This is my code by the way

    void Document_AfterPublish2(IPublishingStrategy sender, PublishEventArgse)
            {
                var Alias = "";
                foreach (var entity in e.PublishedEntities)
                {
                    Alias = entity.ContentType.Name;

                }

                var isChannel= e.PublishedEntities.First().Properties["isAChannel"].Value;           

    }

     Am I missing something about his issue?

     

    Thanks

  • Bogdan 250 posts 427 karma points
    Mar 27, 2015 @ 14:08
    Bogdan
    0

    Hi Mario,

    How did you subscribe to the Published event? It should be from ContentService: ContentService.Published += Document_AfterPublish2;

    In case < IContent> was stripped out by this text editor, try to replace PublishEventArgs e with PublishEventArgs < IContent> e:

    private void Document_AfterPublish2(IPublishingStrategy sender, PublishEventArgs< IContent> e) {

    }

  • Mario Patrao 8 posts 28 karma points
    Mar 27, 2015 @ 14:52
    Mario Patrao
    0

    Hi Bogdan,

    It was jus this text editor that stripped the <IContent>. Yes I'm using the Publish event like this:

    public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += Document_AfterPublish2;
            }

    I'm just thinking if I'm missing a reference or something?

     

  • Bogdan 250 posts 427 karma points
    Mar 27, 2015 @ 14:58
    Bogdan
    0

    Oh yeah, First() is an extension from System.Linq, so add using System.Linq;

  • Mario Patrao 8 posts 28 karma points
    Mar 27, 2015 @ 15:07
    Mario Patrao
    0

    Bogdan,

    Yes I was missing that reference duh :D

    Thank you very much. All is working fine now!

Please Sign in or register to post replies

Write your reply to:

Draft