How to: Map a Crawled Property to 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 map a crawled property to a managed property.

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 the required values for the property mapping were passed in the args[] parameter, and if not, display usage information

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

    static void Usage()
    {
        Console.WriteLine("Map Crawled Property to Managed Property Sample");
        Console.WriteLine("Usage: PropertyMappingSample.exe <cPropGUID> <cPropName> <cPropVType> <mPropPID>");
        Console.WriteLine("<cPropGUID> - The GUID identifying the crawled property.");
        Console.WriteLine("<cPropName> - The crawled property name.");
        Console.WriteLine("<cPropVType> - The variant type of the crawled proeprty.");
        Console.WriteLine("<mPropName> - The name of the managed property.");
    }
    
  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 4; if not (meaning that not all the values required to create the property mapping were passed in the args[] parameter), then call the Usage() function defined in the previous step.

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

To create the property mapping

  1. Create local variables to hold the values for the property mapping passed in the args[] parameter using the following code.

    Guid cPropGUID = new Guid(args[0]);
    string cPropName = args[1];
    int vType = Convert.ToInt32(args[2]);
    string mPropName = args[3];
    
  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
    */
    SearchContext context;
    string strURL = "http://<SiteName>";
    using(SPSite site = new SPSite(strURL))
    {
    context = SearchContext.GetContext(site);
    }
    Schema sspSchema = new Schema(context);
    
  3. Retrieve the collection of managed properties by using the following code:

    ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
    
  4. Retrieve the managed property with the name specified in the args[3] parameter.

    ManagedProperty mProp = properties[mPropName];
    
  5. Create an instance of the Mapping class using the values from the previous steps.

    Mapping newMapping = new Mapping(cPropGUID, cPropName, vType, mProp.PID);
    
  6. Retrieve the collection of mappings for that managed property, check to see if the mapping created in Step 5 matches any existing mapping in the collection, and if so, display that information in the console using the following code.

    MappingCollection mappings = mProp.GetMappings();
    if(mappings.Contains(newMapping))
    {
        Console.WriteLine("Mapping failed: requested mapping already exists.");
        return;
    }
    
  7. If the new mapping does not match any existing mapping in the collection, add the new mapping to the managed property's mapping collection with the following code.

    mappings.Add(newMapping);
    mProp.SetMappings(mappings);
    Console.WriteLine(cPropName + "crawled property mapped to " + mProp.Name + " managed property.");
    return;
    

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 PropertyMappingSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 4)
                {
                    Usage();
                    return;
                }

                Guid cPropGUID = new Guid(args[0]);
                string cPropName = args[1];
                int vType = Convert.ToInt32(args[2]);
                string mPropName = args[3];
                /*
                    Replace <SiteName> with the name of
                    a site that uses the SSP
                */
                string strURL = "http://<SiteName>";
                SearchContext context;
                using(SPSite site = new SPSite(strURL))
                {
                   context = SearchContext.GetContext(site);
                }
                Schema sspSchema = new Schema(context);
                ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
                ManagedProperty mProp = properties[mPropName];
                Mapping newMapping = new Mapping(cPropGUID, cPropName, vType, mProp.PID);
                MappingCollection mappings = mProp.GetMappings();
                if(mappings.Contains(newMapping))
                {
                   Console.WriteLine("Mapping failed: requested mapping already exists.");
                    return;
                }
                mappings.Add(newMapping);
                mProp.SetMappings(mappings);
                Console.WriteLine(cPropName + "crawled property mapped to " + mProp.Name + " managed property.");
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Usage();
            }
        }

        static void Usage()
        {
            Console.WriteLine("Map Crawled Property to Managed Property Sample");
            Console.WriteLine("Usage: PropertyMappingSample.exe <cPropGUID> <cPropName> <cPropVType> <mPropName>");
            Console.WriteLine("<cPropGUID> - The GUID identifying the crawled property.");
            Console.WriteLine("<cPropName> - The crawled property name.");
            Console.WriteLine("<cPropVType> - The variant type of the crawled proeprty.");
            Console.WriteLine("<mPropName> - The name of the managed property.");
        }
    }
}

See Also

Concepts

Managing Metadata
Getting Started with the Search Administration Object Model