Sharepoint 字段尚未在 C# 中初始化 [英] Sharepoint field has not been initialized in C#

查看:21
本文介绍了Sharepoint 字段尚未在 C# 中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,它将遍历共享点列表中的每个列表项并查找一个空字段.如果发现空字段,则会通过电子邮件通知列表项的负责人.

I'm writing a code that will go through every list item in a sharepoint list and look for an empty field. If an empty field is found, the person responsible for the list item is notified by email.

我在 val = oListItem[field.Title]; 行出现错误,其中说明

I'm getting an error at the line val = oListItem[field.Title]; which states

属性或字段尚未初始化.尚未请求或尚未执行请求.可能需要明确请求.

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

在我看来,我已经在该行之前初始化了所有内容.

It seems to me that I have initialized everything before that line.

static void Main()
{
    ClientContext context    = new ClientContext("https://****");
    context.Credentials      = new NetworkCredential("****", "****");
    List oList               = context.Web.Lists.GetByTitle("TestBI");
    FieldCollection fieldcol = oList.Fields;

    context.Load(oList);
    context.Load(fieldcol);
    context.ExecuteQuery();

    ListItem oListItem = oList.GetItemById(1);
    object val = null;

    for (int i = 1; i <= 4; i++)
    {
        oListItem = oList.GetItemById(i);
        foreach (Field field in fieldcol)
        {
            val = oListItem[field.Title];
            if(val == null)
            {
                //Send e-mail
            }
        }
    }
    context.ExecuteQuery();
}

推荐答案

欢迎来到 SharePoint CSOM 地狱.

Welcome to SharePoint CSOM hell.

您确实加载了 List 和 FieldCollection,但您还必须加载每个 Field.事实上,您必须加载要从中获取属性的每个 SharePoint 对象.

You did load your List and FieldCollection, but you also have to load each Field. In fact, you have to load every SharePoint object from which you intend to get properties.

for (int i = 1; i <= 4; i++)
{
    oListItem = oList.GetItemById(i);

    foreach (Field field in fieldcol)
    {
        context.Load(field);
        context.ExecuteQuery();
        val = oListItem[field.Title];
        if(val == null)
        {
            //Send e-mail
        }
    }
}

这篇关于Sharepoint 字段尚未在 C# 中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆