0

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();
reapeat.DataBind();
1
  • Why you used ContentType in fieldref? is this column name?
    – Mihir
    Commented May 27, 2013 at 8:11

3 Answers 3

1

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);
}
0

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.

1
  • Please give your feed back whether it is working or not?
    – Mihir
    Commented May 27, 2013 at 10:33
-2

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.

1
  • This is really bad practice since you shall not dispose the context web! It will severly impact performance Commented May 30, 2013 at 9:18

Your Answer

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