I am confused with SPQuery. How can I use it to get listitem values. Let's say I have a list "Cinema" with 20 columns but i only want 4.

  • Film ID
  • Film Name
  • Film Type
  • Film Review

How can i get all list items using SPQuery and access them using SPListItemCollection.

Example. not working

spsite site = new site("portal.filmworld.com/abc")
spweb web = site.openweb();
   splist li = web.lists["Films"];
   spquery q = new spquery();
   q.query = string.Format("<FieldRef Name='FilmID' /><FieldRef Name='FilmName' /><FieldRef Name='FilmType' /><FieldRef Name='FilmReview' />");

   splistitemcollection items = li.getitems(q);

   foreach(SPListItem item in items)
          string name = item.filmName;     <----   it's not working gives exception of null


Try this:

   splist li = web.lists["Films"];
   spquery q = new spquery();
   q.Query=""; //Enter a caml query here if you would like to restrict your results...
   q.ViewFields = string.Concat(
                                   "<FieldRef Name='FilmID' />",
                                   "<FieldRef Name='FilmName' />",
                                   "<FieldRef Name='FilmType' />",
                                   "<FieldRef Name='FilmReview' />");
   q.ViewFieldsOnly = true; // Fetch only the data that we need

   splistitemcollection items = li.getitems(q);

   foreach(SPListItem item in items)
         string name = item["FilmName"].toString();


The query is for restricting the items that we want to return, not the data returned for each item.

| improve this answer | |

You do as you always do with ListItems. For example:

string name = item["filmName"].toString();

For not nullable types (like DateTime) you should do a null check:

DateTime aDate = item["myDateColumn"] != null ? DateTime.parse(item["myDateColumn"].toString()) : null;

(OK, I know there exists better ways but this is pretty generic)

So, basically not so hard :)

| improve this answer | |
  • did my query synatx ok ? as i am not sure if it is – Mathematics Aug 1 '13 at 12:07
  • i want all list items and only above columns when there are 20 columns in list – Mathematics Aug 1 '13 at 12:07
  • The you should use the ViewFields property and not the Query. Like q.ViewFields = "what you wrote as q.Query" – Robert Lindgren Aug 1 '13 at 12:08
  • plz give some explample i am new to caml query – Mathematics Aug 1 '13 at 12:09
  • I did, just change your q.Query to q.ViewFields – Robert Lindgren Aug 1 '13 at 12:10

The way you wrote q.query is not the usual way to write the query. A normal query would be where you add your conditions in CAML syntax so that you return a subset of the items based on your criteris; if you are looking to get back all the items from a list, then you don't need SPQuery. Here is an example of an SPQuery:

q.Query = "<Where><Eq><FieldRef Name='FilmID'/><Value>Some random film id</Value></Eq></Where>"

Here is a tutorial: SPQuery in SharePoint 2010

The other thing is you wouldn't access the field FilmName like you did above, UNLESS you used something like Linq to SharePoint/Entity Framework (which is a totally separate discussion!). Try using item["FilmName"] instead. Here is another tutorial: Sharepoint 2010 – How best to reference a Column (Field) from an SPListItem

Hope this helps!

| improve this answer | |
  • if you are looking to get back all the items from a list, then you don't need SPQuery ,,,then what do i need ? – Mathematics Aug 1 '13 at 12:26
  • It would be something like the following: SPList list = web.Lists["Some List"]; SPListItemCollection items = list.Items; msdn.microsoft.com/en-us/library/… – Mario Pereira Aug 1 '13 at 12:29
  • which following / – Mathematics Aug 1 '13 at 12:41
  • SPList li = web.Lists["Film"]; SPListItemCollection items= li.Items; MSDN: SPList.Items Property – Mario Pereira Aug 1 '13 at 12:49
  • 2
    I still think he is better of with the SPQuery since he does not want all fields. The data loaded is minimized with the query – Robert Lindgren Aug 1 '13 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.