如何使用CSOM

时间:2016-10-17 15:41:37

标签: sharepoint-2013 csom

我有一个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
                        }

                    }
                }

1 个答案:

答案 0 :(得分:0)

根据预期的文件大小检查新文件的大小是有效的。损坏的文件实际报告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()));
                                }
                            }