Solved

Limit the result of CAML Query

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

18 Experts available now in Live!

Get 1:1 Help Now