Tag Selector in widgets properties

Novice User asked on April 19, 2018 00:03

Hello,

I am building a widget in which i want user to enter tags so data can be filtered based on tags . Is there a tag selector for control available or not. I saw tag group selector but not tag selector. Am i missing some thing here?

Thanks.

Correct Answer

Peter Mogilnitski answered on April 19, 2018 01:00

If you are talking about form controls, there is a tag selector form control, that provides a text field for entering tags and a selector for choosing from already existing tags related to the place where this form control is inserted (e.g., when inserted into the Product page type, only tags from the Products tag group will be displayed in the list).

Image Text

By default the scope is set Page types, but you can just check controls and web parts and it should be available for your widget.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on April 19, 2018 00:51

May not have one out of the box as usually tags need a tag group that is usually attached to the page, however nothing is stopping you from using a multiple choice form control and writing a custom query to show the tags, or use the uni selector and configure it to allow the selection of the tags!

Need help writing the query? On mobile so don't have the database in front of me, but probably something like "select tag, tag as displayvalue from cms_documenttags"

1 votesVote for this answer Mark as a Correct answer

Novice User answered on April 19, 2018 21:43 (last edited on April 19, 2018 22:27)

Thanks Peter Mogilnitski for the fix. Tag selector did become available in my widget. But When i put widget on any page , tag selector is disabled as no tag selector group is assigned. How and where can i define the default tag group selector. Any workaround on this?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 20, 2018 03:16 (last edited on April 20, 2018 12:46)

There is a simple solution and a bit more advanced solution.

Simple:

  1. Go to for form controls
  2. New Form Control, call it - MyTagSelector
  3. Inherit From Existing - pick tag selector
  4. Set available for widgets and other settings
  5. Go to Properties - there is only one - TagGroupId
  6. Set default value to whatever group id you need (select TagGroupID, TagGroupDisplayName from CMS_TagGroup )
  7. Now go to you widget and use MyTagSelector instead of Tag Selector

Advanced:

You need to take a look at the code and debug the tag selector form control \CMSModules\Content\FormControls\Tags\TagSelector.ascx.cs Clone it (copy/paste the code) as CustomTagSelector and customized for you needs. Then use it in your widget the same way instead of original one.
The code is pretty straight forward - You need to set correct GroupId. If it is 0 - selector is disabled.

protected void Page_PreRender(object sender, EventArgs e)
{
    if (GroupId == 0)
    {
        Enabled = false;
        btnSelect.ToolTip = ResHelper.GetString("tags.tagsselector.assigntaggroup");
        txtTags.ToolTip = ResHelper.GetString("tags.tagsselector.assigntaggroup");
    }

    // Enable / Disable control
    txtTags.Enabled = Enabled;
    btnSelect.Enabled = Enabled;
    if (Enabled)
    {
        autoComplete.ContextKey = GroupId.ToString();
    }  
}

Where property GroupId is defined like this:

public int GroupId
    {
        get
        {
            int mGroupId = ValidationHelper.GetInteger(GetValue("TagGroupID"), 0);
            if ((mGroupId == 0) && (Form != null))
            {
                TreeNode node = (TreeNode)Form.EditedObject;

                // When inserting new document
                if (Form.IsInsertMode)
                {
                    var parent = Form.ParentObject as TreeNode;
                    if (parent != null)
                    {
                        // Get path and groupID of the parent node
                        mGroupId = parent.DocumentTagGroupID;
                        // If nothing found try get inherited value
                        if (mGroupId == 0)
                        {
                            mGroupId = ValidationHelper.GetInteger(parent.GetInheritedValue("DocumentTagGroupID", false), 0);
                        }
                    }
                }
                // When editing existing document
                else if (node != null)
                {
                    // Get path and groupID of the parent node
                    mGroupId = node.DocumentTagGroupID;
                    // If nothing found try get inherited value
                    if (mGroupId == 0)
                    {
                        mGroupId = ValidationHelper.GetInteger(node.GetInheritedValue("DocumentTagGroupID", false), 0);
                    }
                }
            }

            return mGroupId;
        }
        set
        {
            SetValue("TagGroupID", value);
        }
    }

My suggestion - clone it, so you always have the original code. You can add a new field to your widget call it tag group, so you will have something like this. Make it as drop down based on query like select TagGroupID, TagGroupDisplayName from CMS_TagGroup where ... So you always have a first group selected by default. And then you can access TagGroup value inside your ClonedTagSelector, i.e.

 public int GroupId
    {
        get
        {
            if (Form.FieldControls["TagGroup"] != null)
            {
                if (ValidationHelper.GetString(Form.FieldControls["TagGroup"].Value,"") != "")
                {
                    return ValidationHelper.GetInteger(Form.FieldControls["TagGroup"].Value, 0);
                }
            }
            ...

P.S. I cant really dig into details here, but it should give you an idea how to approach all this.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.