Question

While debugging an issue I realized that SharePoint 2010 CSOM/JSOM does not return all fields if the list has too many fields. For example, the code below fails on a list. But if I delete one of the fields I created in the list then it works. If I re-create the field then the code fails again. In the example, I use the Editor field but I do not think it is necessarily related to that field.

ExecuteOrDelayUntilScriptLoaded(function()
{
    var context = new SP.ClientContext.get_current()
    var list = context.get_web().get_lists().getByTitle("test");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);

    context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
    {
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

        var items = list.getItems(query);

        context.load(items);

        context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
        {
            var listEnumerator = items.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var listItem = listEnumerator.get_current();

                var modifiedBy = listItem.get_item("Editor");
            }
        }), Function.createDelegate(this, function(){}));
    }), Function.createDelegate(this, function(){}));
}, "sp.js");

I can not find ANYTHING that references this online. Does anyone know why this happens or how to force getting all fields regardless of how many fields are in the list?

Was it helpful?

Solution

In CAML query, you can specify the ViewFields. These fields will be included in results.

var viewFields = <ViewFields>
                     <FieldRef Name="Title" />
                     <FieldRef Name="Country" />
                     <FieldRef Name="Population" />
                   </ViewFields>;
var query = new SP.CamlQuery();
query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query>" + viewFields + "</View>");

Update

OP said CSOM/JSOM does not respect ViewFields. I found it works.

ExecuteOrDelayUntilScriptLoaded(function()
{
    var context = new SP.ClientContext.get_current()
    var list = context.get_web().get_lists().getByTitle("Employee");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);

  var viewFields = '<ViewFields><FieldRef Name="Title" /></ViewFields>';

    context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
    {
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query> "+viewFields+"</View>");

        var items = list.getItems(query);

        context.load(items);

        context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
        {
            var listEnumerator = items.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var listItem = listEnumerator.get_current();

                var title = listItem.get_item("Title");
console.log(title);
            }
        }), Function.createDelegate(this, function(){}));
    }), Function.createDelegate(this, function(){}));
}, "sp.js");

I can access Title column as I have included Title only in ViewFields. If I access other columns it gives me error.

OTHER TIPS

In a list, if there are People & Group Type Column and Lookup Type Column, and the sum of these columns are more than 8, then the code will fail. SharePoint has a restriction of 8 lookup and People & Group Type Columns.

So when you remove the editor column, one People & Group Type Column is less, so it works.

For this issue you have to check these type of columns and if more than 8 then you have to use some logic to do this work.

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