API
Version 7.x > API > Adding Tags to a Document View modes: 
User avatar
Member
Member
ap - 12/10/2012 1:39:42 PM
   
Adding Tags to a Document
Hello.
i'm using the following to import keywords for a document:
string xKeyword = xKeywords2[j];
string xKeywordSQL = xKeyword.Replace("'", "''");

if (xKeyword.Length > 0)
{
xKeyword = xKeyword.Replace("'", "\'");
xKeyword = "\"" + xKeyword + "\"";

DataSet xTags = TagInfoProvider.GetTags("TagName='" + xKeywordSQL + "'", null);

if (xTags.Tables[0].Rows.Count > 0)
{
int TagId = (int)xTags.Tables[0].Rows[0]["TagId"];
DocumentTagInfoProvider.AddTagToDocument(TagId, xDocumentIdK);
}
else
{
DocumentTagInfoProvider.AddTags(xTagGroupId, xDocumentIdK, xKeyword);
}
}

the keywords are added into the table CMS_Tag and CMS_DocumentTag properly, however when i go to
properties -> metadata -> page tags, the associated keywords are not selected. when i check the table CMS_Document the values for field DocumentTags, they are all null.

thank you.
AP

User avatar
Kentico Support
Kentico Support
kentico_janh - 12/12/2012 3:26:54 AM
   
RE:Adding Tags to a Document
Hello,

If you take a look to our API examples section (Site manager -> Support -> API examples), you can find there a recommended way how to add tags to your documents:
private bool AddTagToDocument()
{
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

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

// Get tag group ID
TagGroupInfo updateGroup = TagGroupInfoProvider.GetTagGroupInfo("MyNewGroup", CMSContext.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;
}

Best regards,
Jan Hermann

User avatar
Member
Member
ap - 12/13/2012 3:42:20 PM
   
RE:Adding Tags to a Document
i used the code you provided and it worked perfectly.
out of curiosity what does DocumentTagInfoProvider actually do then, is it not for document tags?

User avatar
Kentico Support
Kentico Support
kentico_janh - 12/14/2012 2:39:46 AM
   
RE:Adding Tags to a Document
Hello,

The DocumentTagInfoProvider and its methods like AddTags add a tag to a document and the database stores these values, however the problem is that the DocumentTags column in the CMS_Document table contains a list of assigned tags and from this column all data are taken to the Properties -> Metadata section, so it then looks like those tags are not assigned to that document if you don't update the document itself.

Best regards,
Jan Hermann