How to: Retrieve the Crawled Properties for a Category in the Search Schema

SharePoint 2007

You can use the Enterprise Search Administration object model to access the crawled property categories for a Shared Services Provider's (SSP's) search metadata using the Schema object. You can then retrieve the list of crawled properties associated with each category using the GetAllCrawledProperties method of the Category object.

The following procedure shows how to write the full list of crawled properties and their property set GUIDs and variant data types from a console application.

To set up your application to use the Enterprise Search Administration object model

  1. In your application, set references to the following DLLs:

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. In the class file of your console application, add the following using statements near the top of the code with the other namespace directives.

    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    

To verify that a value was specified for category name by the user, and if not, provide usage help to the user

  1. Create a function to write out usage information to the console window.

    static void Usage()
    {
       Console.WriteLine("Retrieve Crawled Properties by Category Sample");
       Console.WriteLine("Usage: GetCrawledPropertiesSample.exe CategoryName");
    }
    
  2. In the Main() function of the console application, add code to check if the number of items in the args[] parameter is equal to 1; if not (meaning that no value was specified to identify the category), then call the Usage() function defined in the previous step.

    if (args.Length != 1)
    {
    Usage();
    return;
    }
    

To retrieve the categories for the SSP's search schema and display the list of crawled properties for that category

  1. Create a local string variable for the category name, and set it to the value specified in the args[] parameter, using the following code:

    string strCategoryName = args[0];
    
  2. Retrieve the Schema object for the search context of the SSP, using the following code. For more information about ways to retrieve the search context, see How to: Return the Search Context for the Search Service Provider.

    /*
    Replace <SiteName> with the name of a site using the SSP
    */
    string strURL = "http://<SiteName>";
    Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
    
  3. Retrieve the collection of categories for the search schema by using the following code:

    CategoryCollection categories = sspSchema.AllCategories;
    
  4. Determine if the collection of categories includes the category specified; if not, display the list of categories in the collection using the following code:

    if (categories.Contains(strCategoryName) != true)
    {
        Console.WriteLine(strCategoryName + " does not exist in Schema.");
        Console.WriteLine("Valid Schema Categories are:");
        foreach (Category category in categories)
        {
            Console.WriteLine(category.Name);
        }
        return;
    }
    
  5. If the category exists, retrieve the list of crawled properties for that category, and write to the console the name, property set GUID, and variant type for each crawled property in the list using the following code:

    foreach (Category category in categories)
    {
        if (category.Name == strCategoryName)
        {
            foreach (CrawledProperty property in category.GetAllCrawledProperties())
            {
                Console.WriteLine("");
                Console.Write("NAME: " + property.Name);
                Console.Write("   PROPSET: " + property.Propset.ToString());
                Console.Write("   VARIANTTYPE: " + property.VariantType.ToString());
            }
        }
    }
    

Example

Following is the complete code for the console application class sample.

Prerequisites

  • Ensure that a Shared Services Provider is already created.

Project References

Add the following Project References in your console application code project before running this sample:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.Search

using System;
using System.Collections;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace GetCrawledPropertiesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Usage();
                    return;
                }

                string strCategoryName = args[0];
                /*
                Replace <SiteName> with the name of a site using the SSP
                */
                string strURL = "http://<SiteName>";
                Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
                CategoryCollection categories = sspSchema.AllCategories;

                if (categories.Contains(strCategoryName) != true)
                {
                    Console.WriteLine(strCategoryName + " does not exist in Schema.");
                    Console.WriteLine("Valid Schema Categories are:");
                    foreach (Category category in categories)
                    {
                        Console.WriteLine(category.Name);
                    }
                    return;
                }

                foreach (Category category in categories)
                {
                    if (category.Name == strCategoryName)
                    {
                        foreach (CrawledProperty property in category.GetAllCrawledProperties())
                        {
                            Console.WriteLine("");
                            Console.Write("NAME: " + property.Name);
                            Console.Write("   PROPSET: " + property.Propset.ToString());
                            Console.Write("   VARIANTTYPE: " + property.VariantType.ToString());
                        }
                    }
                }
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Usage();
            }
        }
 
        static void Usage()
        {
            Console.WriteLine("Retrieve Crawled Properties by Category Sample");
            Console.WriteLine("Usage: GetCrawledPropertiesSample.exe CategoryName");
        }
    }
}

Community Additions

ADD
Show: