简体   繁体   中英

Delete an item on a Sharepoint List (C#)

I want to read an Item from a Document Library and then Delete it. The problem I get a Security Exception: "The security validation for this page is invalid"

What am I doing wrong? I'm executing the commands with Elevated Privilages!

 SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 using (SPSite oSiteCollection = new SPSite(SharePointInfo.SubSiteUrl))
                 {
                     using (SPWeb oWebsite = oSiteCollection.OpenWeb())
                     {
                         SPList uploadFilesLibrary = oWebsite.Lists[SharePointInfo.UploadFilesLibraryName];

                         if (files.Count > 0)
                         {
                             foreach (var fileToSend in files)
                             {

                                 try{
                                     /*SPFile file = uploadFilesLibrary.Items.Cast<SPListItem>()
                                                                    .Where(x => x.Name.Equals(fileToSend))
                                                                    .Select(x => x.File).First();*/
                                     SPListItem p = uploadFilesLibrary.Items.Cast<SPListItem>()
                                                                    .Where(x => x.Name.Equals(fileToSend)).First();


                                     byte[] binaryFile = p.File.OpenBinary(); 
                                     p.Delete();
                                     aux = new FileAttachesForm(fileToSend, System.Convert.ToBase64String(binaryFile));
                                     rtn.Add(aux);
                                 }catch (Exception ex){
                                      string errMessage = string.Format("Error al descargar el fichero desde SP: {0} - Pila: {1}", ex.Message, ex.StackTrace);
                                      Logger.LogError(errMessage, ex);
                                      throw ex;
                                 }
                             }
                         }
                     }
                 }
             });
            return rtn;
        }

Try adding this inside of your if :

oSiteCollection.AllowUnsafeUpdates = true;

And take a look at this related question:

SharePoint Security Validation Issue while updating metadata (The security validation for this page is invalid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM