Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Limit the result of CAML Query

Posted on 2010-08-31
5
Medium Priority
?
943 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 1000 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

782 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