c# - 使用 ClientContext 在 SharePoint(在线)文件传输期间保留修改日期
问题描述
如果 SharePoint 有较新的文件副本,我实际上是在尝试从 SharePoint 下载,或者如果我的文件较新,则推送我的。我从不关心合并此应用程序中的更改。更新更好,因为数据都是来自服务器的原始查询。也就是说,这就是我正在做的比较哪个文件更新并相应地上传或下载。问题是,每次上传和每次下载都会使“修改日期”等于我进行传输的确切时间。我发现我可以从 SharePoint 获取修改后的日期并覆盖 Windows 时间戳,但我不确定如何采用其他方式。如果有办法根据对我当前代码的影响来保留价值,我也会接受。那么我怎样才能保留或修改它呢?
using (var ctx = new ClientContext(DataSharepointSite))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = GetSharepointCredentials();
var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
var fileData = file.OpenBinaryStream();
ctx.Load(file);
ctx.ExecuteQuery();
using (var sr = new StreamReader(fileData.Value))
{
DateTime lastDataUpdate = System.IO.File.GetLastWriteTime(localFile).ToUniversalTime();
DateTime ShareDate = file.TimeLastModified.ToUniversalTime();
// If SharePoint has a newer file, download it
if (ShareDate > lastDataUpdate)
using (FileStream fs = new FileStream(localFile, FileMode.Create))
{
sr.BaseStream.CopyTo(fs);
}
// Forced to overwrite modified date to match SharePoint
System.IO.File.SetLastWriteTime(localFile, ShareDate);
}
// else if local copy is newer, push to SharePoint
else if (ShareDate < lastDataUpdate)
{
using (FileStream fs = new FileStream(localFile, FileMode.Open))
{
// This, too, is getting a new "modified date" so next compare will re-download
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, sharepointFile, fs, true);
}
}
}
解决方案
file.ListItemAllFields["Modified"] = System.IO.File.GetLastWriteTime(localFile);
file.ListItemAllFields.Update();
ctx.ExecuteQuery();
推荐阅读
- javascript - Node.js Discord.js 从 ID v11 向频道发送消息
- visual-studio-code - Visual Studio Code 从 HTML 标记中删除结束斜杠
- node.js - npm-check-updates (npm ncu):避免中断更新
- cypress - 在部署上运行赛普拉斯测试
- perl - Encode.c:可加载库和 perl 二进制文件不匹配(获得握手密钥 0xc500080,需要 0xc400080)
- c++ - 如何最好地使用 #include 来防止我的多重定义错误?
- python-3.x - 如何在 Python 中附加到数据框单元格内的列表?
- r - 将变量拉入新的数据框
- python - 使脚本搜索文件,如果找到则读取文件。蟒蛇3.5
- python - 删除数据框中公共列的内容