首页 > 解决方案 > C# SharePoint 异常“Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException”当 GetFileByServerRelativeURL

问题描述

我得到了PropertyException这段代码的第二部分。第一部分按预期上传文件。之后context.ExecuteQuery();我得到:

“uploadedFile.CheckInComment”引发了“Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException”类型的异常

我不确定为什么上下文应该没问题,因为它确实上传了文件。

我将尝试更新我刚刚上传的文档上的一些元数据字段。

    Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/");
FileCreationInformation newFile = new FileCreationInformation
{
    Content = System.IO.File.ReadAllBytes(@p),
    Url = Path.GetFileName(@p),
    Overwrite = true
};

currentRunFolder.Files.Add(newFile);
currentRunFolder.Update();
context.ExecuteQuery();
newUrl = siteUrl + barRootFolderRelativeUrl + "/" + Path.GetFileName(@p);

// Set document properties
Microsoft.SharePoint.Client.File uploadedFile = context.Web.GetFileByServerRelativeUrl(newUrl);
ListItem listItem = uploadedFile.ListItemAllFields;
listItem["TestEQCode"] = "387074";
listItem.Update();
context.ExecuteQuery();

标签: c#sharepointlistitem

解决方案


好的,即使 ListItems 为 NULL,我也可以设置 TestEQCode 并进行更新,并且该字段正在 SharePoint 端进行更新。一直以来,我都在关注 ListItems 获取实际元数据列表,但实际上它并不需要。我只需要对这些项目进行硬编码,它就会更新。–


推荐阅读