在SharePoint 2010中允许UserProfileManager权限 [英] Allowing UserProfileManager Permissions in SharePoint 2010

查看:167
本文介绍了在SharePoint 2010中允许UserProfileManager权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to display a list of users in a custom webpart using the UserProfileManager. For some reason, I can view the webpart and all profiles are output to the screen (maybe because I am an administrator). But when a standard user logs in, they encounter a 403 page.

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

I have done some reading up on this and I know its something to do with permissions. This is what I have in my code:

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

根据我要返回的用户数量,我的代码基本上得到了一个随机的用户集合.

My code basically gets a random collection of users depending on the number of users I want to return.

是否可以为用户创建一个SPUserToken,以获取检索用户配置文件所需的所有权限?

Is it possible to create a SPUserToken for a user that all permissions needed to retrieve the user profiles?

谢谢!

推荐答案

我很欣赏这个问题,但是我遇到了完全相同的问题.为了帮助原始海报发布者和其他用户,我将代码从原始帖子更改为以下内容:

I appreciate this question is old, but I had the exact same problem. To help the original poster and other users, I have altered the code from the original post to the following:

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;

希望这段代码可以解决该错误.

Hopefully this code should resolve the error.

这篇关于在SharePoint 2010中允许UserProfileManager权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆