2010-01-08 71 views
1

有没有办法从对象模型中搜索MOSS中的配置文件?我需要搜索在他们的个人资料上设置了特定值的个人资料,然后为他们执行一些操作。SharePoint用户配置文件搜索

我需要编写一些可以搜索配置文件数据库并返回匹配配置文件的c#代码。基本上,

列表的配置文件=选择配置文件从配置文件存储在哪里配置文件属性值= someValue中

我试图避免以下:

private IEnumerable<UserProfile> SearchProfiles(string value) { 
     ServerContext serverContext = ServerContext.GetContext(SPContext.Current.Site); 
     UserProfileManager profileManager = new UserProfileManager(serverContext); 
     foreach (UserProfile profile in profileManager) { 
      if ((string)profile["MyProp"].Value == value) { 
       yield return profile; 
      } 
     } 
    } 

回答

1

这是可能的使用FullTextSqlQuery类:

FullTextSqlQuery q = new FullTextSqlQuery(ServerContext.Current); 
q.ResultTypes = ResultType.RelevantResults; 
q.QueryText = "SELECT UserName, Email, PreferredName FROM SCOPE() WHERE \"scope\" = 'People' AND Department = 'IT'"; 

ResultTableCollection tables = q.Execute(); 
ResultTable results = tables[ResultType.RelevantResults]; 

这个类允许你查询一个特定的作用域(即人),并使用WHERE子句根据属性对它们进行过滤,w它看起来和普通的Sql查询基本相同。

为了能够在(自定义)用户配置文件属性上进行搜索和过滤,配置文件属性需要在共享服务提供商的元数据设置中具有映射。大多数开箱即用用户配置文件属性已经具有这些自定义属性,您必须自行添加。

有关托管属性的更多信息here

0

两件事情:

  1. 与提升的权限运行时,我们需要建立呼叫内的一个新SPSite对象,并从那里加载安全上下文。不要使用使用SPContext.Current.Site获取的上下文。

    因此:

    SPSecurity.RunWithElevatedPrivileges(delegate() 
    { 
    using (SPSite site = new SPSite("<your site url>")) 
    { 
        ServerContext context = ServerContext.GetContext(site); 
    
        UserProfileManager profileManager = new 
    
        UserProfileManager(context); 
    
        foreach (UserProfile profile in profileManager) 
        { 
        // your code 
        } 
    } 
    } 
    
  2. 确保应用程序池帐户在SSP适当的用户权限。即(使用个人功能,管理用户配置文件)

+0

6年后,我知道,但为什么我不能在SPContext.Current.Site中使用RunWithElevatedPrivileges? – 2016-05-25 13:04:25

相关问题