2

I am attempting to simply check a list of Managed Properties for a specific property. In theory, not difficult. In practice, it is proving to give me trouble. The first approach I found is as follows:

static void Main(string[] args)
    {
     try
      {
        string strURL = "http://<SiteName>";
        Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
        ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
        foreach (ManagedProperty property in properties)
         {
           if (property.Name.Equals("ContentType")
            {
              Console.WriteLine(property.Name);
            }
         }
       }
     catch(Exception ex)
        {
          Console.WriteLine(ex.ToString());
        }
    }

This pulled back exactly what I wanted. However, the issue with this is that Visual Studio 2012 says that SearchContext is obsolete and depricated, and that I should use SearchServiceApplication instead. So I did some more searching and found the following:

SPServiceContext context = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);// Get the search service application proxy
    var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;

    if (searchProxy != null)
    {
        SearchServiceApplicationInfo ssai = searchProxy.GetSearchServiceApplicationInfo();

        var application = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(ssai.SearchServiceApplicationId);

        var schema = new Schema(application);
        ManagedPropertyCollection properties = schema.AllManagedProperties;
        foreach (ManagedProperty property in properties)
        {
         if (property.Name.Equals("ContentType")
         {
          Console.WriteLine(property.Name);
         }
        }
    }

The problem I run into with this is an EndpointNotFoundException. I'm guessing I am just configuring the second option incorrectly as the first method can find everything just fine. Can anyone shed some light on anything obviously wrong that I am missing? Any tips/hints will be appreciated!

wjhguitarman
  • 1,063
  • 1
  • 9
  • 27

1 Answers1

2

This chunk of code should get you what you want.

foreach (SPService service in SPFarm.Local.Services)
{
    if (service is SearchService)
    {
        SearchService searchService = (SearchService)service;

        foreach (SearchServiceApplication ssa in searchService.SearchApplications)
        {
            Schema schema = new Schema(ssa);

            foreach (ManagedProperty property in schema.AllManagedProperties)
            {
                if (property.Name == "ContentType")
                {
                    //Handle here
                }
            }
        }
    }
}
athom
  • 1,257
  • 4
  • 13
  • 30