Tutorial :Why does criteria get ignored by the SharePoint List Service's GetListItems Method


Although I specify a ViewFields element in my sharepoint list service's GetListItems query, all fields are returned. The following code builds the request:

XmlDocument xmlDoc = new System.Xml.XmlDocument();  XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");  XmlNode viewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");  XmlNode queryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");        ... set query ...    viewFields.InnerXml = "<FieldRef Name='LinkFilename' /><FieldRef Name='FileDirRef' /><FieldRef Name='FileLeafRef' />";                queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc><Folder>Resource Management Tools</Folder><ViewAttributes Scope='Recursive' />";    XmlNode xmlNode = SharePointListWebService.GetListItems(  _listServiceConfigurationSettings.ListName,                                  string.Empty,                                  query,                                  viewFields,                                  null,                                  queryOptions,                                  null);  

According to fiddler, this results in the following soap envelope being posted to the list service:

<?xml version="1.0" encoding="utf-8"?>  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">    <soap:Body>      <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">            <listName>Shared Documents</listName>        <viewName />        <query>          <Query xmlns="">            <Where>              <And>                <Contains>                  <FieldRef Name="FileLeafRef" />                  <Value Type="Text">.xls</Value>                </Contains>                <Geq>                  <FieldRef Name="Modified"                  IncludeTimeValue="True" />                  <Value Type="DateTime">2010-05-10T11:53:32Z</Value>                </Geq>              </And>            </Where>            <OrderBy>              <FieldRef Name="FileDirRef" />            </OrderBy>          </Query>        </query>        <viewFields>          <ViewFields xmlns="">            <FieldRef Name="LinkFilename" />            <FieldRef Name="FileDirRef" />            <FieldRef Name="FileLeafRef" />          </ViewFields>        </viewFields>        <queryOptions>          <QueryOptions xmlns="">            <IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>            <DateInUtc>TRUE</DateInUtc>            <Folder>Resource Management Tools</Folder>            <ViewAttributes Scope="Recursive" />          </QueryOptions>        </queryOptions>      </GetListItems>    </soap:Body>  </soap:Envelope>  

and the following soap response being returned from the service:

<?xml version="1.0" encoding="utf-8"?>  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">    <soap:Body>      <GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">        <GetListItemsResult>          <listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'          xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'          xmlns:rs='urn:schemas-microsoft-com:rowset'          xmlns:z='#RowsetSchema'>            <rs:data ItemCount="19">              <z:row ows_LinkFilename='[SENSITIVE DATA].xls'              ows_FileDirRef='278;#sites[SENSITIVE DATA]'              ows_FileLeafRef='278;#[SENSITIVE DATA].xls'              ows_MetaInfo='278;#Subject:SW| vti_parserversion:SR| ContentTypeId:SW|0x0101006C2E647253A1074FB6079F08E5F2A395 _Author:SW|[SENSITIVE DATA] _Category:SW| vti_author:SR|[SENSITIVE DATA] _Comments:SW| vti_approvallevel:SR| vti_categories:VW| vti_cachedcustomprops:VX|vti_approvallevel vti_categories Subject vti_assignedto Keywords _Author _Category _Comments vti_assignedto:SR| Keywords:SW| vti_modifiedby:SR|[SENSITIVE DATA]'              ows__ModerationStatus='0' ows__Level='1'              ows_Last_x0020_Modified='278;#2010-06-29T18:55:38Z'              ows_ID='278' ows_owshiddenversion='53'              ows_UniqueId='278;#{0E51B2B1-89A7-4895-8ECC-0FE7D420470C}'              ows_FSObjType='278;#0'              ows_Created_x0020_Date='278;#2009-03-09T16:06:41Z'              ows_ProgId='278;#' ows_Modified='2010-06-29T18:55:37Z'              ows_FileRef='278;#sites[SENSITIVE DATA].xls'              ows_DocIcon='xls'              ows_Editor='262;#[SENSITIVE DATA]' />                        </rs:data>          </listitems>        </GetListItemsResult>      </GetListItemsResponse>    </soap:Body>  </soap:Envelope>  

Notice the z:row element has more fields included than I specified in my ViewFields criteria. I also set IncludeMandatoryColumns to false in my query options. Did I do something wrong or do I not understand how ViewFields really works as I thought it would limit the fields i.e. z:row attributes returned by the SharePoint list service.


I've been struggling with this issue, too. Tonight I found a solution that seems to work for me...hopefully it's helpful for others, as well.

The structure of your viewFields node is fine...

   <viewFields>      <ViewFields xmlns="">        <FieldRef Name="LinkFilename" />        <FieldRef Name="FileDirRef" />        <FieldRef Name="FileLeafRef" />      </ViewFields>    </viewFields>  

The problem is likely your field names. Apparently you have to use the internal names rather than the display names. One quick & easy way to determine the internal names is to go to the List Settings page for your list and hover over each column in the list. The browser's status bar will show a URL similar to this..


Use the internal field name shown there for each of the fields you want to return from the GetListItems() service. For example,

<FieldRef Name="LinkFilename" />  

....might need to be written as...

<FieldRef Name="Link_x0020_Filename" />  

At least that's what it took to solve the problem for me. Hope that helps!


There is a ViewFieldsOnly property for the purpose (I've not experimented with it though).

When you specify fields by setting the ViewFields property, the query retrieves data for more than just those fields. To optimize performance, you can limit the data that is returned by the query by setting the ViewFieldsOnly property to true.


Without doing any of my own research, tt looks like you're generating the following XML node:

  <viewFields>      <ViewFields xmlns="">        <FieldRef Name="LinkFilename" />        <FieldRef Name="FileDirRef" />        <FieldRef Name="FileLeafRef" />      </ViewFields>    </viewFields>  

Do you mean to have your ViewFields element embedded in another viewFields element?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »