How to return only specific columns of a list when using a SharePoint web service?

The SharePoint web service has a signed method:

 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 would like to return only the "ID" and "Title" columns in my list, for example. "ProductNames" instead of returning all columns.

How can I use this web method to achieve only returning specific columns?

I am using below method which returns all 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 would like to write a new method like:

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

}

      

and call it

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

      

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

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

      

Thank,

+3


source to share


2 answers


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 has IncludeMandatoryColumns

worked for me in the past.

+5


source


Try setting queryoption ViewFieldsOnly to True.



+1


source







All Articles