Solved

Limit the result of CAML Query

Posted on 2010-08-31
5
877 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now