locked
RunWithElevatedPrivileges with Access is denied RRS feed

  • Question

  • Hi,

    I've tried to delete a list item with elevated privileges. In SharePoint 2010 (windows authentication) everything works fine. In SharePoint 2013 (claims based authentication) the method throws an "Access is denied" exception in the line "SPList oDelList = oWeb.Lists[ParentListID];"

    public static void DeleteListItemElevated(SPListItem oItem)
    {
        Guid ParentListID = Guid.Empty;
        Guid ItemID = Guid.Empty;
        String strUrl = "";
    
        ItemID = oItem.UniqueId;
        ParentListID = oItem.ParentList.ID;
        strUrl = oItem.Web.Url;
    
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite oSiteCollection = new SPSite(strUrl))
            {
                using (SPWeb oWeb = oSiteCollection.OpenWeb())
                {
                    try
                    {
                        oWeb.AllowUnsafeUpdates = true;
    
                        SPList oDelList = oWeb.Lists[ParentListID];
                        SPListItem oDelItem = oDelList.GetItemByUniqueId(ItemID);
                        oDelItem.Delete();
    
                        oWeb.AllowUnsafeUpdates = false;
                    }
                    catch (Exception ex)
                    {
                        // Do something
                        throw;
                    }
                }
            }
        });
    }

    Any suggestion what's going wrong?

    Thursday, September 4, 2014 1:37 PM

Answers

All replies

  • are you sure that you need AllowUnsafeUpdates? I'd guess that about 99% of the time that I see it in use, it's unnecessary.

    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com
    Strategic Data Systems - for all your SharePoint needs

    Thursday, September 4, 2014 1:40 PM
  • hi

    open web as bellow:

    using (SPSite oSiteCollection = new SPSite(item.Web.Site.ID))
                    {
                        using (SPWeb oWeb = oSiteCollection.OpenWeb(item.Web.ID))
                        {
                        }
                    }


    yaşamak bir eylemdir

    Thursday, September 4, 2014 2:14 PM
  • Thanks for the feedback. When I try to open the web with the WebID the Access denied exception occurs in the line "using (SPWeb oWeb = oSiteCollection.OpenWeb(item.Web.ID))". How can this be? I'm running with a system account. If I login with this system account I can navigate to the web without any permission problem.
    Thursday, September 4, 2014 2:54 PM
  • Found the solution here http://absolute-sharepoint.com/2013/10/sharepoint-2013-apps-load-css-properly-403-defaultcss-ashx.html

    Unbelievable but reproducible! When I run the Web Application and the Application Pool with different accounts the problem occurs. When I run both with the same account is works.

    • Marked as answer by Harry_Ks Thursday, September 4, 2014 7:05 PM
    Thursday, September 4, 2014 7:04 PM