Search code examples
sharepointguidsplistitem

Item count should be 10 but is 0


the items.count should be atlease 10. I have 10 subfolders (Release 1 ..... Release 10) with in this documnent library "Auto Cad" and each subfolder has a file called license.txt. hmmm Why this is not returning any file(s)?

private void btnGetFileGuid_Click(object sender, EventArgs e)
{ 

using (SPSite site = new SPSite("https://www.abc.com/sites/Software"))
 { 
 using (SPWeb web = site.OpenWeb())
 { 
  SPList spList = web.Lists["Auto Cad"];
  string fileName = "license.txt"; 
  SPQuery query = new SPQuery(); 
  query.Query="<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>" + fileName + "</Value></Eq></Where>"; 
  SPListItemCollection items = spList.GetItems(query); 
  if (items.Count > 0) 
   { 
    Guid id = items[0].UniqueId; 
    lblGuid.Text = id.ToString(); 
   } 
  }
 } 
}  

Solution

  • SPQuery only searches a particular folder - to recursively search through subfolders you need to set

    SPQuery.ViewAttributes = "Scope=\"Recursive\"";

    So your code should be

    SPQuery query = new SPQuery(); 
    query.ViewAttributes = "Scope=\"Recursive\"";
    query.Query=".... REST OF YOUR CODE HERE "