0

I'm trying to move documents set with as much history data as possible from one library to another through event receiver triggered by user setting an appropriate attribute. While executing a method

toDS.Folder.Files.Add(urlOfFile, file, properties, createdBy, modifiedBy, timeCreated, timeLastModified, checkInComment, overwrite)

I get exception E_ACCESSDENIED

If I use

toDS.Folder.Files.Add(urlOfFile, file, properties, true)

it executes properly, but it does not reproduce history data. If I use admin account, the call to a method setting history data succeeds. I would like all Contribute uses to be able to use this functionality. What should I do? I am not skilled in Sharepoint administration.

0

You could use the SPSecurity.RunWithElevatedPrivileges method which uses impersonation i.e means to execute the code, it has higher access (application pool account) than the logged-in actual user.

Example:

SPSite  site = new SPSite("siteURL");

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    //using (SPWeb web = site.OpenWeb())
    //{
        //string user = web.CurrentUser.Name;
        //Operations that need high level access.
   //} 

 Inject all your code here.

});

Refer to the below articles for how to use this:

Impersonation and Elevation of Privilege

When and How to Use Run With Elevated Privileges (RWEP) in SharePoint

How to use SPSecurity.RunWithElevatedPrivileges in event receiver Example ?

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SharePoint.Development.EventReceivers
{
    ///
    /// List Item Events
    ///
    public class ItemCheckedInEventReceiver : SPItemEventReceiver
    {
        private string newDocumentUrlExample = "http://newfilelocation/document.doc";

        public override void ItemCheckedIn(SPItemEventProperties properties)
        {
            base.EventFiringEnabled = false; // Disable event receiver firing so that we do not accidentally kick off a different thread
            try
            {
                // Check if user has a certain set of permissions, i.e. can they delete items? if not, then we will have to RunWithElevatedPrevileges
                if (properties.Web.DoesUserHavePermissions(properties.UserLoginName, SPBasePermissions.DeleteListItems))
                {
                    SPFile file = properties.ListItem.File; // Get the file
                    file.MoveTo(newDocumentUrlExample); // Move it to the new location
                }
                else
                {
                    RunWithElevatedPrivileges(properties, newDocumentUrlExample); // User doesn't have required permissions and therefore the code must be executed with Elevated perms.
                }
            }
            catch (Exception exception)
            {
                // Write exception to ULS and Event Logs
            }
            finally
            {
                base.EventFiringEnabled = true;
            }
        }
        public static void RunWithElevatedPrivileges(SPItemEventProperties properties, string newURL)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    // Everything executed here will be executed under the AppPool account and will have full permissions.. so becareful!!
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(properties.Web.ID))
                        {
                            SPList list = web.Lists[properties.ListId];
                            SPListItem listItem = list.Items.GetItemById(properties.ListItemId);
                            SPFile file = listItem.File;
                            file.MoveTo(newURL);
                        }
                    }
                });
            }
            catch (Exception exception)
            {
                // Write exception to ULS and Event Logs
            }
        }
    }

Reference:

Execute Code In SharePoint With Elevated Privileges

| improve this answer | |
  • I did what you have proposed - newweb = properties.Site.OpenWeb(), used newweb to re-read the folder and tried to add file to that folder. To my surprise, the result is the same. – Furli Jun 24 at 13:29
  • Please share your code for better understanding – Habibur Rahaman Jun 24 at 13:58
  • Refer to the updated example - How to use SPSecurity.RunWithElevatedPrivileges in event receiver Example? – Habibur Rahaman Jun 24 at 14:02
  • Sorry for late reply. I was out of office for few days. I have already tried to send the code, but the space for comment is very limited in this forum and I do not want to answer to my question just to add additional info. What is correct way to send it? – Furli Jun 29 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.