1

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?

  • If You re-create the field then your code fails right.....Are you getting any specific error or your list does just not return all the value ..? – Rohit Waghela Oct 20 '16 at 5:19
  • Yes. No error. The value is not returned. – IMTheNachoMan Oct 20 '16 at 9:57
1

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.

  • That does not work. My understanding is that CSOM/JSOM does not respect the ViewFields option from viewXML. – IMTheNachoMan Oct 20 '16 at 10:04
  • It does respect. Just test in my environment. Check my update. – Atish Dipongkor - MVP Oct 20 '16 at 10:33
0

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.

  • So you have to actually remove the fields from the column? You can't just remove them from the view? – IMTheNachoMan Oct 20 '16 at 10:05
  • If you will remove a column from the list, it will also get removed from the list view. – Vaibhav Taunk Oct 20 '16 at 15:28
  • I'm saying, is there anyway to get the code to not fail by just removing it from the view? So I keep all the fields but for ViewFields I just specify the ones I want. – IMTheNachoMan Oct 20 '16 at 21:03
  • Yes, you can keep the fields. If you removed it from viewfields, it will work. – Vaibhav Taunk Oct 21 '16 at 3:10

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.