Share via


How to: Delete 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 programmatically delete a managed property using the Enterprise Search Administration object model in a console application.

To delete 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("Delete Managed Properties Sample");
      Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe  PropertyName");
    }
    
  4. In the Main() function of the console application, add code to check the number of items in the args[] parameter; it must equal 1. If it does not, then call the Usage() function defined in Step 3.

    if (args.Length != 1)
    {
    Usage();
    return;
    }
    
  5. Retrieve the value specified in the args[] parameter, to be used to specify the name of the managed property to delete.

    string strName = args[0];
    
  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 includes the property specified.

    if (properties.Contains(strName))
    {
       Console.WriteLine(strName + " property does not exist.  Delete failed.");
       return;
    }
    
  9. If the Contains method returns true, loop through the managed properties, and for each one, check to see if the Name property matches the value of strName.

    foreach (ManagedProperty property in properties)
    {
        if (property.Name == strName)
        {
    
  10. Next, confirm that the property can be deleted by checking the value of the DeleteDisallowed property.

    if (property.DeleteDisallowed)
    {
       Console.WriteLine("DeleteDisallowed enabled for " + strName + ".  Delete failed.");
       return;
    } 
    
  11. If the DeleteDisallowed property returns false, call the DeleteAllMappings method, and then call the Delete method for the property.

       property.DeleteAllMappings();
       property.Delete();
       Console.WriteLine(strName + " deleted.");
       return;
     }
    }
    

Example

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

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 DeleteManagedPropertiesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    Usage();
                    return;
                }
                string strName = 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)));
                ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
                if (!properties.Contains(strName))
                {
                    Console.WriteLine(strName + " property does not exist.  Delete failed.");
                    return;
                }
                foreach (ManagedProperty property in properties)
                {
                    if (property.Name == strName)
                    {
                        if (property.DeleteDisallowed)
                        {
                            Console.WriteLine("DeleteDisallowed enabled for " + strName + ".  Delete failed.");
  return;
                        }

                        property.DeleteAllMappings();
                        property.Delete();
                        Console.WriteLine(strName + " deleted.");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private static void Usage()
        {
            Console.WriteLine("Delete Managed Properties Sample");
            Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe  PropertyName");
        }
    }
}

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: Create a Managed Property

Concepts

Managing Metadata