Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:00:17+00:00 2026-06-01T18:00:17+00:00

I am trying to display a list of users in a custom webpart using

  • 0

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.

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

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:00:18+00:00Added an answer on June 1, 2026 at 6:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display a dropdown list of users in my view. Here is
I'm trying to display some data from a list sharepoint and then using jquery
I am trying to display a list of all files found in the selected
I'm trying to display a list of all events for a particular venue depending
I'm trying to display a list of check-ins in a venue using the following
I'm trying to display a list coming from my database using Linqtosql but I
I am trying to display only selected items in list view. public class Mact
I'm trying to display all the users I have not voted on yet! CREATE
i am trying to use FilteredSelectMultiple widget to display list of users. currently it
I am trying to display a list of items in a datagrid from an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.