c# - 从 SharePoint 网站检索托管属性

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

最佳答案

这段代码应该可以满足您的需求。

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

关于c# - 从 SharePoint 网站检索托管属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12938294/

相关文章:

c# - 使用 C# 捕获 MS Word 的按键事件

c# - 如何获取与linux/windows兼容的硬盘序列号

sharepoint - 如何更改共享点工作流任务状态?

SharePoint 中的 JavaScript REST GET 变量范围困惑

sharepoint-2010 - 如何开始使用 Sharepoint 2010?

c# - 此平台不支持 Microsoft.Data.SqlClient - Entity Framework Core 3.1

c# - 可以应用什么设计模式来构建需要来自两个数据读取器的数据的对象?

SharePoint 2007 : How to Restrict Access at the Field Level?

asp.net - SP2010 - httpcontext.response.write() 不适用于 LinkBut​​ton 的 onClick 事件

java - Sharepoint Web 服务抛出 Microsoft.SharePoint.SoapServer.SoapServerException