Contact us on: +44 (0)1432 617 006

The Site Doctor Blog

Footprints in the snow of a warped mind

< Back to Blog

Group Newsletter Studio mailing lists by name in the Umbraco backend tree

In a follow-on from my previous post about how to Group Newsletter Studio newsletters by month in the Umbraco backend tree. This is a quick post to give you an idea of how to group the mailing lists by name.

As with the newsletters we have a load of different mailing lists. By default Newsletter Studio outputs the lists as they were created which isn't great so we needed to sort them alphabetically but also pop them into folders.

We used the naming convention of Department - List Name so were able to simply group the nodes based on the bit before the hyphen which then groups them nicely. We've got about 100 in the tree which ends up being 10 folders each with around 10 items in.

Here's a little screenshot of what it was and what it is now:


Before:
image

After:
image


As with the newsletter tree, this was just a quick "get it out there ASAP" change so I'm sure the code can be cleared up. Welcome any comments.

Again with the installation, you'll need to add the class to your solution and then modify the `/config/trees.config` file:

<add title="Newsletters" initialize="true" sortorder="5" alias="Newsletter" application="NewsletterStudio" iconclosed="icon-message" iconopen="icon-message" type="YourProject.NewsletterStudio.CustomMailingListTreeController, YourProject"></add>


using System.Linq;
using System.Net.Http.Formatting;
using NewsletterStudio.Infrastucture;
using NewsletterStudio.Infrastucture.Services;
using NewsletterStudio.Umbraco;
using umbraco.BusinessLogic.Actions;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees;

namespace YourProject.NewsletterStudio
{
	[PluginController(NewsletterStudioApplication.Name)]
	[Tree("NewsletterStudio", "MailingList", "Mailing lists", "icon-folder", "icon-folder-open", true, 10)]
	public class CustomMailingListTreeController : TreeController
	{
		private LocalizationService _localization;

		public CustomMailingListTreeController()
		{
			this._localization = new LocalizationService();
		}

		protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
		{
			TreeNodeCollection treeNodeCollection = new TreeNodeCollection();
			var lists = GlobalFactory.Current.MailingListRepository.GetAll();
			var listNames = lists.Select(l => GenerateNameParts(l.Name));

			if (!id.StartsWith("folder"))
			{
				var parts = listNames.Select(n => n.FirstOrDefault()).Distinct().OrderBy(l => l);
				foreach (var part in parts)
				{
					var treeNode = CreateTreeNode($"folder-{part}", id, queryStrings, part, "icon-folder", true);
					treeNodeCollection.Add(treeNode);
				}
			}
			else
			{
				var key = id.Replace("folder-", string.Empty);
				foreach (var mailingList in lists.Where(l => l.Name.StartsWith(key)))
				{
					var treeNode = CreateTreeNode(mailingList.Id.ToString(), id, queryStrings, GetCleanName(key, mailingList.Name), "icon-users", false);
					treeNodeCollection.Add(treeNode);
				}
			}
			return treeNodeCollection;
		}

		private string GetCleanName(string key, string mailingListName)
		{
			if (mailingListName.Length > key.Length)
				return mailingListName.Substring(key.Length).Trim(' ', '-');

			return mailingListName;
		}

		private string[] GenerateNameParts(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
				return new[] { "(empty)" };

			return name.Split('-').Select(p => p.Trim()).ToArray();
		}

		protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
		{
			MenuItemCollection menuItemCollection = new MenuItemCollection();
			if (id == "-1")
			{
				menuItemCollection.DefaultMenuAlias = ActionNew.Instance.Alias;
				menuItemCollection.Items.Add(_localization.Get("actions_create"));
				menuItemCollection.Items.Add(_localization.Get("actions_refreshNode"));
			}
			else
				menuItemCollection.Items.Add(_localization.Get("actions_delete"));
			return menuItemCollection;
		}
	}

}
Author: Tim on

Liked this post? Got a suggestion? Leave a comment