2011-02-18 89 views
1

我正嘗試使用UserProfileManager顯示自定義web部件中的用戶列表。出於某種原因,我可以查看webpart並將所有配置文件輸出到屏幕(可能是因爲我是管理員)。但是當一個標準用戶登錄時,他們會遇到一個403頁面。在SharePoint 2010中允許UserProfileManager權限

我已經做了一些閱讀,我知道它與權限有關。這是我在我的代碼:

private DataTable GetProfiles() 
    { 
     DataTable dtUserProfile = new DataTable(); 
     //...DataTable Columns 

     SPSecurity.RunWithElevatedPrivileges(delegate() 
     { 
      Guid guid = SPContext.Current.Site.ID; 

      using (SPSite intranet = new SPSite(guid)) 
      { 
       SPUserToken userToken = intranet.Owner.UserToken; 

       //Get current intranet context. 
       SPServiceContext sContext = SPServiceContext.GetContext(intranet); 

       UserProfileManager profileManager = new UserProfileManager(sContext); 

       int totalUsers = int.Parse(profileManager.Count.ToString()); 

       Random random = new Random(); 

       for (int i = 0; i < NumberOfUsersToRetrieve(NoOfProfiles, totalUsers); i++) 
       { 
        int randNumber = random.Next(1, totalUsers); 

        DataRow drUserProfile; 

        UserProfile up = profileManager.GetUserProfile(randNumber); 

        drUserProfile = dtUserProfile.NewRow(); 

        drUserProfile["DisplayName"] = up.DisplayName; 
        drUserProfile["FirstName"] = up["FirstName"].Value; 
        drUserProfile["LastName"] = up["LastName"].Value; 
        drUserProfile["Department"] = up["Department"].Value; 
        drUserProfile["ContactNumber"] = up["Office"].Value;       
        drUserProfile["MySiteUrl"] = up.PublicUrl; 

        dtUserProfile.Rows.Add(drUserProfile); 
       } 
      } 
     }); 

     return dtUserProfile; 
    } 

我的代碼基本上得到取決於我想要返回的用戶數用戶的隨機集合。

是否有可能爲用戶創建一個SPUserToken,以獲取檢索用戶配置文件所需的所有權限?

謝謝!

回答

1

我明白這個問題是舊的,但我有完全相同的問題。爲了幫助原始海報和其他用戶,我已將原始帖子的代碼更改爲以下內容:

SPSecurity.RunWithElevatedPrivileges(delegate() 
{ 
    SPSite sc = new SPSite(SPContext.Current.Site.ID); 
    SPServiceContext context = SPServiceContext.GetContext(sc); 

    HttpContext currentContext = HttpContext.Current; 
    HttpContext.Current = null; 

    UserProfileManager profileManager = new UserProfileManager(context); 

    IEnumerator profileEnum = profileManager.GetEnumerator(); 

    while (profileEnum.MoveNext()) 
    { 
     UserProfile up = (UserProfile)profileEnum.Current; 

     if ((up["FirstName"] != null && up["FirstName"].Value != null && !String.IsNullOrEmpty(up["FirstName"].Value.ToString())) 
      && (up.PublicUrl != null && !String.IsNullOrEmpty(up.PublicUrl.ToString()))) 
     { 
      DataRow drUserProfile; 

      drUserProfile = dtUserProfile.NewRow(); 

      drUserProfile["DisplayName"] = up.DisplayName; 
      drUserProfile["FirstName"] = up["FirstName"].Value; 
      drUserProfile["LastName"] = up["LastName"].Value; 
      drUserProfile["Department"] = up["Department"].Value; 
      drUserProfile["Location"] = up["SPS-Location"].Value;   

      drUserProfile["MySiteUrl"] = up.PublicUrl.ToString().Replace(@"\", @"&#92;"); 

      dtUserProfile.Rows.Add(drUserProfile); 
     } 
    } 
} 

HttpContext.Current = currentContext; 

希望此代碼能夠解決錯誤。

0

而不是獲取SPSite.Owner的UserToken,你嘗試過SPSite.SystemAccount.UserToken或SPWeb.AllUsers [「user」]。UserToken;

我願意做後者如果可能的話,最低權限規則等

+0

那麼我要更換這行:「SPUserToken userToken = intranet.Owner.UserToken;」與「SPUserToken userToken = SPWeb.AllUsers [」user「]。UserToken;」? 「用戶」是具有管理員權限的用戶嗎? – R100 2011-02-18 21:29:50

+0

更準確地說,SPUserToken userToken = intranet.OpenWeb()。AllUsers。[「user」]。UserToken - 是的,「user」將是具有所需特權的那個。 – 2011-02-19 00:01:56

相關問題