Home > C#, Sitecore, WFFM > WFFM publishing with related item, fixing missing form elements

WFFM publishing with related item, fixing missing form elements

When adding a form to a page via the page editor, Sitecore registers a reference to form element, so when you publish you can check the publish related Items checkbox, and you would expect the form to be visible on your CD-server

29-10-2014 11-22-29

However WFFM places all elements in the form as children to form element itself, and these are not added to the publishing queue because they are not related to page where the form was first added.

29-10-2014 11-23-32

So when publishing the form is correctly being published, but the fields for the forms isn’t moved from the master database to web database and the form is of course then not rendered out.

As always Sitecore has a pipeline, that you can hook into in this case the “getItemReferences” this pipelines, resolves related items and adds them to the publishing queue. So hooking into this and adding child elements for forms is actually quite easy.


public class GetFormChildElementProcessor : GetItemReferencesProcessor
{
  private readonly ID _formTemplateId = new ID("{FFB1DA32-2764-47DB-83B0-95B843546A7E}");
 protected override List<Item> GetItemReferences(PublishItemContext context)
 {
  List<Item> relatedformItems = new List<Item>();
  if(!context.Result.ReferredItems.Any())
   return relatedformItems;
  Database database = context.PublishContext.PublishOptions.SourceDatabase;
  var formItems = context.Result.ReferredItems.Where(i=>IsDerived(database.GetItem(i.ItemId).Template, _formTemplateId)).ToList();

  foreach (var form in formItems)
  {
    var formChildren = form.ChildEntries;
    foreach (var child in formChildren)
    {
      relatedformItems.Add(database.GetItem(child.ItemId));
    }
  }
  return relatedformItems;
}

 public bool IsDerived(TemplateItem template, ID templateId)
 {
  if (!(template.ID == templateId))
  {
   return Enumerable.Any<TemplateItem>(template.BaseTemplates, baseTemplate => IsDerived(baseTemplate,templateId));
  }
   return true;
 }
}

And now register the processor in the web.config  under /configuration/sitecore/pipelines/getItemReferences add it at the end.

<processor type="PT.Framework.PublishForm.GetFormChildElementProcessor, PT.Framework.PublishForm"/>

Now all the child elements to items the derives from the form element will be added to the publishing queue.

Categories: C#, Sitecore, WFFM Tags: , ,
  1. 26/11/2014 at 15:31

    Hi Thomas! This is a great solution. I implemented this but it only publishes the items directly below the form. If there is a form section with form fields, it only publishes the form sections but not it’s children. I updated it so it checks whether the “child in formChildren” has any ChildEntries and add that as well to the list. Let me know if there is a better way to do it.

    Thanks,
    Chitrang

    • 29/11/2014 at 20:27

      Hi yes of course you are right it needs to take all descendants for the form root element. Maybe a xpath query would be a good solution to this. Otherwise as you said, just check if the child is a form section. Thanks for the update.

  1. No trackbacks yet.

Leave a reply to Chitrang Cancel reply