使用 clientcontext.executequery() 上载文档和更新 SharePoint 库中的元数据时,该



我使用 CSOM 编写了一个程序,将文档上传到 SharePoint 并将元数据插入属性。 每隔一段时间(如每 3 个月(SharePoint服务器会变忙,或者我们重置IIS或它可能出现的任何其他通信问题,我们会在clientContext.ExecuteQuery((上收到"操作已超时"错误。为了解决这个问题,我为 ExecuteQuery 编写了一个扩展方法,每 10 秒尝试一次 5 次以连接到服务器并执行查询。我的代码在 Dev 和 QA 环境中没有任何问题,但在 Prod 中,当它第一次失败并出现超时错误时,在第二次尝试中,它只上传文档但不更新属性,并且库中的所有属性都是空的。它不会作为 ExecteQuery(( 的结果返回任何错误,但似乎从批处理中的两个请求正在上传文件和更新属性来看,它只是上传,我不知道属性会发生什么。它在第二次尝试中有点从批处理中删除它!

我使用了两种上传方法文档。RootFolder.Files.Add 和 File.SaveBinaryDirect 在我的代码的不同部分,但我在这里只复制其中一个,以便您可以看到我的代码中的内容。 我感谢您的帮助。

public static void ExecuteSharePointQuery(ClientContext context)
{
int cnt = 0;
bool isExecute = false;
while (cnt < 5)
{
try
{                    
context.ExecuteQuery();
isExecute = true;
break;
}
catch (Exception ex)
{
cnt++;
Logger.Error(string.Format("Communication attempt with SharePoint failed. Attempt {0}", cnt));
Logger.Error(ex.Message);
Thread.Sleep(10000);
if (cnt == 5 && isExecute == false)
{
Logger.Error(string.Format("Couldn't execute the query in SharePoint."));
Logger.Error(ex.Message);
throw;
}
}
}
}
public static void UploadSPFileWithProperties(string siteURL, string listTitle, FieldMapper item)
{
Logger.Info(string.Format("Uploading to SharePoint: {0}", item.pdfPath));
using (ClientContext clientContext = new ClientContext(siteURL))
{
using (FileStream fs = new FileStream(item.pdfPath, FileMode.Open))
{
try
{
FileCreationInformation fileCreationInformation = new FileCreationInformation();
fileCreationInformation.ContentStream = fs;
fileCreationInformation.Url = Path.GetFileName(item.pdfPath);
fileCreationInformation.Overwrite = true;
List docs = clientContext.Web.Lists.GetByTitle(listTitle);
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(fileCreationInformation);
uploadFile.CheckOut();
//Update the metadata
ListItem listItem = uploadFile.ListItemAllFields;
//Set field values on item
foreach (List<string> list in item.fieldMappings)
{
if (list[FieldMapper.SP_VALUE_INDEX] != null)
{
TrySet(ref listItem, list[FieldMapper.SP_FIELD_NAME_INDEX], (FieldType)Enum.Parse(typeof(FieldType), list[FieldMapper.SP_TYPE_INDEX]), list[FieldMapper.SP_VALUE_INDEX]);
}
}
listItem.Update();                       
uploadFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
SharePointUtilities.ExecuteSharePointQuery(clientContext);                      
}
catch (Exception ex)
{
}
}
}
}

我有太多可能的理由真正评论解决方案,特别是考虑到它只在生产环境中。

我能说的是,保留对上次上传文件的引用可能是最简单的。如果代码失败,请检查最后一个文件是否已正确上传。

旁注:我不确定这是否相关,但如果它是一个大文件,您想将其切片上传。

最新更新