Monday, January 4, 2010

How to get a file from N th - level of subfolder in a Document Library?

Objective :

             To reterive the Sharepoint files in a document from the n-th Level of Sub folder.

 Solution :

           We can achieve through "SPQuery.ViewAttributes" Property.

Steps :

          SPQuery.ViewAttributes is String property, this contains the view attributes.

         What are all View element in the View attributes :  Please Ref  -> View Elements.

         For this scenario we can use only one View attribute  (ViewAttributes = "Scope="Recursive" )

         If the Scope attribute is set to Recursive (Scope="Recursive"), the query displays all the files within a document library, including ones in subfolders. If it is set to anything else, the query displays only files in the top folder.


Code Snippet :

SPView oView = oList.Views["View_Name"];
SPQuery oQuery = new SPQuery(oView);
oQuery.ViewAttributes = "Scope=\"Recursive\"";
SPListItemCollection oSelectedItemColection = oList.GetItems(oQuery);

foreach (SPListItem oItem in oSelectedItemColection)
{
        if (oItem.File != null)
       {
              String sFileName = oItem.Name;
       }
}


Thank You

No comments: