【问题标题】:How can I find corrupt files in a SharePoint 2013 ListItemCollection using CSOM如何使用 CSOM 在 SharePoint 2013 ListItemCollection 中找到损坏的文件
【发布时间】:2017-02-26 16:23:02
【问题描述】:

我有一个 CSOM 程序,可以将数百个 PDF 文件传输到 SharePoint 2013 库中。有时,其中一个传输的文件将损坏并且无法打开。源文件很好,同一个文件转移到其他库后可以打开,但在一个随机库中它会损坏。 我想遍历库,找到损坏的文件并删除它们,但是如果文件损坏,我如何使用 CSOM 来判断?我尝试循环并使用 File.OpenBinaryStream() ,但在损坏的文件上成功了。下面是读取库并循环文件的代码。任何建议将不胜感激。

                    using (ClientContext destContext = new ClientContext(clientSite.Value))
                {
                    destContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
                    destContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(ClientSiteUsername, ClientSitePassword);

                    // get the new list
                    Web destWeb = destContext.Web;
                    ListCollection lists = destWeb.Lists;
                    List selectedList = lists.GetByTitle(clientLibraryName);
                    destContext.Load(lists);
                    destContext.Load(selectedList);
                    ListItemCollection clientCurrentItemsList = selectedList.GetItems(CamlQuery.CreateAllItemsQuery());

                    destContext.Load(clientCurrentItemsList,
                                       eachItem => eachItem.Include(
                                       item => item,
                                       item => item["ID"],
                                       item => item["FileLeafRef"]));

                    try
                    {
                        destContext.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        log.Warn(String.Format("Error in VerifyClientDocuments. Could not read client library: {0}", clientSite.Value), ex);
                        continue;
                    }

                    foreach (ListItem item in clientCurrentItemsList)
                    {
                        try
                        {
                            item.File.OpenBinaryStream();
                        }
                        catch (Exception ex)
                        {
                            var val = ex.Message;
                            //delete here
                        }

                    }
                }

【问题讨论】:

    标签: sharepoint-2013 csom


    【解决方案1】:

    有效的方法是根据预期文件大小检查新文件的大小。损坏的文件实际上报告了 0 字节大小,所以我删除了这些文件,然后再重新添加它们。

                                    for (var counter = clientCurrentItemsList.Count; counter > 0; counter--)
                                    {
    
                                    var clientFileSize = clientCurrentItemsList[counter - 1].FieldValues.
                                        Where(x => x.Key == "File_x0020_Size").FirstOrDefault().Value.ToString();
                                    var fileName = clientCurrentItemsList[counter - 1].FieldValues.
                                        Where(x => x.Key == "FileLeafRef").FirstOrDefault().Value.ToString();
                                    var serverFileSize = approvedDocumentList.Where(x => x.FieldValues["FileLeafRef"].ToString() == 
                                        fileName).FirstOrDefault().FieldValues["File_x0020_Size"].ToString();
    
                                    if (clientFileSize != serverFileSize)
                                    {
                                        clientCurrentItemsList[counter - 1].DeleteObject();
                                        destContext.ExecuteQuery();
                                        log.Info(String.Format("File [{0}] deleted from {1} File type {2} - Reason: Client file was corrupt",
                                            fileName, clientSiteURL, documentType.ToString()));
                                    }
                                }
    

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多