2

Please guide me with code sample to get list item in CSOM c# for all fields

I loaded the client context for list item and only the title field is retrieved

Other fields like the choice field are not retrieved.

Error

The property or field has not been initialized

Please help me!

3
  • 1
    Please try to post your code to can help you faster Commented Aug 27, 2017 at 23:56
  • 1
    can you share which all type field you want to retrieve, text and choice field?
    – Anuja
    Commented Aug 28, 2017 at 5:28
  • The list has three fields with different data type, i am able to get only the title field Commented Aug 28, 2017 at 8:25

1 Answer 1

3

You can modify following code to fetch all fields of your item:

        var list = _clientContext.Web.GetListByUrl(<ListUrl>);
        var query = new CamlQuery
        {
            ViewXml = @"<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Home</Value></Eq></Where></Query></View>"
        };
        var listItems = list.GetItems(query);
        _clientContext.Load(listItems);
        _clientContext.ExecuteQuery();

GetListByUrl() is extension method from Office PnP Core library - if you do not use it, you can replace it with following code:

_clientContext.Web.Lists.GetByTitle()

In provided sample I was filtering by Title field - if you specify your query as follows:

var query = new CamlQuery();

you will fetch all items and all of their fields.

Hope it will help:)

UDPATE

Getting items with IDs only can be done this way:

        var query2 = new CamlQuery();
        var listItems2 = list.GetItems(query2);
        _clientContext.Load(listItems2, items => items.Include(i => i.Id).Where(i => i.Id == 1));
        _clientContext.ExecuteQuery();

Where statement can be removed if you want to get all items with theirs' ids. This approach is a bit different than previous one - I do not use ViewXml of CamlQuery, but output is exactly the same.

2
  • Two concerns I'm having: why you use value in viewxml, 2.how can I get only list item with it's ID? Commented Aug 28, 2017 at 8:19
  • I used ViewXml to limit result of the query to single element matching my requirements.
    – rafter13
    Commented Aug 28, 2017 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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