Sharepoint 2013 如何使用CSOM在SharePoint 2013 ListItemCollection中查找损坏的文件
我有一个CSOM程序,可以将数百个PDF文件传输到SharePoint 2013库中。偶尔会有一个传输的文件被损坏,无法打开。源文件良好,同一文件在传输到其他库后可以打开,但在一个随机库中,它将被损坏。 我想在库中循环,找到损坏的文件并删除它们,但是如何使用CSOM判断文件是否损坏?我尝试循环使用File.OpenBinaryStream(),但在损坏的文件上成功了。下面是读取库并循环文件的代码。如有任何建议,将不胜感激Sharepoint 2013 如何使用CSOM在SharePoint 2013 ListItemCollection中查找损坏的文件,sharepoint-2013,csom,Sharepoint 2013,Csom,我有一个CSOM程序,可以将数百个PDF文件传输到SharePoint 2013库中。偶尔会有一个传输的文件被损坏,无法打开。源文件良好,同一文件在传输到其他库后可以打开,但在一个随机库中,它将被损坏。 我想在库中循环,找到损坏的文件并删除它们,但是如何使用CSOM判断文件是否损坏?我尝试循环使用File.OpenBinaryStream(),但在损坏的文件上成功了。下面是读取库并循环文件的代码。如有任何建议,将不胜感激 using (ClientCont
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
}
}
}
有效的方法是根据预期的文件大小检查新文件的大小。损坏的文件实际上报告为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()));
}
}
有效的方法是根据预期的文件大小检查新文件的大小。损坏的文件实际上报告为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()));
}
}