Attaching elements

Step 10 – Adding Children to Tree

And now, the fun part: adding the children to the tree. Here is the new version of our ElementAttachingProvider:

[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"); } }
  // ...the code skipped for readability - see Steps 3 and 7
  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 and 7
      }
      else
      {
        MyEntityToken myEntityToken = parentEntityToken as MyEntityToken;
        if (myEntityToken == null) throw new NotImplementedException();
        IEnumerable<IPage> pages;
        ResourceHandle icon;
        if (myEntityToken.Id == "Approval")
        {
          pages = connection.Get<IPage>().
            Where(f => f.PublicationStatus == "awaitingApproval");
          icon = DataAwaitingApproval;
        }
        else if (myEntityToken.Id == "Publication")
        {
          pages = connection.Get<IPage>().
            Where(f => f.PublicationStatus == "awaitingPublication");
          icon = DataAwaitingPublication;
        }
        else
        {
          throw new NotImplementedException();
        }
        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
            }
          };
          yield return element;
        }    
      }
    }
  }
  private static ResourceHandle GetIconHandle(string name)
  {
    return new ResourceHandle(BuildInIconProviderName.ProviderName, name);
  }
}

As mentioned in Step 5 (and Step 8) we have constructed our entity tokens for our children with some extra information. Now it’s time to use this information.

By the construct, we know that the element “Pages awaiting approval” has an entity token with the Id value of “Approval”. So when our GetChildren method has to find children for an element with an entity token of the MyEntityToken type, we can use the Id property value of this entity token to see what pages should be children. We also use this value to find the right icon. As listed in the code, if the Id property value is “Approval”, we take all the pages in the “Awaiting approval” state – the same goes for “Publication”.

Lastly, we create the actual elements. This is again done with the Context, but this time we get the entity token from the page (data item) itself.

Now we are done with the tree, which was one of the goals of this tutorial. Try to add some pages and change their publication status back and forth and note that the tree automatically refreshes. C1 CMS does this by default and uses the security relations to determine how to refresh the tree. If your elements don’t show up, refresh them manually.