0
votes

We have custom upload page for document library. On upload page we have one people picker field, (user can enter multiple users id) Design for People picker

<SharePoint:PeopleEditor ID="pplApprovers" runat="server" Width="250px" Height="25px" MultiSelect="true"/>

to get emp id from people picker we use below code

 public ArrayList approversArray; 
public SPFieldUserValueCollection approversCollection; 

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
 { 
  ArrayList aAccount1 = new ArrayList();
    approversArray= pplApprovers.Entities;

approversCollection = new SPFieldUserValueCollection(); 
SPUser user; 
SPGroup group; 
SPUser currentUser;
SPWeb web=SPControl.GetContextWeb(Context);
currentUser=web.CurrentUser;
 ArrayList aAccount = new ArrayList();
aAccount = pplApprovers.Accounts;
ArrayList peEntities = pplApprovers.Entities;
approversArray = pplApprovers.ResolvedEntities;

foreach (PickerEntity entity in approversArray) 
{
if (entity.EntityData["PrincipalType"].ToString() == "SharePointGroup") 
{
    group = web.SiteGroups[entity.Key]; 
    approversCollection.Add(new SPFieldUserValue(web,group.ID, group.Name)); 
} 
else 
{
    //handles SecurityGroup, Distribution List and User 
    user = web.EnsureUser(entity.Key); 
    approversCollection.Add(new SPFieldUserValue(web,user.ID, user.Name)); 
} 
} 
}); 

catch (Exception ex)
{
// Manage error event
}

and after getting this value we are inserting it in document library.

item.Item["Account Partner"]="approversCollection";

but after clicking upload button the only user who have site admin access can successfully upload the file but other user's who don't have admin access gets redirected to the https://web/_layouts/AccessDenied.aspx page

We tried using SPSecurity.RunWithElevatedPrivileges but got no success...

Anyone please let me know how to resolve this issue or alternate way to use people picker

1
Where is it failing? And please show the code you're using when you use RunWithElevatedPrivileges.Chris Farmer
when I comment above code then user without admin access is also able to upload..I am not getting the exact line where it is failing. below is the for RunWithElevatedPrivileges, SPSecurity.RunWithElevatedPrivileges(delegate() { });Rushikesh
Your last comment makes zero sense. Why can't you tell which line is failing? And what is being run in the elevated block? Surely you don't mean that you're running nothing inside the elevated block, right?Chris Farmer
Hi, I have added RunWithElevatedPrivileges in the question.. We are also not able to track and trying to find, because of which exact line the code is failing. Thanks for taking time..Rushikesh
I still don't get it. Don't you get a stack trace with the exception?Chris Farmer

1 Answers

0
votes

Instead of using the SPContext of the web object you need to create a new site and web object under the elevated privileges.

SPWeb web=SPControl.GetContextWeb(Context)

You need to use this under your elevated permissions:

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
   using(SPWeb web = site.OpenWeb())
   {
      ....
   }
}