How to Return Only Certain Columns of a List When using the SharePoint Web Service?

The Light picture The Light · Mar 27, 2012 · Viewed 7.8k times · Source

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,

Answer

Rich Bennema picture Rich Bennema · Mar 27, 2012

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.