Attaching elements

Step 7 – Implementing MyElementAttachingProvider Class

The last step of adding some children to our root element is done in the MyElementAttachingProvider class. Here is the new version:

[ConfigurationElementType(typeof(NonConfigurableElementAttachingProvider))]
public sealed class MyElementAttachingProvider : IElementAttachingProvider
{
  // ...the code skipped for readability - see Step 3
  public IEnumerable<Element> GetChildren(EntityToken parentEntityToken, 
                      Dictionary<string, string> piggybag)
  {
    using (DataConnection connection = new DataConnection())
    {
      if ((parentEntityToken is MyRootEntityToken) == true)
      {
        yield return new Element(this.Context.CreateElementHandle(
          new MyEntityToken("Approval")))
        {
          VisualData = new ElementVisualizedData
          {
            Label = "Pages awaiting approval",
            ToolTip = "Pages awaiting approval",
            HasChildren = connection.Get<IPage>().
                  Where(f => f.PublicationStatus == "awaitingApproval").
                  Any(),
            Icon = CommonElementIcons.Search
          }
        };
        yield return new Element(this.Context.CreateElementHandle(
          new MyEntityToken("Publication")))
        {
          VisualData = new ElementVisualizedData
          {
            Label = "Pages awaiting publication",
            ToolTip = "Pages awaiting publication",
            HasChildren = connection.Get<IPage>().
                  Where(f => f.PublicationStatus == "awaitingPublication").
                  Any(),
            Icon = CommonElementIcons.Search
          }
        };
      }
      else
      {
        throw new NotImplementedException("Will be implemented later in the tutorial"); 
      }
    }
  }
}

As all the new stuff is inside the GetChildren method, let’s take a closer look at it.

Again, like with the root element, we use the Context to create the element handle, but this time it’s with our new entity token type. We feed the constructor of this entity token with values of “Approval” and “Publication” respectively. We will use these values later on in the tutorial. Another thing to note here is that we dynamically set the HasChildren property depending on whether any pages are awaiting approval or publication respectively.

Now we have to add children to our root element with security and all in place. Next, we should add children to our children.