1

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(); 
   } 
  }
 } 
}  
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
John Locke
  • 13
  • 4

3 Answers3

0
query.Query="" + fileName + "";

This line is wrong. This should be CAML query and not name of filename.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
0

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 "
Ryan
  • 23,871
  • 24
  • 86
  • 132
0

You need to make a recursive call use the solution provided in question link provided below

i would recommend use qry.ViewAttributes = "Scope='RecursiveAll'"; to get documents and folders as well query to get all items in a list including items in the sub folders in sharepoint

Community
  • 1
  • 1
  • ashutosh: looks like Ryan's type solution. I will sure to try this one as well. What's the main difference between these two? – John Locke Dec 15 '10 at 04:18
  • if you see the link provided with the answer and see their it answers the question you have asked – Ashutosh Singh-MVP SharePoint Dec 15 '10 at 06:44
  • How does an identical answer that came hours after get more up votes? Points jealousy much ;) – Ryan Dec 15 '10 at 12:52
  • Recursive returns all files from all subfolders - RecursiveAll returns both Files AND folders from all subfolders. Basically RecursiveAll does exactly the same in this context as he's looking for files, not folders. http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spviewscope.aspx – Ryan Dec 15 '10 at 15:45
  • 1
    @Ryan - See here for a discussion about this: http://meta.stackexchange.com/questions/72439/reason-for-loss-of-reputation – Brad Larson Dec 17 '10 at 04:26