0

When i trying to delete listItem from list and anonymous access in site perm(for Fund3) is disabled my code throw exception. Where anonymous access is enable everything is OKAY-> items are deleted.....

Exception:

Error message: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(Unauthoriz edAccessException ex) at Microsoft.SharePoint.Library.SPRequest.GetListsWithCallback(String bstrUrl , Guid foreignWebId, String bstrListInternalName, Int32 dwBaseType, Int32 dwBase TypeAlt, Int32 dwServerTemplate, UInt32 dwGetListFlags, UInt32 dwListFilterFlags , Boolean bPrefetchMetaData, Boolean bSecurityTr Error stackTrace: at Microso ft.SharePoint.SPGlobal.HandleComException(COMException comEx) at Microsoft.SharePoint.Library.SPRequest.DeleteItem(String bstrUrl, String b strListName, Int32 lID, UInt32 dwDeleteOp, Boolean bUnRestrictedUpdateInProgress , Guid& pgDeleteTransactionId) at Microsoft.SharePoint.SPListItem.DeleteCore(DeleteOp deleteOp) at Microsoft.SharePoint.SPListItem.Delete() at ClearLibsInFund.Program.b__0()

My code:

 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
     try
     {
         using (var oSite = new SPSite(args[0])) //argument for Console application
         {
             using (var oWeb = oSite.OpenWeb())
             {
                 oWeb.AllowUnsafeUpdates = true;

                         var list = oWeb.Lists.TryGetList(listName);

                         var queryItems = new SPQuery();
                         queryItems.ViewXml = "<View Scope='Recursive'></View>";

                         var items = list.GetItems(queryItems);

                        //Option 1
                         for (int i = items.Count - 1; i >= 0; i--)
                         {
                             items[i].Recycle();
                         }

                         //Option 2
                         //foreach (SPListItem listitem in items)
                         //{
                         //    SPListItem itemToDelete = list.GetItemById(listitem.ID);
                         //    itemToDelete.Recycle();
                         //}

                         //Option 3
                         //while (items.Count > 0)
                         //{
                         //    items[0].Recycle();
                         //}

                        oWeb.AllowUnsafeUpdates = false;
                 }

             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error message: " + ex.Message + " Error stackTrace: " + ex.StackTrace);
     }
 });

Where is the problem, because i run code with RunWithElevatedPrivileges and which option to delete must use(best practice)...

  • 1
    Are you running your console app as an Administrator (Right click --> "Run as administrator")? – Cecilia Jan 6 '17 at 20:46
  • 1
    Option n.1 is ok, but it's better if you get item count firstrly (better for performance): var itemCount = items.Count; for (int i = itemCount - 1; i >= 0; i--) – Cecilia Jan 6 '17 at 20:49
  • yes i running cmd as an Administrator... and pass url as argument... When i use cmd with another site where anonymous access is disable... is all OKay... I dont know the reason.. – Goshky Jan 6 '17 at 22:45
-1

I don't know why it doesn't work in case Anonymous is disabled... But in all cases, can you try without RunWithElevatedPrivileges and rather with SPUserToken.SystemAccount, and use

try
{
   using (var oSite = new SPSite("http://dd-spdev1:23840/EN/HR/DKC/Fund3/", SPUserToken.SystemAccount))
   {
      using (var oWeb = oSite.OpenWeb())
      {
         oWeb.AllowUnsafeUpdates = true;
         var list = oWeb.Lists.TryGetList(listName);
         . . .

Also: why do you have a hard-coded URL http://dd-spdev1:23840/EN/HR/DKC/Fund3? Does it mean you're not targetting the current SPWeb your Web part (or user control) is running in? That could also be a problem...

And about the ways to delete items in a loop, I personally like the option #1.

EDIT
OK, you said in a comment a console app it is.
That's a completely different story. Regarding security and access control, a console app has nothing to do with a Web context:

  1. Connections to DB will be made with under the identity of the user who launches the app (usually the user who logged on the server... ho yes, a console app making use of the server-side API must be used on the SharePoint server, not remotely)
  2. --> No need for privileges elevation. Its completely useless since there's no context to go back under.
  3. --> The user who runs the app must have a read-write access to the SQL content DB!
  4. I think AllowUnsafeUpdates is not needed in a console app context.
  5. The user must also have permissions at the SharePoint.
  6. See some other inputs I wrote in this answer, that may also apply here.
  • URL is hardcoded for the exam, URL is args[0], because VS project is C# console application – Goshky Jan 6 '17 at 17:26
  • see my updated code – Goshky Jan 6 '17 at 17:28
  • Ho, concole app is anoher story... – Evariste Jan 6 '17 at 18:03

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.