简体   繁体   English

删除共享点列表(C#)上的项目

[英]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 : 尝试将其添加到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) 更新元数据时,SharePoint安全验证问题(此页面的安全验证无效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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