views:

1589

answers:

2

Hi

I'm looking for a way to get the categories defined in the site directory of a portal programmatically. As far as I could see categories are represented as fields of the type "choice" with multiple values the user can chose form.

My problem is, how could I distinguish category fields from normal fields like "title" or "author". Actually I don't wan to hard code the categories names but want to load them dynamically. I think there have to be some criteria as the standard web part on the site directory page also loads the categories dynamically.

Unfortunately I couldn't find many information about this problem.

Bye Flo

+1  A: 

Get a reference to the SPWeb. Use SharePoint Manager (not strictly necessary, but the app is useful) to get the details of the Field you want.

This code can get the list from the fields xmlSchema. Note: the commented xml is how my own Category field's SchemaXml looks.

public static ArrayList GetCategory(SPWeb web)
{
    ArrayList result = new ArrayList();

    /*            <?xml version="1.0" encoding="utf-16"?>
    <Field ID="{6DF9BD52-550E-4a30-BC31-A4366832A87D}" Type="Choice" Group="_Hidden" Name="Category" DisplayName="Category" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Category">
      <CHOICES>
        <CHOICE>(1) Category1</CHOICE>
        <CHOICE>(2) Category2</CHOICE>
        <CHOICE>(3) Category3</CHOICE>
      </CHOICES>
      <Default>(2) Category2</Default>
    </Field>*/
    try
    {
        SPField catField = web.Fields[new Guid("6DF9BD52-550E-4a30-BC31-A4366832A87D")];
        XmlDocument Doc = new XmlDocument();
        Doc.LoadXml(catField.SchemaXml);
        XmlNodeList Choices = Doc.SelectNodes("Field/CHOICES/CHOICE");
        foreach (XmlNode Choice in Choices)
        {
            result.Add(Choice.InnerText);
        }
    }
    catch (Exception ex)
    {
        result.Add("Failed: " + ex.Message);
    }
    return result;
}
Nat
+1  A: 

Ok, I see, I expressed myself not clearly. I know how to get the field schema form field. But I think after your post I got the answer my question myself.

The thing I wanted to know is, that I could assume that every field of the type "choice" in the "websites" list of the site directory represent a category a site could be assigned to? So every category field has to be of the type "choice"?

To get all available categories I have to iterate through the list's fields and look for the fields based on the type "choice", right?

UPDATE

Meanwhile I think I found an answer to my question. A column in the websites list is handled as a category of the web site directory if it part of the "categories" view. In general category columns don't have to be necessarily of the type 'choice', although other types don't make sense in this context.

Flo
Ah, I get ya. I think that is the only way to go as they are not stored anywhere else.
Nat
Fields of type "Choise" don't exist in SharePoint, unless you've created them yourself. It's "Choice" ;-) And why the messy code with all that XML parsing? There's a nice class called SPFieldChoice. Just cast the SPField to a SPFieldChoice and enumerate the Choices property.
LeonZandman