Alphabetically auto-sorting media item child nodes
I am trying to auto-sort a child nodes of a media item alphabetically
i.e. Whenever I add a media item child node, it gets added to the bottom of the list. However, I want that after I save the media item child node; the list should automatically get alphabetically updated(updated by property "givenName"). I am attaching the code snippet I have created so far. Please let me know if this is correct?
Alphabetically auto-sorting media item child nodes
I am trying to auto-sort a child nodes of a media item alphabetically
i.e. Whenever I add a media item child node, it gets added to the bottom of the list. However, I want that after I save the media item child node; the list should automatically get alphabetically updated(updated by property "givenName"). I am attaching the code snippet I have created so far. Please let me know if this is correct?
public class AutoSort : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
MediaService.Saved += MediaService_Saved;
}
private void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e)
{
try
{
foreach (var mediaitem in e.SavedEntities)
{
var parentnode = mediaitem.Parent();
var parentid = parentnode.Id;
var childnodes = parentnode.Children();
var staffprjnodes = new List<StaffPrjNode>();
var staffnewprjnodes = new List<StaffPrjNode>();
foreach (var childnode in childnodes)
{
int childid = childnode.Id;
string givenName = "";
string name = childnode.Properties.Where(property => property.Alias == "givenName").ToString();
if (!string.IsNullOrEmpty(name))
{
staffprjnodes.Add(
new StaffPrjNode
{
Id = childid,
GivenName = name
});
}
}
if (staffprjnodes.Count > 1)
{
var sortorder = "";
staffnewprjnodes= staffprjnodes.OrderBy(x => x.GivenName).ToList();
foreach (var staffnewprjnode in staffnewprjnodes)
sortorder += staffnewprjnode.Id + ",";
var nodesorter = new umbraco.presentation.webservices.nodeSorter();
nodesorter.UpdateSortOrder(parentid,sortorder.Trim(','));
}
}
}
catch(Exception)
{
Logging.LogMessage("Not working");
}
}
}
public class StaffPrjNode
{
public int Id { get; set; }
public string GivenName { get; set; }
}
}
is working on a reply...