Sharepoint field has not been initialized in C#

百般思念 提交于 2019-12-06 07:03:20

问题


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.

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

回答1:


Welcome to SharePoint CSOM hell.

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
        }
    }
}


来源:https://stackoverflow.com/questions/23515915/sharepoint-field-has-not-been-initialized-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!