3

SharePoint Web Service has a method with the below signature:

 public System.Xml.XmlNode GetListItems(string listName, string viewName, System.Xml.XmlNode query, System.Xml.XmlNode viewFields, string rowLimit, System.Xml.XmlNode queryOptions, string webID) {
            object[] results = this.Invoke("GetListItems", new object[] {
                        listName,
                        viewName,
                        query,
                        viewFields,
                        rowLimit,
                        queryOptions,
                        webID});
            return ((System.Xml.XmlNode)(results[0]));
        }

I'd like to return only the "ID" and "Title" columns of my list, e.g. "ProductNames" rather than returning all the columns.

How to use this webmethod to achieve to only return certain columns?

I'm using the below method which returns all the columns:

public XmlNode GetListItems(string listName, XmlElement camlQuery)
        {
            sp.Lists listService = this.getSPListService();

            XmlDocument xmlDoc = new XmlDocument();

            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

            XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

            XmlElement foldersEl = xmlDoc.CreateElement("Folder");

            if (camlQuery == null) // override default view filters
            {
                camlQuery = xmlDoc.CreateElement("Query");

                camlQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";
            }

            queryOptions.AppendChild(foldersEl);

            return listService.GetListItems(listName, null, camlQuery, ndViewFields, int.MaxValue.ToString(), queryOptions, null);
        }

so, now I'd like to write a new method like:

public XmlNode GetListItems(string listName, XmlElement camlQueryForFilteringRows, bool includeAllColumns, string[] columnNamesToInclude)         
{

}

and call it with

var listItems = GetListItems("ProductNames", null, new [] {"ID", "Title"});

I tried adding the below structure to the ViewFields variable but still it returns all the columns:

<ViewFields>
   <FieldRef Name="ID" />
   <FieldRef Name="Title" />
</ViewFields>

Thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258
  • I believe it's a bug. It does however bring in the fields you specify in ViewFields first. So you could potentially do a workaround that only returns the first X columns, where X is the number of ViewFields specified. – Riegardt Steyn Nov 13 '13 at 16:01

2 Answers2

5

Try something like this:

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
if (!includeAllColumns)
{
    ndViewFields.InnerXml =
        string.Join(string.Empty,
            columnNamesToInclude
            .Select(t1 => string.Format("<FieldRef Name=\"{0}\" />", t1))
            .ToArray());
    queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
}

The combination of ViewFields and IncludeMandatoryColumns has worked for me in the past.

Rich Bennema
  • 10,295
  • 4
  • 37
  • 58
  • Maybe this was true a year ago, but it most certainly does NOT work today. Setting IncludeMandatoryColumns to FALSE does NOT remove the system fields, neither does setting ViewFieldsOnly to TRUE. There are numerous forums on the web that report this as a bug. – Riegardt Steyn Nov 13 '13 at 15:54
  • I've noticed it brings in the fields you specify in ViewFields first. So you could potentially do a workaround that only returns the first X columns, where X is the number of ViewFields specified. – Riegardt Steyn Nov 13 '13 at 16:00
1

Try setting queryoption ViewFieldsOnly to True.