ASPX templates
Version 5.x > ASPX templates > Selecting Documents By TagId View modes: 
User avatar
Member
Member
eric.rovtar-hudsonchapel - 8/4/2010 10:50:05 AM
   
Selecting Documents By TagId
What's the best way to select all documents with a specific tag? I tried using the following line without much success:

DataSet ds = TagInfoProvider.GetTags("DocumentID IN ( SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = " + tagID + ")", "TagId ASC");


Where tagID is the ID of the tag I'm trying to list documents it's tagged with.

Thanks!

-Eric

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/4/2010 2:37:00 PM
   
RE:Selecting Documents By TagId
Hi Eric,


The code seems to be correct. What problems do you have with result?


Best regards,
Helena Grulichova

User avatar
Member
Member
eric.rovtar-hudsonchapel - 8/4/2010 3:59:43 PM
   
RE:Selecting Documents By TagId
Here's the error info:

[DataConnection.ExecuteQuery]: Query: SELECT * FROM CMS_Tag WHERE DocumentID IN ( SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = 23) ORDER BY TagId ASC: caused exception: Invalid column name 'DocumentID'.


Here's my full code:

DataSet ds = TagInfoProvider.GetTags("DocumentID IN ( SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = " + tagID + ")", "TagId ASC");

foreach (DataRow dr in ds.Tables[0].Rows)
{
Literal doc = new Literal();
doc.Text = "Doc: " + dr["DocumentID"].ToString();

plContentText.Controls.Add(doc);
}


Thanks!

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/6/2010 8:25:43 AM
   
RE:Selecting Documents By TagId
Hello,


Now, you use the API method to get tags but you say you want to get documents. You could use SelectNodes method with the same Where condition. You can see an API example here: Selecting nodes

Please see the API Reference for the overloaded methods with Where condition.


Best regards,
Helena Grulichova

User avatar
Member
Member
eric.rovtar-hudsonchapel - 8/9/2010 9:09:38 AM
   
RE:Selecting Documents By TagId
Ok... So now here's my code:

// create a TreeProvider instance
UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);

HCC.Tag tag2 = (HCC.Tag)HCC.Tags.CurrentDocumentTags[0];
string where = "DocumentID IN ( SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = " + tag2.ID + ")";

// get dataset of tree nodes specified by alias path and class names (separated by semicolon),
// the second parameter says whether to return default culture documents if the required
// document language version is not available
DataSet ds = tree.SelectNodes("Community Chapel", "/Crossroads/%", "en-us", true, "cms.menuitem;cms.products", where, "TagID ASC");

foreach (DataRow dr in ds.Tables[0].Rows)
{
Literal doc = new Literal();
doc.Text = "Doc: " + dr["DocumentID"].ToString();

plContentText.Controls.Add(doc);
}


And I get this error:

[DataConnection.ExecuteQuery]: Query: SELECT * FROM View_CONTENT_MenuItem_Joined WHERE (((((SiteName = N'Community Chapel') AND (Published = 1)) AND (DocumentCulture = N'en-us')) AND (NodeAliasPath LIKE N'/Crossroads/%')) AND (DocumentID IN ( SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = 23))) ORDER BY TagID ASC : caused exception: Invalid column name 'TagID'.

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/12/2010 5:11:35 AM
   
RE:Selecting Documents By TagId
Hello,

the problem is in the Order by part:

ORDER BY TagID ASC

because there is no such column in the original table.

If you remove Order by it should work correctly. Please only use the valid SQL statements in your query.


Best regards,
Helena Grulichova

User avatar
Member
Member
eric.rovtar-hudsonchapel - 9/3/2010 12:14:03 PM
   
RE:Selecting Documents By TagId
Ok.. I'm still not having luck. Could someone please tell me how to select documents by a tag?

Thanks!

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 9/22/2010 2:35:56 PM
   
RE:Selecting Documents By TagId
Hi,

Using following simple query over database you can get the list of documents containing requested tag:

SELECT NodeID, NodeAliasPath, DocumentName
FROM [View_CMS_Tree_Joined]
WHERE DocumentTags LIKE '%flight%'


The TagName can be taken according to tagID from CMS_DocumentTag table.

Best regards,
Ivana Tomanickova