Question

I am using CAML to query a SharePoint list through the web service and outputting to a GridView in C#/ASP.Net. The problem I am having is that more columns are being shown than what I have specified in the 'ViewFields' part of the query (eg. shown below).

viewFields.InnerXml = "<ViewFields>" +
                      "<FieldRef Name="Col1" />" +
                      "<FieldRef Name="Col2" />" +
                      "<FieldRef Name="Col3" />" +
                      "<FieldRef Name="Col4" />" +
                      "</ViewFields>";

I have tried setting to false in the 'QueryOptions'.

Can anyone offer any solution to this problem, so that only the specified columns are returned?

Was it helpful?

Solution

This is to help people who are looking for an answer to this question, the solution we found most effective:

We took the node idea from here: How to Return Only Certain Columns of a List When using the SharePoint Web Service?

Then we went through the process of extract the fields that we want into a datatable. Firstly we create an appropriate amount of columns to fields. Then going through the child nodes to extract all the row data. Doing this for each of the fields that we have requested.

  var myData = new DataTable();

        foreach (string field in fields)
        {
            myData.Columns.Add(field);
        }

        foreach (XmlNode node in nodeListItems)
        {
            // rs = RowSet
            if (node.Name == "rs:data")
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    if (node.ChildNodes[i].Name == "z:row")
                    {
                        DataRow row = myData.NewRow();
                        foreach (string field in fields)
                        {
                            var xmlAttributeCollection = node.ChildNodes[i].Attributes;
                            if (xmlAttributeCollection != null)
                                row[field] = xmlAttributeCollection["ows_" + field].Value;
                        }
                        myData.Rows.Add(row);
                    }
                }
            }
        }
        return myData;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top