0

Any idea why the follow code is not working in item added?

Error:

<nativehr>0x80070005</nativehr><nativestack></nativestack>Access denied.

The code:

SPSite site = new SPSite("http://portal.xyz.com);
Guid siteId = site.ID;
SPWeb web = site.RootWeb;
Guid webId = web.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite osite = new SPSite(siteId))
    {
        using (SPWeb oweb = osite.OpenWeb(webId))
        {
            properties.ListItem.File.CopyTo(properties.Web.Site.Url + "/SecretDL/" + properties.ListItem.File.Name);
        }
    }
});
9
  • try using SPSite site = properties.Web.Site, SPWeb web = properties.Web instead of initializing objects as in above example. Feb 29, 2016 at 11:23
  • SPSite site = properties.Web.Site; Guid siteId = site.ID; SPWeb web = properties.Web; Guid webId = web.ID; File didn't copy.
    – bswee
    Feb 29, 2016 at 11:27
  • 0x80070005Access denied
    – bswee
    Feb 29, 2016 at 11:47
  • are you having access on that sharepoint list. On which your are writing code? I mean what rights are you having Feb 29, 2016 at 11:54
  • Yes. The test account has full access to the list with the event receiver running the RWEP code for item added.
    – bswee
    Feb 29, 2016 at 11:57

2 Answers 2

0

You created a spsite object with osite name inside RWEP

using (SPSite osite = new SPSite(siteId))

and used "site" name for opening web!

using (SPWeb oweb = site.OpenWeb(webId))

you should be using

using (SPWeb oweb = osite.OpenWeb(webId))
1
  • Thanks. I've fixed that and it still doesn't work
    – bswee
    Mar 1, 2016 at 3:59
0

Elevated list item is the answer. List item needs to be elevated in the RWEP block to avoid the access denied problem.

 SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite elevatedSite = new SPSite(properties.SiteId))
            {
                using (SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.Web.ID))
                {
                    SPList elevatedList = elevatedWeb.Lists[currentItem.ParentList.ID];
                    SPListItem elevatedListItem = elevatedList.GetItemById(currentItem.ID);
                    elevatedListItem.File.CopyTo(elevatedWeb.Site.Url + "/SecretDL/" + elevatedListItem.File.Name);       
                }                                                     
            }
        });  

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .