Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > How Do I Determine if a Document Has a Certain Category Assinged to it? View modes: 
User avatar
Member
Member
scott_hancock-urmc.rochester - 5/15/2012 3:41:30 PM
   
How Do I Determine if a Document Has a Certain Category Assinged to it?
I'm assigning categories to the documents in my site and in code I want to determine if the current document has been assigned a certain category. In the code below everything works except that the ds.Contains() returns false even though the current document has the correct category (I verified it in debug mode) . What am I doing wrong?


InfoDataSet<CategoryInfo> ds = CategoryInfoProvider.GetDocumentCategories(CMSContext.CurrentDocument.DocumentID, null, null);
CategoryInfo myCategory = CategoryInfoProvider.GetCategoryInfo("TheCategory","MySite");

if (ds.Contains(myCategory))
{
//Do something.
}

Thanks,
Scott

User avatar
Member
Member
kentico_michal - 5/20/2012 6:09:36 AM
   
RE:How Do I Determine if a Document Has a Certain Category Assinged to it?
Hi,

The relationship between a document and a category is represented by the DocumentCategoryInfo class. So, you can use the DocumentCategoryInfoProvider.GetDocumentCategoryInfo method to determine if the given document has been assigned to the category. Please take a look at the following code snippet:



using CMS.SiteProvider;
using CMS.TreeEngine;

CategoryInfo myCategory = CategoryInfoProvider.GetCategoryInfo("TheCategory", "MySite");

DocumentCategoryInfo dcInfo = DocumentCategoryInfoProvider.GetDocumentCategoryInfo(CMSContext.CurrentDocument.DocumentID, myCategory.CategoryID);

if (dcInfo == null)
{
// document is not assigned to the category
}


Best regards,
Michal Legen

User avatar
Member
Member
scott_hancock-urmc.rochester - 5/23/2012 7:52:55 AM
   
RE:How Do I Determine if a Document Has a Certain Category Assinged to it?
That works fine.

Thanks,
Scott

User avatar
Member
Member
scott_hancock-urmc.rochester - 5/25/2012 3:55:05 PM
   
RE:How Do I Determine if a Document Has a Certain Category Assinged to it?
Is there any way to do this from a macro with K#?

Thanks,
Scott

User avatar
Member
Member
kentico_michal - 5/30/2012 3:12:33 AM
   
RE:How Do I Determine if a Document Has a Certain Category Assinged to it?
Hi,

You can register a custom macro method which determines if a document in question is assigned to some category. For more information I would like to point you to the following article: Registering cutom macro methods.

Best regards,
Michal Legen