在SharePoint 2010中允许UserProfileManager权限

时间:2011-02-18 20:45:09

标签: sharepoint permissions sharepoint-2010 web-parts

我正在尝试使用UserProfileManager在自定义webpart中显示用户列表。出于某种原因,我可以查看webpart并将所有配置文件输出到屏幕(可能是因为我是管理员)。但是当标准用户登录时,他们会遇到403页。

我已经对此做了一些阅读,我知道它与权限有关。这就是我的代码中的内容:

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,以获取检索用户配置文件所需的所有权限?

谢谢!

2 个答案:

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

希望此代码可以解决错误。

答案 1 :(得分:0)

您没有获取SPSite.Owner的UserToken,您是否尝试过SPSite.SystemAccount.UserToken或SPWeb.AllUsers [“user”]。UserToken;

如果可能,我会做后者,最少特权的规则等。