I know folder name I want the ID of that folder which is created in Sharepoint Document Library. How to write CAML Query to search document library to get folder and its ID Plz help on this..
Rushikesh
SPQuery q = new SPQuery();
q.MeetingInstanceId = -1; //in case your document library is in meeting workspace, query items from all meetings
q.ViewAttributes = "Scope='RecursiveAll'"; //Query all folders (if this is not set, only current folder will be queryied)
q.Query =
<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentTypeId' />
<Value Type='Text'>0x0120</Value>
</BeginsWith>
<Eq>
<FieldRef Name='Title' />
<Value Type='Text'>Folder name</Value>
</Eq>
</And>
</Where>
Then you execute query and get the id:
SPListItemCollection items = list.GetItems(q);
if (items.Count > 0)
int folderId = items[0].ID
You can also enumerate list folders by using SPList.Folders property
I know this is an old thread but I tried the above example in a SP 2010 environment and it didn't work so I thought I'd add my working CAML. Instead of using the ContentTypeID field, I just used ContentType=Folder
<Where>
<And>
<Eq>
<FieldRef Name='ContentType'/>
<Value Type='Text'>Folder</Value>
</Eq>
<Eq>
<FieldRef Name="Title"/>
<Value Type='Text'>Folder Name</Value>
</Eq>
</And>
</Where>