从SharePoint站点检索托管属性

时间:2012-10-17 15:51:51

标签: c# sharepoint sharepoint-2010

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

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。 我猜我只是错误地配置第二个选项,因为第一个方法可以找到一切正常。任何人都可以解释一些我错过的明显错误吗? 任何提示/提示将不胜感激!

1 个答案:

答案 0 :(得分: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
                }
            }
        }
    }
}