【问题标题】:SharePoint API ClientContextLoad property not initializedSharePoint API ClientContextLoad 属性未初始化
【发布时间】:2015-08-27 04:04:35
【问题描述】:

我有以下代码(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(字符串 道具名称)

为什么会出现此错误?我在上下文的 Load 方法中定义的那个属性不是已经初始化了吗?

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    你应该先加载网络上下文

    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();
    }
    

    【讨论】:

    • 我修改了我的代码以加载 Web 对象并在访问此属性之前执行该查询,但在尝试访问 CheckOutType 属性时仍然遇到相同的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2021-06-21
    • 2017-03-03
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多