首页 > 解决方案 > 如何更新 Sharepoint CSOM 中的文件元数据?

问题描述

我知道过去曾有人问过这个问题,但我似乎找不到可行的解决方案。我想更新我使用 CSOM 上传的文件的 CreatedDateTime、ModifiedBy 和 CreatedBy 元数据。我有这个代码。它不会崩溃,但也不会更新属性。

            FileCreationInformation newFile = new FileCreationInformation();
            byte[] FileContent = null;
           
            using (var webClient = new WebClient())
            {
                webClient.Headers.Add("Authorization", authorization);
                FileContent = webClient.DownloadData(file.DowlnloadUrl);
            }

            newFile.ContentStream = new System.IO.MemoryStream(FileContent);
            newFile.Url = System.IO.Path.GetFileName(file.Name);
            Microsoft.SharePoint.Client.File sharepointFile = _spFolder.Files.Add(newFile);
            _context.ExecuteQuery();

            sharepointFile.ListItemAllFields["Modified"] = DateTime.Today.AddDays(-3);
            sharepointFile.ListItemAllFields["Created"] = DateTime.Today.AddDays(-3);
            sharepointFile.ListItemAllFields.Update();
            _context.ExecuteQuery();

该文件仍然显示它是几秒钟前创建/修改的。 在此处输入图像描述

任何帮助,将不胜感激。PS:如果这可以一次性完成(一个单一的 ExecuteQuery),那就太好了。

标签: c#sharepointsharepoint-onlinecsom

解决方案


我最终找到了一个有效的代码片段。请注意,这仅在您用于生成上下文的帐户是Sharepoint 管理员(具有 SP 管理员角色,而不仅仅是全局管理员角色)时才有效。成为该网站的所有者也是不够的。

代码可以在这里找到: https ://www.c-sharpcorner.com/blogs/update-a-sharepoint-listitem-without-increasing-its-item-file-version-using-sharepoint-client-side-modelcsom


推荐阅读