2012-10-17 114 views
2

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

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。 我猜我只是配置错误的第二个选项,因为第一个方法可以找到一切都很好。任何人都可以对我错过的任何明显错误的东西进行阐述? 任何提示/提示将不胜感激!

回答

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