SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using this code to get items from the folder but it return null value in the SPListItemCollection. What's the wrong in my code?

siteurl = SPContext.Current.Site.Url;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["PoAlbum"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Computed'>Folder</Value></Eq></Where>";
DataTable listItemsTable = list.GetItems(query).GetDataTable();
string name = (listItemsTable.Rows[0]["NameOrTitle"]).ToString();      
SPFolder folder = web.GetFolder("/PoAlbum/" + name);
query.Folder = folder;
SPListItemCollection itms = list.GetItems(query);
reapeat.DataSource = itms.GetDataTable();
share|improve this question

migrated from May 30 '13 at 9:00

This question came from our site for professional and enthusiast programmers.

Why you used ContentType in fieldref? is this column name? – Mihir May 27 '13 at 8:11

Use this as your query, finding the content type by name should be Value type Text:

 <Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Folder</Value></Eq></Where><OrderBy></OrderBy>

But my tip would be to first find the folder, without using Caml:

SPFolder myFolder = web.GetFolder("/LibraryName/FolderName");

if(myFolder != null){
    SPQuery query = new SPQuery();
    query.Query = "<OrderBy><FieldRef Name='Title'/></OrderBy>";
    query.Folder = myFolder;
    query.ViewAttributes = "Scope='RecursiveAll'";
    SPListItemCollection col = list.GetItems(query);
share|improve this answer

I think you are giving condition to retrieve but you are not setting what fields need to show. You should use ViewFields attribute for your query object. query.ViewFields="<FieldReft name=""ABC""/><FieldRef name=""123""/>" If you use this line, in the result these fields will come.

This is samplequery

            myquery.Query= @"<Where><IsNotNull><FieldRef Name=""Title"" /></IsNotNull></Where>";
            myquery.ViewFields= @"<FieldRef Name=""Title"" />";
            myquery.ViewAttributes = " Scope='RecursiveAll";
            SPList querylist = web.Lists["ListName"];
            SPListItemCollection curItems = queryList.GetItems(myquery);

Modify your query accordingly. Will works fine.

share|improve this answer
Please give your feed back whether it is working or not? – Mihir May 27 '13 at 10:33

You should wrap your SPWeb code in a using block

using(SPWeb web = SPContext.Current.Web) {

This will reduce any memory leaks.

That said, there's an easier way to get the items in your folder. The answer on this post should point you in the right direction.

share|improve this answer
This is really bad practice since you shall not dispose the context web! It will severly impact performance – Robert Lindgren May 30 '13 at 9:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.