none
Field in list not available in ListItem using Managed Client Object Model

    Question

  • I am trying to get some data from a list using the Client Object Model in a console app. It has a number of fields, some are text, some are DateTime, some are Lookups, etc, etc.

    It seems to have a problem with field that is called "Folder", all the other fields are fine, but if I try to access the "Folder" field I get the following error message.

    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.

    The code I used is below.

    using (var sourceContext = new ClientContext(SourceUrl))
    {
      var fields = sourceContext.Web.Lists.GetByTitle(SourceLibraryTitle).Fields;
                    
      sourceContext.Load(fields);
      sourceContext.ExecuteQuery();
    
      IList<string> viewFields = new List<string>();
    
      foreach (var field in fields)
      {
        viewFields.Add(field.InternalName);
      }
    
      var query = CamlQuery.CreateAllItemsQuery(5000, viewFields.ToArray());
    
      ListItemCollection sourceDocuments = sourceContext.Web.Lists.GetByTitle("Documents").GetItems(query);
    
      sourceContext.Load(sourceDocuments);
      sourceContext.ExecuteQuery();
                
      foreach (ListItem sourceDocument in sourceDocuments)
      {
        string folderValue = sourceDocument["Folder"].ToString();
        // Doesn't seem to like having folder as a field name
      }
    }
    

    I have also tried the following

    sourceContext.Load(sourceDocuments, items => items.IncludeWithDefaultProperties(i => i["Folder"]));

    This works, but again has no effect.

    Looking at the CAML query text I can see that the Folder field is included as a ViewField, so this means it is a Field in the Field Collection. This is confirmed during the debugging that this field exists.

    During debugging and can see that the "Folder" field is not shown in the FieldValues collection.

    Is "Folder" a reserved field name? Unfortunately this is a legacy list and I can't change the name (though the internal name will always be "Folder")

    Anyone got any ideas?

    Tuesday, January 22, 2013 10:03 AM

Answers

  • I have collated all the information in a blog post for future reference.

    http://statto1974.wordpress.com/2013/01/25/field-in-list-not-available-in-listitem-using-managed-client-object-model/

    • Proposed as answer by Dmitry Kaloshin Friday, January 25, 2013 5:43 PM
    • Marked as answer by Sjoukje Zaal Thursday, January 12, 2017 1:02 PM
    Friday, January 25, 2013 2:24 PM

All replies

  • Hi,

    Perhaps it has different name. Try this code in order to print all field names:

    public static void PrintListColumns(ClientContext clientContext, string listName)
    {
        List list = clientContext.Web.Lists.GetByTitle(listName);
        clientContext.Load(list.Fields);
        clientContext.ExecuteQuery();
        foreach (Field field in list.Fields)
        {
            Console.WriteLine("Field title:" + field.Title + " --- Field internal name:" + field.InternalName);
        }
    }



    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Tuesday, January 22, 2013 2:02 PM
  • Tried that, the internal and title are the same. Going to crank up .NET Reflector and do some further investigation.
    Tuesday, January 22, 2013 9:21 PM
  • Try this method and pass "Folder" in the third parameter columnName:

    public static void PrintListItemsWithSpecificColumn(ClientContext clientContext, string listName, string columnName)
    {
        List list = clientContext.Web.Lists.GetByTitle(listName);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View/>";
        ListItemCollection listItems = list.GetItems(camlQuery);
        clientContext.Load(listItems, items => items.Include(item => item[columnName], item => item["Title"]));
        clientContext.ExecuteQuery();
    
        foreach (ListItem li in listItems)
        {
            Console.WriteLine(li[columnName].ToString());
        }
    }


    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Tuesday, January 22, 2013 9:42 PM
  • Could you publish schema of your list.

    Probably I would try to check in my environment if I face similar problem.

    Regards,

    Hiren

    Wednesday, January 23, 2013 5:10 AM
  • That doesn't work either
    Wednesday, January 23, 2013 12:03 PM
  • This definetely something to do with a field that has the same name as a ListItem property

    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"]);
                    }
                }

    Run the code.

    It will error on the ExecuteQuery method with the following message

    The type of data at position 875 is different than the one expected.

    Make Folder and DisplayName values blank for each of the rows

    Run the code.

    It will error on the listItem["Folder"] object with the following message

    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.

    Remove the Folder column and DisplayName column and it works.

    Wednesday, January 23, 2013 12:14 PM
  • Great tonight I will check the problem and search for any viable solution.

    Regards,

    Hiren

    Thursday, January 24, 2013 8:37 AM
  • Hi,

    It seems to be a known bug Bug report - Client Object Model: Lists with a column named 'Folder'


    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Thursday, January 24, 2013 9:07 PM
  • Thanks for that.

    I came to the same conclusion after having a look using .Net Reflector. Something to do with the JSON serialisation.

    Microsoft.SharePoint.Client.ListItem

    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;
        }
    

    Friday, January 25, 2013 1:59 PM
  • I have collated all the information in a blog post for future reference.

    http://statto1974.wordpress.com/2013/01/25/field-in-list-not-available-in-listitem-using-managed-client-object-model/

    • Proposed as answer by Dmitry Kaloshin Friday, January 25, 2013 5:43 PM
    • Marked as answer by Sjoukje Zaal Thursday, January 12, 2017 1:02 PM
    Friday, January 25, 2013 2:24 PM