Attaching elements

Step 14 - Adding URL Action to Page Elements

Adding the URL action to the page elements is done in our MyElementAttachingProvider and it now looks like this:

[ConfigurationElementType(typeof(NonConfigurableElementAttachingProvider))]
public sealed class MyElementAttachingProvider : IElementAttachingProvider
{  
   private static ResourceHandle DataAwaitingApproval { get { 
    return GetIconHandle("page-awaiting-approval"); } }
  private static ResourceHandle DataAwaitingPublication { get { 
    return GetIconHandle("page-awaiting-publication"); } }
  private static readonly ActionGroup ActionGroup = 
    new ActionGroup("My tasks", ActionGroupPriority.PrimaryLow);
  // ...the code skipped for readability - see Step 3, 7 and 10
  public IEnumerable<Element> GetChildren(EntityToken parentEntityToken, 
                      Dictionary<string, string> piggybag)
  {
    using (DataConnection connection = new DataConnection())
    {
      if ((parentEntityToken is MyRootEntityToken) == true)
      {
         // ...the code skipped for readability - see Step 3, 7 and 10
      }
      else
      {
         // ...the code skipped for readability - see Step 3, 7 and 10
        foreach (IPage page in pages)
        {
          Element element = 
           new Element(this.Context.CreateElementHandle(page.GetDataEntityToken()))
          {
            VisualData = new ElementVisualizedData
            {
              Label = page.Title,
              ToolTip = page.Title,
              HasChildren = false,
              Icon = icon
            }
          };
          element.AddAction(new ElementAction(new ActionHandle(new MyUrlActionToken()))
          {
            VisualData = new ActionVisualizedData
            {
              Label = "My Url Action",
              ToolTip = "My Url Action",
              Icon = CommonCommandIcons.ShowReport,
              ActionLocation = new ActionLocation
              {
                 ActionType = ActionType.Other,
                 IsInFolder = false,
                 IsInToolbar = true,
                 ActionGroup = ActionGroup
              }
            }
          });
          yield return element;
        }    
      }
    }
  }
  private static ResourceHandle GetIconHandle(string name)
  {
    return new ResourceHandle(BuildInIconProviderName.ProviderName, name);
  }
}

We use the Element.AddAction method to add our action and uses our MyUrlActionToken to specify that it is our URL action that we want to be executed. When you have done this and refreshes C1 CMS you should be able to see a new action when selecting a page in our attached tree. Note that this action is not on the pages in the original tree. When clicking on the new action a new tab window opens and the title of the page is shown.