Assigning Tags

Problem

You want to assign a tag to a content item.

Solution

Tip

A tag must be created before it can be assigned to content.

Warning

You must have administrator privileges to assign a tag.

To assign a tag, you need:

  • The unique identifier (GUID) for the content item you want to tag. See Content recipes, or copy the GUID from the Info pane in the Connect Dashboard.
  • The identifier for the tag you want to use. See Find tags recipe for details.
from posit import connect

CONTENT_GUID = 'your-content-item-guid'
TAG_ID = "your-tag-id"

client = connect.Client()

client.post(f"/v1/content/{CONTENT_GUID}/tags", json={
    'tag_id': TAG_ID,
})

To assign a tag, you need:

  • The unique identifier (GUID) for the content item you want to tag. See Content recipes, or copy the GUID from the Info pane in the Connect Dashboard.
  • The name or identifier for the tag you want to use. See Find tags recipe for details.

The set_content_tags() function in connectapi can take multiple tag objects to set multiple tags at once.

library(connectapi)
client <- connect()

# Create an object representing the content you wish to update.
CONTENT_GUID <- "154bd2af-e8fa-4aa4-aab8-dcef701f4af9"
target_content <- get_content(client, guid = CONTENT_GUID)

# Get the tags from the Connect server, then specify the tag you wish to apply
# to the content.
tags <- get_tags(client)
target_tag <- tags$category$my_tag

target_content <- set_content_tags(content = target_content, tag = target_tag)

When called, set_content_tags() prints out the updated tag tree. It returns an object representing the updated content item.

> target_content <- set_content_tags(target_content, target_tag)
Posit Connect Tag Tree (target_content)
└── category
   └── my_tag

> target_content
Posit Connect Content:
  Content GUID: 154bd2af-e8fa-4aa4-aab8-dcef701f4af9
  Content URL: https://connect.example.org/connect/#/apps/154bd2af-e8fa-4aa4-aab8-dcef701f4af9/
  Content Title: Example Content

content_item(client, guid = "154bd2af-e8fa-4aa4-aab8-dcef701f4af9")

See also