Question

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);
        }
    }
});
Was it helpful?

Solution 2

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);       
                }                                                     
            }
        });  

OTHER TIPS

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top