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.
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.
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.
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.
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 want something like the below imge.
Appreciate any help.
Thank you in advance.
Kind Regards
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.
Are there any documentation on how I can call the umbraco event Content Published?
Ideally, the event would be exactly on publish.. so that I can overrload value and just save normally.
Are there a list of events?
You can create an OnApplicationStarted method and then add a handler for the Published event
and then create the method
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
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 thePublished
event. Try something like :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.
is working on a reply...