Solved

Limit the result of CAML Query

Posted on 2010-08-31
5
901 Views
Last Modified: 2013-12-17
I have a list in sharepoint that contains five columns. Is it possible to limit the result of the query to 3 columns only?
Example:

I have Firstname, Lastname, Middlename, Age, Gender. When I execute the query it only returns Firstname, Lastname, Middlename.
Is that possible to CAML query?
0
Comment
Question by:PagodNaUtak
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
shahzadbux earned 250 total points
ID: 33574347
have a look at this guide: http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

quoted from the site:

"In cases you work with large lists it can be important to retrieve a subset of list items containing only the columns you need. In that case you will have to set the ViewFields property of the SPQuery object. You can do this by specifying all columns you want to see returned:

qry.ViewFields = "";
This will return the first name and the last name of the retrieved employees, but also the system fields like the ID and the Created date."
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 33574371
You should be able to filter the list of fields returned using the ViewFields property of the SPQuery object.

See here for details: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx
0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 33575229
qry.ViewFields = "<FieldRef Name='FirstName'  DisplayName='FIRSTNAME'/>
Is it possible to set the displayname of the column? I tried the above code but to no avail? Any ideas?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33575244
What are you trying to do with the output, display it directly or access it programmatically?
0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 33575302
I am binding it to gridview... see the code below...
  SPQuery query = new SPQuery();
                   query.Query = sb.ToString();
                   query.ViewFields = @"<FieldRef Name='ID'/><FieldRef Name='Title' />
                    <FieldRef Name='Position' /><FieldRef Name='Status' />
                    <FieldRef Name='DateApplied'/><FieldRef Name='InitialInterview'/>
                    <FieldRef Name='AptitudeExam'/><FieldRef Name='AptitudeScore'/>
                    <FieldRef Name='Essay'/><FieldRef Name='TechExamDate'/>
                    <FieldRef Name='Score'/><FieldRef Name='TechInterview'/>
                    <FieldRef Name='FinalInterview'/><FieldRef Name='JobOffer'/>
                    <FieldRef Name='CreatedDate' />";
                   
                   gdviewApplicants.DataSource = null;
                   gdviewApplicants.DataSource = listApplicants.GetItems(query).GetDataTable();

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question