Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
Sharepoint will allways add some more extra fields so you will need to only use the ones you need when rendering your grid –  Luis Aug 14 '13 at 13:37
add comment

1 Answer

up vote 0 down vote accepted

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.