Home > Code > Field in List not available in ListItem using Managed Client Object Model

Field in List not available in ListItem using Managed Client Object Model

If you have a field called “Folder” it will not be available in the returned ListItem object

e.g. listItem[“Folder”].ToString()

The inner workings of the ListItem object uses an ExpandoObject to store the properties. It seems it mixes this up in the FieldValues Collection with all your custom fields. The Folder property then takes the value from the FieldValues collection to make it available to the Folder property. Thus making your own Folder field “disappear”

Here is how you can replicate it.

  • Create a list based on the Custom List template
  • Add a column called “Folder” and make it a text field
  • Add a column called “DisplayName” and make it a text field
  • Add a column called “MyField” and make it a text field

Add a couple of dummy rows of data

Create a console application in VS that references Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime

Add the following code to the Main method

using (var context = new ClientContext("http://siteurl"))
{
  var query = CamlQuery.CreateAllItemsQuery();
  var listItems = context.Web.Lists.GetByTitle("Testing").GetItems(query);

  context.Load(listItems);
  context.ExecuteQuery();

  foreach (var listItem in listItems)
  {
    Console.WriteLine("Title: {0}", listItem["Title"]);
    Console.WriteLine("Folder: {0}", listItem["Folder"]);
    Console.WriteLine("DisplayName: {0}", listItem["DisplayName"]);
    Console.WriteLine("MyField: {0}", listItem["MyField"]);
  }
}

Here is the code from .Net Reflector that shows the ListItem object populating the properties. It’s also worth noting that the other properties detailed in this method will have the same problem, but it’s unlikely that you’ll call a field FileSystemObject!

protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
    {
        bool flag = base.InitOnePropertyFromJson(peekedName, reader);
        if (!flag)
        {
            switch (peekedName)
            {
                case "AttachmentFiles":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("AttachmentFiles", this.AttachmentFiles, reader);
                    this.AttachmentFiles.FromJson(reader);
                    return flag;

                case "ContentType":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("ContentType", this.ContentType, reader);
                    this.ContentType.FromJson(reader);
                    return flag;

                case "DisplayName":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["DisplayName"] = reader.ReadString();
                    return flag;

                case "EffectiveBasePermissions":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["EffectiveBasePermissions"] = reader.Read<BasePermissions>();
                    return flag;

                case "EffectiveBasePermissionsForUI":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["EffectiveBasePermissionsForUI"] = reader.Read<BasePermissions>();
                    return flag;

                case "FieldValuesAsHtml":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesAsHtml", this.FieldValuesAsHtml, reader);
                    this.FieldValuesAsHtml.FromJson(reader);
                    return flag;

                case "FieldValuesAsText":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesAsText", this.FieldValuesAsText, reader);
                    this.FieldValuesAsText.FromJson(reader);
                    return flag;

                case "FieldValuesForEdit":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("FieldValuesForEdit", this.FieldValuesForEdit, reader);
                    this.FieldValuesForEdit.FromJson(reader);
                    return flag;

                case "File":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("File", this.File, reader);
                    this.File.FromJson(reader);
                    return flag;

                case "FileSystemObjectType":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["FileSystemObjectType"] = reader.ReadEnum<FileSystemObjectType>();
                    return flag;

                case "Folder":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("Folder", this.Folder, reader);
                    this.Folder.FromJson(reader);
                    return flag;

                case "Id":
                    flag = true;
                    reader.ReadName();
                    base.ObjectData.Properties["Id"] = reader.ReadInt32();
                    return flag;

                case "ParentList":
                    flag = true;
                    reader.ReadName();
                    base.UpdateClientObjectPropertyType("ParentList", this.ParentList, reader);
                    this.ParentList.FromJson(reader);
                    return flag;
            }
        }
        return flag;
    }

Here’s a link in TechNet Forums to the initial discussion and a link to the reported bug

Advertisements
Categories: Code Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: