locked
RunWithElevatedPrivileges doenst work RRS feed

  • Question

  • I want to do job with admin account, so i put it inside:

     

     SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.OpenWeb().Site.ID))
                    {

    //my job here

                    }

                 }

     

    But it doesnt run my job with admin acc, it still uses account of the one that has triggered an event handler. Why ?

    Tuesday, March 29, 2011 1:10 PM

Answers

  • Hi,

    You are opening site with elevated privileges but then using properties.ListItem without elevation. 

    You have to get ListItem again under admin account. Something like that

    SPWeb web = site.OpenWeb();
    SPList list = web.Lists[properties.ListItem.ParentList.Id];
    SPListItem item = list.GetItemById(properties.ListItem.ID);
    

    Hope it helps.

    Regards,

     


    Vladimir
    MCTS, SharePoint tips blog: http://buyevich.blogspot.com
    Tuesday, March 29, 2011 1:39 PM
  •                    SPListItem myitem = properties.ListItem;

     

    I'm assuming properties is an SPContext.

    That object was not created inside the elevated code block, It was created as the logged in user, therefore you get access denied.

    In order for any object to get the Access rights of the elevated code, it has to be Instantiated Inside the elevated code block.

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

        SPSite parentSite = properties.Feature.Parent;

           using (SPSite site = new SPSite(parentSite.ID))

           {

                SPContext elevatedContext = SPContext.GetContext(site);

                //do something

           }

    });


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction
    Tuesday, March 29, 2011 1:46 PM

All replies

  •  

    Expanded one:

     

                SPUtility.ValidateFormDigest();

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite site = new SPSite(properties.OpenWeb().Site.ID))

                    {

                        SPListItem myitem = properties.ListItem;

                        try

                        {

                            SPFieldUser userField = (SPFieldUser)myitem.Fields.GetFieldByInternalName("AssignedTo");                       

                            SPFieldUserValueCollection fieldValueCollection = new SPFieldUserValueCollection();

                                                                            

                            try

                            {

                                fieldValueCollection = (SPFieldUserValueCollection)userField.GetFieldValue(myitem[myitem.Fields.GetFieldByInternalName("AssignedTo").ToString()].ToString());

                            }

                            catch                        {                                                        

                                SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(myitem[myitem.Fields.GetFieldByInternalName("AssignedTo").ToString()].ToString());

                                fieldValueCollection.Add(fieldValue);

                            }                                            


                            foreach (SPFieldUserValue fieldValue in fieldValueCollection)

                            {

                                try

                                {

    // cast item security settings here

                                }

                                catch { }

                            }

                        }

                        catch

                        { }                 

                    }

                });

     

    Tuesday, March 29, 2011 1:28 PM
  • Hi,

    You are opening site with elevated privileges but then using properties.ListItem without elevation. 

    You have to get ListItem again under admin account. Something like that

    SPWeb web = site.OpenWeb();
    SPList list = web.Lists[properties.ListItem.ParentList.Id];
    SPListItem item = list.GetItemById(properties.ListItem.ID);
    

    Hope it helps.

    Regards,

     


    Vladimir
    MCTS, SharePoint tips blog: http://buyevich.blogspot.com
    Tuesday, March 29, 2011 1:39 PM
  •                    SPListItem myitem = properties.ListItem;

     

    I'm assuming properties is an SPContext.

    That object was not created inside the elevated code block, It was created as the logged in user, therefore you get access denied.

    In order for any object to get the Access rights of the elevated code, it has to be Instantiated Inside the elevated code block.

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

        SPSite parentSite = properties.Feature.Parent;

           using (SPSite site = new SPSite(parentSite.ID))

           {

                SPContext elevatedContext = SPContext.GetContext(site);

                //do something

           }

    });


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction
    Tuesday, March 29, 2011 1:46 PM