1

I'm stuck to something probably very basics

So the idea is the following: I have a list with 4 columns : Title, CostCenter, City, currency

Via CSOM, I'd like to read this list, so I did something like that:

ClientContext SPcontext = new ClientContext("MySharepointsite");
         Web web = SPcontext.Web;

         List list = web.Lists.GetByTitle("ListTest");
         CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
         ListItemCollection items = list.GetItems(query);
         SPcontext.Load(list);
         SPcontext.Load(items);
         SPcontext.ExecuteQuery();

         foreach (ListItem listItem in items)
         {

              Console.Writeline(FieldName = listItem["Title"].ToString());
              Console.Writeline(listItem["City"].ToString());

         }

But it returns me the following error message:

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: '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.'

I know that it's comming from my line:

Console.Writeline(listItem["City"].ToString());

because I remove it, it works

Any idea of how could I do that?

Thank you for your help :)

0

Try replacing this line:

SPcontext.Load(items);

with this:

SPcontext.Load(items, its => its.Include(item => item["currency"], item => item["Title"], item => item["CostCenter"], item => item["City"]));

That way you are asking for specific columns to be downloaded from server.

0

Since City is not a default list field, you will have to specifically include it in your load call.
Title, CostCenter, City, currency

SPcontext.Load(items, items => items.include(item => item["Title"], item => item["City"], item => item["CostCenter"], item => item["currency"]));

Any time custom columns are being retrieved, they have to be specifically asked for.

as a template

SPcontext.Load(<ItemCollection>, <Variable for item collection> => <variable for item collection>.include(<variable for item> => <variable for item>["<field name"], <variable for item> => <variable for item>["<field name"]));

FOR EXAMPLE

SPcontext.Load(itemCollection, col => col.include(i => i["Title"], i => i["Description"]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.