2010-01-08 62 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

相關問題