6

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.

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

|improve this answer|||||
  • No it doesn't load all values – Kunal Valecha Jul 1 '16 at 12:07
2

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

...

|improve this answer|||||
  • that would work if it was my answer ;) – Nicholas DiPiazza Aug 13 '19 at 12:31
  • Why are you loading collListItem twice? – Emaborsa Mar 5 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. – Nicholas DiPiazza Mar 16 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 Mar 16 at 20:53
1

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
    }
} 
|improve this answer|||||
0

try this

var item = list.GetItemById(id);
                _context.Load(item);
            _context.ExecuteQuery();
|improve this answer|||||
  • No field values are loaded this way. – Shai Petel Aug 2 '19 at 16:30
-1

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.

|improve this answer|||||
  • 2
    It seems that a list item in this context does not have a Fields collection so it can't be loaded this way. – Colin Gardner Apr 15 '18 at 8:25

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.