Sharepoint field has not been initialized in C#

24,249

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

Edit: 8 years later, I haven't used SharePoint in a long while, but now that I look at this answer, I think it would be much better not to call context.ExecuteQuery() in the loop. You should probably use a first loop to .Load every field of interest, then call .ExecuteQuery, and finally do another loop to do stuff with your newly loaded fields.

When it comes to web requests, try to be bulky, not chatty: strive to limit the amount of requests you do (as long as it makes sense, of course).

Share:
24,249
user3158291
Author by

user3158291

Updated on December 21, 2020

Comments

  • user3158291
    user3158291 over 3 years

    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();
    }
    
  • user3158291
    user3158291 about 10 years
    I tried that but its now giving me an error at the ExecuteQuery after loading the field... "Item does not exist. It may have been deleted by another user.". Any suggestions?
  • Kilazur
    Kilazur about 10 years
    This is most likely a entire other problem. Have a look here stackoverflow.com/questions/15790926/… and search for permission issues, I sadly didn't work much with this side of SharePoint.
  • user3158291
    user3158291 about 10 years
    I'm not sure if thats the problem. The account i'm using has full control over the site and the list.
  • Kilazur
    Kilazur about 10 years
    So maybe a ID issue stackoverflow.com/questions/6672604/… or stackoverflow.com/questions/13184873/… ... But really, I can't just keep googling those for you ;p Once more, it seems like another problem, maybe you should start a new question.
  • user3158291
    user3158291 about 10 years
    Your earlier advice worked. You're right the second problem was with my sharepoint list. Thanks!