Thursday, December 15, 2011

SharePoint CAML query to search folder

I am posting here SharePoint CAML query to search a folder by name. The thing to be notice here is that name field is neither "Title" nor "Name" as we might guess but "NameOrTitle".

            string caml = "<Where>" +
                                "<And>" +
                                    "<Eq>" +
                                        "<FieldRef Name='NameOrTitle'/>" +
                                        "<Value Type='Text'>" + FolderId + "</Value>" +
                                    "</Eq>"+
                                    "<Eq>" +
                                        "<FieldRef Name='ContentType' />" + 
                                        "<Value Type='Text'>Folder</Value>" +
                                    "</Eq>" +
                                "</And>" + 
                            "</Where>";
            SPQuery qry = new SPQuery();
            qry.Query = caml;
            SPListItemCollection items = PicLib.GetItems(qry);

Thanks,
Amit