2

我试图简单地检查特定属性的托管属性列表。理论上,不难。在实践中,事实证明这给我带来了麻烦。我发现的第一种方法如下:

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());
        }
    }

这完全符合我的要求。然而,这个问题是 Visual Studio 2012 说它SearchContext已经过时和过时,我应该SearchServiceApplication改用它。所以我做了更多搜索,发现以下内容:

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);
         }
        }
    }

我遇到的问题是EndpointNotFoundException. 我猜我只是错误地配置了第二个选项,因为第一个方法可以找到一切都很好。任何人都可以对我遗漏的任何明显错误的事情有所了解吗?任何提示/提示将不胜感激!

4

1 回答 1

2

这段代码应该可以得到你想要的。

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
                }
            }
        }
    }
}
于 2012-10-17T21:42:57.047 回答