3

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
   }

}

3 Answers 3

6

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.

2

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 :)

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

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!

5
  • if you are looking to get back all the items from a list, then you don't need SPQuery ,,,then what do i need ? Commented Aug 1, 2013 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/… Commented Aug 1, 2013 at 12:29
  • which following / Commented Aug 1, 2013 at 12:41
  • SPList li = web.Lists["Film"]; SPListItemCollection items= li.Items; MSDN: SPList.Items Property Commented Aug 1, 2013 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 Commented Aug 1, 2013 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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