Skip to main content

I'm writing a library, making use of the C# K2 APIs, which determines which smartobjects (SMOs) and methods are in an inconsistent state. I've been able to do this sucessfully but I cannot find a way to get the category a SMO is associated with.

 

In the smart object tester you can see the category a SMO is associated with, as it appears under a folder icon. I'd like to get the same information programatically.

 

I was hoping to find the category information inside an instance of the SmartObjectInfo class (e.g. within the Metadata property) but it does not appear to be there.

 

I also had a brief look at the SourceCode.Categories.Client.dll library, and was hoping to find a method where I could determine the category of a SMO, but I couldn't find anything.

 

Can anyone let me know how I can use the K2 API to get at the category info for a SMO? 

Hi Ben

 

Below is a code snippet i wrote to get the full category path of any object. To get the SmartObject category path you will need to pass in the SMO Guid and the object type as "SmartObject". You will need to reference SourceCode.Categories.Client.dll and use the CategoryServer object.

 

You will need to create a connection string like: "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=localhost;Port=5555;WindowsDomain=idomain name];SecurityLabelName=K2"

 

using SourceCode.Categories.Client;   

public string ObjectCategoryPath(string objectGuid, string objectType)
{
CategoryServer catServer = new CategoryServer();
BaseAPIConnection conn = catServer.CreateConnection();
catServer.Connection.Open(k2Connection);


using (catServer.Connection)
{


int ] cats = catServer.GetCategoriesByDataId(objectGuid, objectType);

if (cats.Length != 0)
{
StringBuilder path = new StringBuilder();
CategoryManager catManager = catServer.GetCategoryManager(1, false);

Category cat = catManager.Categories.GetCategoryById(catsc0]);

path.Append(@"/");
path.Append(cat.Name);

while (!cat.ParentCategory.IsRoot)
{
Category parentCat = cat.ParentCategory;

path.Insert(0, parentCat.Name);
path.Insert(0, @"/");
cat = parentCat;
}

// remove first forward slash
path.Remove(0, 1);

return path.ToString();
}
else
return String.Empty;
}

}

Nice work Andrew. That's exactly what I was looking for. Kudo +1!


Reply