[英]How to Overwrite SharePoint's Modified Date after Doing a SaveBinaryDirect?
I can push a file to SharePoint, but the modified date is always equal to the exact time I uploaded it.我可以将文件推送到 SharePoint,但修改日期始终等于我上传的确切时间。 How can I overwrite the modified date to be equal to the value of the file?
如何覆盖修改后的日期以等于文件的值? The code below was my failing guess after many searches.
下面的代码是我多次搜索后的失败猜测。
Using "TimeLastModified" seems to error as non-existent while "Modified" seems to not error but the subsequent ExecuteQuery then gives an unknown error.使用“TimeLastModified”似乎不存在错误,而“Modified”似乎没有错误,但随后的 ExecuteQuery 会给出未知错误。
using (var ctx = new ClientContext(DataSharepointSite))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = GetCreds();
using (FileStream fs = new FileStream(localFile, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, sharepointFile, fs, true);
}
//****************
// Now somehow set Modified Date to local File's Modified Date
//****************
var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
ListItem lstItem = file.ListItemAllFields;
ctx.Load(lstItem);
ctx.ExecuteQuery();
lstItem["TimeLastModified"] = System.IO.File.GetLastWriteTime(localFile).ToUniversalTime();
lstItem.Update();
ctx.ExecuteQuery();
}
I was so close.我是如此接近。 Looks like the Load and first ExecuteQuery were also not needed.
看起来也不需要 Load 和 first ExecuteQuery。 Now I just don't know if it's read as local or UTC...
现在我只是不知道它是读作本地还是 UTC ......
lstItem["Modified"] = System.IO.File.GetLastWriteTime(localFile).ToUniversalTime().ToString();
Try the code snippet to update Modified field:尝试使用代码片段更新 Modified 字段:
var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
ListItem lstItem = file.ListItemAllFields;
ctx.Load(lstItem);
ctx.ExecuteQuery();
lstItem["Modified"] = System.IO.File.GetLastWriteTime(localFile).ToShortDateString();
lstItem.Update();
ctx.ExecuteQuery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.