How to: Create a Managed Property

The Schema object in the Enterprise Search Administration object model provides access to the managed properties configured for the search service of a Shared Services Provider (SSP). For more information about the Schema object, see Managing Metadata.

The following procedure shows how to create a managed property from a console application.

To create a managed property from a console application

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

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. In your console application's class file, 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;
    
  3. Create a function to write out usage information to the console window.

    static void Usage()
    {
       Console.WriteLine("Create Managed Property");
       Console.WriteLine("Usage: CreateManagedPropertiesSample.exe PropertyName <DataType>");
       Console.WriteLine("<DataType> - The data type for the new property.  Must be one of:");
       Console.WriteLine("binary");
       Console.WriteLine("datetime");
       Console.WriteLine("decimal");
       Console.WriteLine("integer");
       Console.WriteLine("text");
       Console.WriteLine("yesno");
    }
    
  4. In the Main() function of the console application, add code to check the number of items in the args[] parameter; it must equal 2. If it does not, then call the Usage() function defined in Step 3.

    if (args.Length != 2)
    {
    Usage();
    return;
    }
    
  5. Retrieve the values specified in the args[] parameter, to be used to specify the name and data type for the managed property.

    string strName = args[0];
    string strDataType = args[1];
    
  6. To retrieve the Schema object for the SSP's search context, add 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)));
    
  7. Retrieve the collection of managed properties by using the following code:

    ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
    
  8. Determine if the collection of managed properties already includes a managed property with the same name as the one specified for the new property.

    if (properties.Contains(strName))
    {
       Console.WriteLine("Managed Property with that name already exists.");
       return;
    }
    
  9. If the Contains method returns false, using the value in the strDataType variable, determine which data type to use, and then call the Create method to create the new managed property.

    switch (strDataType)
    {
        case ("binary"):
    properties.Create(strName, ManagedDataType.Binary);
    Console.WriteLine(strName + " created.");
    break;
    
        case ("datetime"):
    properties.Create(strName, ManagedDataType.DateTime);
                 Console.WriteLine(strName + " created.");
                 break;
    
        case ("decimal"):
        properties.Create(strName, ManagedDataType.Decimal);
                 Console.WriteLine(strName + " created.");
                 break;
    
        case ("integer"):
        properties.Create(strName, ManagedDataType.Integer);
                 Console.WriteLine(strName + " created.");
                 break;
    
        case ("text"):
        properties.Create(strName, ManagedDataType.Text);
                 Console.WriteLine(strName + " created.");
                 break;
    
        case ("yesno"):
        properties.Create(strName, ManagedDataType.YesNo);
                 Console.WriteLine(strName + " created.");
                 break;
    
        default:
        Console.WriteLine("Datatype not recognized.");
                 Usage();
                 break;
    }
    

Example

Following is the complete code for the sample console application described in this topic.

Prerequisites

  • Ensure 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 CreateManagedPropertiesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 2)
                {
                    Usage();
                    return;
                }
                string strName = args[0];
                string strDataType = args[1];
/*
Replace <SiteName> with the name of a site using the SSP
*/
                string strURL = "http://<SiteName>";
                Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
                ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
                if (properties.Contains(strName))
                {
                    Console.WriteLine("Managed Property with that name already exists.");
                    return;
                }
                switch (strDataType)
                {
                    case ("binary"):
                        properties.Create(strName, ManagedDataType.Binary);
                        Console.WriteLine(strName + " created.");
                        break;

                    case ("datetime"):
                        properties.Create(strName, ManagedDataType.DateTime);
                        Console.WriteLine(strName + " created.");
                        break;

                    case ("decimal"):
                        properties.Create(strName, ManagedDataType.Decimal);
                        Console.WriteLine(strName + " created.");
                        break;

                    case ("integer"):
                        properties.Create(strName, ManagedDataType.Integer);
                        Console.WriteLine(strName + " created.");
                        break;

                    case ("text"):
                        properties.Create(strName, ManagedDataType.Text);
                        Console.WriteLine(strName + " created.");
                        break;

                    case ("yesno"):
                        properties.Create(strName, ManagedDataType.YesNo);
                        Console.WriteLine(strName + " created.");
                        break;

                    default:
                        Console.WriteLine("Datatype not recognized.");
                        Usage();
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        static void Usage()
        {
            Console.WriteLine("Create Managed Property");
            Console.WriteLine("Usage: CreateManagedPropertiesSample.exe PropertyName <DataType>");
            Console.WriteLine("<DataType> - The data type for the new property.  Must be one of:");
            Console.WriteLine("binary");
            Console.WriteLine("datetime");
            Console.WriteLine("decimal");
            Console.WriteLine("integer");
            Console.WriteLine("text");
            Console.WriteLine("yesno");
        }
    }
}

See Also

Tasks

How to: Return the Search Context for the Search Service Provider
How to: Retrieve the Managed Properties for a Shared Services Provider
How to: Delete a Managed Property

Concepts

Managing Metadata