Click to Rate and Give Feedback
MSDN
MSDN Library
Office Development
SDK Documentation
General Reference
Managing Metadata
 How to: Map a Crawled Property to a...
Community Content
In this section
Statistics Annotations (4)
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.

    C#
    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.

    C#
    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.

    C#
    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.

    C#
    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.

    C#
    /*
    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:

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

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

    C#
    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.

    C#
    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.

    C#
    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

C#
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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How to create a managed property and map it to a crawled property      mattgl   |   Edit   |   Show History
/// <summary>
/// Creates a managed property and maps it to a crawled property of type text.
/// </summary>
/// <param name="categoryName">The category, i.e. "People".</param>
/// <param name="crawledPropertyName">The crawled property name, i.e. "Manager".</param>
/// <param name="managedPropertyName">The managed property name.</param>
public void CreateAndMapManagedProperty(string managedPropertyName, string categoryName, string crawledPropertyName)
{
SPWebApplication webApplication = SPWebService.ContentService.WebApplications.First<SPWebApplication>();
ServerContext serverContext = ServerContext.GetContext(webApplication);
SearchContext searchContext = SearchContext.GetContext(serverContext);
Schema sspSchema = new Schema(searchContext);
ManagedProperty managedProperty;
if (sspSchema.AllManagedProperties.Contains(managedPropertyName))
{
Console.WriteLine("Managed Property '{0}' already exists.", managedPropertyName);
managedProperty = sspSchema.AllManagedProperties[managedPropertyName];
}
else
{
managedProperty = sspSchema.AllManagedProperties.Create(managedPropertyName, ManagedDataType.Text);
managedProperty.Description = "Property created by a tool.";
Console.WriteLine("Managed Property '{0}' created.", managedPropertyName);
}
Category category = sspSchema.AllCategories[categoryName];
addMapping(category, managedProperty, crawledPropertyName);
}

/// <summary>
/// Add a mapping between a crawled property and a managed property to the schema.
/// </summary>
/// <param name="schema">The search schema of the SSP.</param>
/// <param name="managedProperty">The managed property</param>
/// <param name="crawledPropertyName">the name of the crawled paroperty</param>
private static void addMapping(Category category, ManagedProperty managedProperty, string crawledPropertyName)
{
foreach (CrawledProperty crawledProperty in category.QueryCrawledProperties(crawledPropertyName, 1, Guid.Empty, null, true))
{
MappingCollection mappingCollection = managedProperty.GetMappings();
Mapping mapping = new Mapping(crawledProperty.Propset, crawledProperty.Name, crawledProperty.VariantType, managedProperty.PID);
if (!mappingCollection.Contains(mapping))
{
mappingCollection.Add(mapping);
managedProperty.SetMappings(mappingCollection);
managedProperty.Update();
Console.WriteLine("Mapping between '{0}' and '{1}' created.", crawledPropertyName, managedProperty.Name);
}
else
{
Console.WriteLine("Mapping of '{0}' to '{1}' was already defined.", crawledPropertyName, managedProperty.Name);
}
// We actually only want to add one mapping so we break out of the foreach loop here
break;
}
}
Tags What's this?: Add a tag
Flag as ContentBug
The sample doesn't show how to determine the essential parameters...      mattgl   |   Edit   |   Show History

... so I added another sample.

Tags What's this?: Add a tag
Flag as ContentBug
RE: How do you use this example?      RiverBoatman   |   Edit   |   Show History
OK try 31 for text and 20 for integer. You can look them up by editing a mapped property using Search Server 2008 Administration
Tags What's this?: Add a tag
Flag as ContentBug
How do you use this example?      Andre mulder   |   Edit   |   Show History

I get an exception if i Fill in ManagedDataType.Text as propertydatatype.

The exception is: The variant type 1 cannot be mapped to managed data type Text.

I use this example as follows:

MapManagedProperty(

"f0d3d197-0d2a-41c9-947b-66dfa0c98d99", "BACCODE", ManagedDataType.Text , "BACCODE");

My method:

private

staticvoidMapManagedProperty(string PropertyId, string PropertyNaam, ManagedDataType propertydatatype, string MappingKolomNaam)

{

Guid cPropGUID = newGuid(PropertyId); string cPropName = PropertyNaam; int vType = Convert.ToInt32(propertydatatype);string mPropName = MappingKolomNaam; SearchContext context; string strURL = urlusing (SPSite site = newSPSite(strURL))

{

context =

SearchContext.GetContext(site);

}

Schema sspSchema = newSchema(context); ManagedPropertyCollection properties = sspSchema.AllManagedProperties; ManagedProperty mProp = properties[mPropName]; Mapping newMapping = newMapping(cPropGUID, cPropName,vType, mProp.PID); MappingCollection mappings = mProp.GetMappings(); if (mappings.Contains(newMapping))

{

Console.WriteLine("Toewijzing mislukt: Aangevraagde toewijzing bestaat al."); return;

}

mappings.Add(newMapping);

mProp.SetMappings(mappings);

Console.WriteLine(cPropName + "Verkende eigenschap toegewezen aan " + mProp.Name + " eigenschap."); return;

}

Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker