1

I have a custom solution that generates temporary junk files that I need to remove at the end of the process.

I was originally using the following code:

// Loops through the converted PDFs and deletes them from SP.
private void DeleteTempPDFs(List<SPListItem> convertedItems)
{
    foreach (SPListItem item in convertedItems)
    {
        item.Delete();
    }
}

I have since discovered that our standard users do not have permission to delete from the library so the code throws an exception when it tries to delete the junk files.

The solutions I can think of is either:

  • Temporarily grant the executing user delete permissions
  • Pass the code to a service account to execute

Which is the best practise approach in this case? And how would I go about achieving it?

EDIT

I have tried running the code with elevated permissions, this is the exception I am getting:

Debug Exception Error

3

You have to run the code with elevated privileges and you also need to specify a new context in which the code needs to be executed.

private void DeleteTempPDFs(List<SPListItem> convertedItems)
{
    // Elevate the privileges to run code as AppPool administrator
    SPSecurity.RunWithElevatedPrivileges(() => 
    {
        // Specify a new context with the elevated privileges
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        using (SPWeb web = site.OpenWeb())
        {
            // Now the items should be deleted.
            // EDITED:
            // You will need to reget the item too using the new context 
            // as it is passed in from out of the scope of the new context 
            foreach (SPListItem item in convertedItems) {
              web.Lists[item.ParentList.ID].GetItemById(item.ID).Delete();
            } 
        }
    });
}

Note
The using statements will automatically dispose the SPSite and SPWeb objects when the code has executed, only granting the users elevated permission briefly while the code is executing.

|improve this answer|||||
  • 1
    YOu will need to reget the item too as it is still coming from out of the scope of the new context, i.e. foreach (SPListItem item in convertedItems) { web.Lists[item.ParentList.ID].GetItemById(item.ID).Delete(); } – Colin Mar 4 '16 at 15:30
  • 1
    @Colin Feel free to update my answer :) – user2536 Mar 4 '16 at 15:31
  • This is the most complete answer, thank you. I had to add web.AllowUnsafeUpdates = true; before my foreach, but it now appears to work as intended. – Burgi Mar 4 '16 at 16:18
  • @DanielZiga done ;-) – Colin Mar 4 '16 at 17:48
  • @Burgi see my edit – Colin Mar 4 '16 at 17:48
1

TRY THIS

// Actually which deletes an item by System Account Permission   
// Loops through the converted PDFs and deletes them from SP.

private void DeleteTempPDFs(List<SPListItem> convertedItems)
{
  SPUtility.ValidateFormDigest();
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    foreach (SPListItem item in convertedItems)
    {
        item.Delete();
    }
  });
}
|improve this answer|||||
  • Unfortunately this doesn't work, see my edit. – Burgi Mar 4 '16 at 14:13
0

Use elevation of rights with this command around what you are trying to do

 SPSecurity.RunWithElevatedPrivileges(delegate()
  {
            //my elevated code
  });

Some reference if needed

|improve this answer|||||

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.