8

I want to load all FieldValues for a specific ListItem

ForExample

  public ListItem GetItemById(List SPList, int id, string[] fieldNames)
        {
            using (Ctx)
            {
                ListItem item = SPList.GetItemById(id);
                if (fieldNames != null)
                {
                    foreach (string field in fieldNames)
                    {
                        Ctx.Load(item, i => i[field]);
                    }                   
                } 
                //Tried below line, doesn't work
                //Ctx.Load(item, i => i.FieldValues);            
                Ctx.ExecuteQuery();
                return item;
            }

So in above example i am passing the field names to load, however i want all fields to be loaded.

6 Answers 6

4

None of the existing answers worked for me. This does:

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", itemId);
ListItemCollection collListItem = list.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.Load(collListItem,
      items => items.Include(
          item => item.Id,
          item => item.DisplayName,
          item => item.HasUniqueRoleAssignments,
          item => item.Folder,
          item => item.File,
          item => item.ContentType
          ));
clientContext.ExecuteQuery();

...

4
  • that would work if it was my answer ;) Commented Aug 13, 2019 at 12:31
  • Why are you loading collListItem twice?
    – Emaborsa
    Commented Mar 5, 2020 at 9:45
  • i believe that is a typo. but i don't have an environment handy to test if that's true or not. Commented Mar 16, 2020 at 20:42
  • I dont think it is a typo, because the first line loads all fields, the second one just the "included" ones
    – Emaborsa
    Commented Mar 16, 2020 at 20:53
2

According to my research, List.GetItemById will naturally return all the fields of the item as well as their values, while other methods of getting a list item will not.

For example: List.GetItemById Will Return Item with All Fields

1
  • 3
    No it doesn't load all values Commented Jul 1, 2016 at 12:07
0

try this

var item = list.GetItemById(id);
                _context.Load(item);
            _context.ExecuteQuery();
1
  • No field values are loaded this way.
    – Shai Petel
    Commented Aug 2, 2019 at 16:30
0

The following CSOM worked from my desktop. Change parameter as needed...

Import-Module ‘C:\Program Files\Common Files\microsoft shared\SharePoint Client\Microsoft.SharePoint.Client.dll’

$site = 'https://inside.nov.com/mfg/QC'
$context = [System.Net.CredentialCache]::DefaultNetworkCredentials
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$context.Credentials = $credentials
$list = $context.Web.Lists.GetByTitle('Report Issues')
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Context.Load($ListItems)
$Context.ExecuteQuery()    


write-host "Total Number of List Items found:"$ListItems.Count

# get required fields from this list

$fields = $list.Fields
$Context.Load($fields)
$Context.ExecuteQuery() 

foreach ($field in $fields)
{
    if($field.Required -eq $true)
    {
       write-host $field.Title
       #write-host $field.InternalName
       #write-host $field.StaticName
    }
} 
1
  • This does not appear to load the fields and values of the specified list item, which is what was requested. Commented Aug 7, 2020 at 13:27
0
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>";
ListItemCollection listItemCollection = srcList.GetItems(camlQuery);
srcContext.Load(listItemCollection);
await srcContext.ExecuteQueryAsync();

Querying for FSObjType=0 returns all items with all fields populated! See: https://blog.velingeorgiev.com/sharepoint-online-csom-all-list-items-content-caml-query

The query gets all list items. May you just could add an AND clause to the query to get a specific item.

-2

One should try following code

var item = list.GetItemById(id);
spContext.Load(item);
spContext.Load(item.Fields);
spContext.ExecuteQuery();

This will load all the fields of the given list item.

1
  • 2
    It seems that a list item in this context does not have a Fields collection so it can't be loaded this way. Commented Apr 15, 2018 at 8:25

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.