Looking for info on API for adding Tags and assigning tags

David Pearson asked on February 19, 2015 19:29

I am attempting to import News Articles from Sitefinity.
- I am able to get the articles with assign tags. - I am able to insert the articles into the Kentico Tree

I am still researching how to assign the tags to the article once imported into Kentico. I am guessing I will be using TagHelper, DocumentTagInfoProvider etc.... Is there a how to somewhere on this?

Thanks David

Correct Answer

Brenden Kehren answered on February 19, 2015 22:11

Right from the API Examples documentation, should get you in the right direction:

Create a tag group

private bool CreateTagGroup()
{
    // Create new tag group object
    TagGroupInfo newGroup = new TagGroupInfo();

    // Set the properties
    newGroup.TagGroupDisplayName = "My new group";
    newGroup.TagGroupName = "MyNewGroup";
    newGroup.TagGroupDescription = "";
    newGroup.TagGroupSiteID = SiteContext.CurrentSiteID;
    newGroup.TagGroupIsAdHoc = false;

    // Create the tag group
    TagGroupInfoProvider.SetTagGroupInfo(newGroup);

    return true;
}

Get and update a tag group

private bool GetAndUpdateTagGroup()
{
    // Get the tag group
    TagGroupInfo updateGroup = TagGroupInfoProvider.GetTagGroupInfo("MyNewGroup", SiteContext.CurrentSiteID);
    if (updateGroup != null)
    {
        // Update the property
        updateGroup.TagGroupDisplayName = updateGroup.TagGroupDisplayName.ToLower();

        // Update the tag group
        TagGroupInfoProvider.SetTagGroupInfo(updateGroup);

        return true;
    }

    return false;
}

Add a tag to a document

private bool AddTagToDocument()
{
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Get the root document
    TreeNode root = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/", null, true);

    // Get tag group ID
    TagGroupInfo updateGroup = TagGroupInfoProvider.GetTagGroupInfo("MyNewGroup", SiteContext.CurrentSiteID);

    if ((root != null) && (updateGroup != null))
    {
        // Add tag to document
        root.DocumentTags = "\"My New Tag\"";

        // Add tag to document
        root.DocumentTagGroupID = updateGroup.TagGroupID;

        // Update document
        root.Update();

        return true;
    }

    return false;
}

Update a document and update tags

private bool GetDocumentAndUpdateItsTags()
{
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Get root document
    TreeNode root = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/", null, true);
    if (root != null)
    {
        if (!String.IsNullOrEmpty(root.DocumentTags))
        {
            // Update the tags
            root.DocumentTags = root.DocumentTags.ToUpper();

            // Update document
            root.Update();

            return true;
        }
    }

    return false;
}
1 votesVote for this answer Unmark Correct answer

Recent Answers


David Pearson answered on February 20, 2015 13:35

Thanks for about that folder and section...

David

0 votesVote for this answer Mark as a Correct answer

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