Copied to clipboard

Flag this post as spam?

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


  • Simon 692 posts 1068 karma points
    Feb 05, 2016 @ 12:13
    Simon
    0

    Custom List View

    Hi Guys,

    Good Afternoon.

    Hope your well.

    I have such dilemma about how can I include a column in a list view to show.

    I have this scenario:

    • I have a parent node showing the children in a list view. Now each children (which is an agent) can be related with one to many buidlings.
    • Now I want an extra column in the child Items view, where to show these agents, where I can set the amount of buildings have related with... I am using nuPickers for relationship.

    I want something like the below imge.

    enter image description here

    Appreciate any help.

    Thank you in advance.

    Kind Regards

  • Barry Fogarty 493 posts 1129 karma points
    Feb 05, 2016 @ 12:48
    Barry Fogarty
    100

    Hi Simon,

    Create a label field in your agent document type called numberOfBuildings (or something). Then you'll need to write some code the executes on the Umbraco Content_Published event. In there, essentially you will check if the current node is an Agent document type, and if so, get the count of building relations from your nuPicker property. Update the value of your numberOfBuildings property with this value.

    Then its a simple case of adding your numberOfBuildings property to the list of columns in your ListView.

  • Simon 692 posts 1068 karma points
    Feb 05, 2016 @ 12:49
    Simon
    0

    Are there any documentation on how I can call the umbraco event Content Published?

  • Simon 692 posts 1068 karma points
    Feb 07, 2016 @ 13:41
    Simon
    0

    Ideally, the event would be exactly on publish.. so that I can overrload value and just save normally.

    Are there a list of events?

  • Veronica Burd 76 posts 201 karma points
    Feb 08, 2016 @ 09:00
    Veronica Burd
    1

    You can create an OnApplicationStarted method and then add a handler for the Published event

    Umbraco.Core.Services.ContentService.Published += ContentService_Published;
    

    and then create the method

        private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            foreach(IPublishedContent p in e.PublishedEntities)
            {
                 //check type of document
                if(p.ContentType.Alias == 'Agent')
                {
                     //logic to update count field
                }
            }
        }
    

    However, I'd prefer to do any changes to data via the Saving event since you'd already have access to the IContent of the Agent document and can avoid a second save operation.

    Here's the link to the Event documentation https://our.umbraco.org/documentation/Reference/Events/

    HTH

    Ver

  • Barry Fogarty 493 posts 1129 karma points
    Feb 08, 2016 @ 10:02
    Barry Fogarty
    0

    Hi Simon, I agree with Veronica's contribution that it's better to use the Saving event as it avoids having to do a second save in the Published event. Try something like :

        /// <summary>
        /// Set number of buildings property on Agent nodes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
        {
            foreach (IContent entity in e.SavedEntities)
            {
                if (entity.HasProperty("buildingRelations"))
                {
                    var numberOfBuildingRelations = entity.GetValue<IEnumerable<IPublishedContent>>("buildingRelations").Count();
                    entity.SetValue("numberOfBuildings", numberOfBuildingRelations);
                }
            }
        }
    

    NB This is untested and I am just assuming you can get a collection of nodes as the value of your nuPicker relations property. Once you can get some form of collection though you should be able to get the count.

Please Sign in or register to post replies

Write your reply to:

Draft