Click or drag to resize

How To implement a tag field

Many applications require tagging data in a convenient way. This tutorial explains how to easily achieve such functionnality within Packflow, using modeling features with minimal customization.

A simple Todo application is used as an example.

We'll model a content-type to store Tags, add a choice targeting it in Todo content-type. Then, in custom code, we'll add the 'Tag' field in every quick search query, to ensure convenient search within tags.

This topic contains the following sections:

Modeling the Tag content-type
  1. In Packflow Designer, open your application model.
  2. Create a new content-type. Name it 'Tag'.
  3. Edit default view to only show the 'Title' field.
  4. Create a 'Many to Many' relation from TodoItem to Tag content-types. Name it 'Todo_Tag'.
  5. In Todo content-type, create a choice field, Name it 'Tag'.
    • In 'Relation', select previously created relation 'Todo_Tag'.
    • In 'Control' panel, select 'Auto Complete', and 'Close after selection'.
    • In 'Control' panel, select 'Show Creation button, and 'Simple form'.
  6. Add the 'Tag' field to the 'TodoItem' content-type form.
  7. Generate your application.
Quick search customization

Quick Search is the search feature available in all grid views and navigation pages. By default, it searches through a set of pre-defined columns (Id, Title) plus the displayed columns.

It is possible to override this behavior on any view, to add or remove columns from the default selection. We'll use this API feature to add the 'Tag' column to every quick search made on TodoItem content-type.

  1. Open your application project in Visual Studio.
  2. Open the 'view of application' class file at (YourProjectFolder)\Objects\Views\_ViewOfApplication_YourAppName.Custom.cs
  3. Override the GetQuickSearchFields method to add the 'Tag' field when applicable.

    C#
    public partial class ViewOfApplication_Todo : PFView
    {
        public override List<PFField> GetQuickSearchFields()
        {
            List<PFField> quickSearchFields = base.GetQuickSearchFields();
            //If view targets Todo content-type, and Tag field is not selected, add Tag 
            //to quick search fields.
            PFContentType todoCt = this.ContentTypes.FirstOrDefault(ct => ct.Name == Application_Todo.ContentTypeName_TodoItem);
            if (todoCt != null)
            {
                PFField tagField = todoCt.Fields.GetByName(ContentType_TodoItem.PFFieldName_Tag);
    
                //Names may not be unique because of multiple possible target content-types.
                //Modeling guid is a good reference in this case.
                if (!quickSearchFields.Any(field => field.ModelColumnGuid == tagField.ModelColumnGuid))
                    quickSearchFields.Add(tagField);
            }
            return quickSearchFields;
        }
    }
  4. You may now build and deploy your application.

Since every view in this application uses this class, now every quick search targetting 'Todo' content-type will consume this implementation and look in 'Tag' field. This remains true for runtime views.

Result

When editing a Todo item, it possible to conveniently select and add tags.

When searching for tag terms in navigation or grid views, the 'tag' field is always selected by default.

Below, you can see the effect of a quick search filter in navigation. Note the 'Tag' field is not in the view's columns.

After applying quick search, our initial todo 'Have a walk' is found in view.