Question

I was querying a SharePoint library, trying to retrieve some file attributes. Apparently some things like FileType and Date are not supported, even though those fields are listed as a built in field. How do I know what is supported and what isn't? It's a runtime error that I'd like to avoid.

    private static void ListDocuments(ClientContext clientContext)
    {
        List list = clientContext.Web.Lists.GetByTitle("My Documents");

        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml =
            @"<View>
            <Query/>
            <RowLimit>100</RowLimit>
          </View>";
        ListItemCollection listItems = list.GetItems(camlQuery);
        clientContext.Load(
             listItems,
             items => items
                 .Include(
                   // how do I know what is legal here?
                   item => item["LinkFilename"],
                     item => item["FileRef"],
                     item => item["File_x0020_Size"],
                     item => item["File_x0020_Type"],
                     item => item["Modified"]
                     ));
     }
Was it helpful?

Solution 2

The following code will query the server for the available fields and print them to the console window

        ClientContext clientContext = new ClientContext(URL);
        List list = clientContext.Web.Lists.GetByTitle(Documents);
        FieldCollection fields = list.Fields;  
        clientContext.Load(fields);

        clientContext.ExecuteQuery();

        foreach (Field field in fields)
        {
            System.Diagnostics.Trace.WriteLine("");
            System.Diagnostics.Trace.WriteLine("Title\t\t\t" + field.Title);
            System.Diagnostics.Trace.WriteLine("InternalName\t" + field.InternalName);
            System.Diagnostics.Trace.WriteLine("Scope\t\t\t" + field.Scope);
            System.Diagnostics.Trace.WriteLine("StaticName\t\t" + field.StaticName);
        }

OTHER TIPS

Double check that you are using the internal name for the field you want to query. This has gotten me more than once. Here is a good article that goes over a few ways to check this:

In addition to the owesome article, which Abe Miessler has posted:

In fact, all the OOTB SharePoint fields have their Guids in SPBuiltInFieldId class, but their Titles are not always refer to their property names in this class. So, how to determine, which SPBuiltInFieldId member corresponds your concrete field?

Here is an article, which provides full list of Guids, field titles and corresponding SPBuiltInFieldId member names:

http://aarebrot.net/blog/2008/07/frodes-awesome-list-of-sharepoint-column-field-ids-for-sharepoint-2007/

(you can even download xls/csv version of this list)

I have used the U2U CAML Query Builder in the past which gives you a list of fields and helps you build the query and test the results. In 2010 I have mainly used linq to SharePoint for querying data, however I have used the following method as well and it worked great

http://ranaictiu-technicalblog.blogspot.com/2011/01/u2u-caml-query-builder-for-sharepoint.html

Well I cannot be sure which field objects get initiated when we execute any query or object, its really frustrating but I am used to now. Here's my one of my queries that will initiate all item fields, however to retrieve you would need to know the internal name of the field you want to extract.

items = context.LoadQuery(collListItem.Include
                                                (
                                                item => item.File.ListItemAllFields,
                                                item => item.FileSystemObjectType,
                                                item => item.Id,                     
                                                item => item.File,
                                                item => item.File.ServerRelativeUrl,
                                                item => item.DisplayName
                                                 ).Where(item => item.Id > id && item.FileSystemObjectType == FileSystemObjectType.File)); 

hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top