SharePoint API ClientContextLoad属性未初始化
本文关键字:初始化 属性 ClientContextLoad API SharePoint | 更新日期: 2023-09-27 18:10:07
我有以下代码(SharePointUpload是一个自定义类,我有一个包含Microsoft.SharePoint.Client.File对象):
private static bool LoadSharePointFile(SharePointUpload fileUpload)
{
ClientContext clientContext = HttpContextManager.SharepointClientContext;
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(fileUpload.FullSharepointPath);
clientContext.Load(file, f => f.CheckOutType, f => f.MajorVersion);
clientContext.ExecuteQuery();
fileUpload.File = file;
}
在这个方法返回之后,我立即像下面这样访问CheckOutType属性:
if (fileUpload.File.CheckOutType != CheckOutType.None)
{
...
}
我得到这个错误:
Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException:属性或字段尚未初始化。它还没有已请求或请求尚未执行。它可能需要这样做显式地请求。在Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(字符串propName)
为什么我得到这个错误?这个属性不是像我在context的Load方法中定义的那样初始化了吗?
你应该先加载web上下文
using (ClientContext ctx = ....))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl(fileUpload.FullSharepointPath);
clientContext.Load(file, f => f.CheckOutType, f => f.MajorVersion);
clientContext.ExecuteQuery();
}