Tuesday, July 20, 2010

Getting all documents in document library inside all folders !

Hi All,

Many times we require getting all documents in the entire document library including sub folders. For this we need to use the ViewAttributes property on SPQuery object and set it to specify Scope=\"Recursive\

If we don’t do this, then result we will get it all documents from the top folder only.

Here is a simple way, we will achieve this.

SPList objList = objWeb.Lists["{name of the doc lib}"];

SPView objView = objList.Views["name of the view"];

SPQuery objQuery = new SPQuery(objView);

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

SPListItemCollection objItemColl = objList.GetItems(objQuery);

Sometimes we may require returning the result from specific folder inside the document library.

At this time, we need to use SPFolder class and set the SPQuery object’s Folder property to the object of SPFolder.

Here is a way.

SPFolder objFolder = objList.RootFolder.SubFolders["name of the folder"];
objQuery.Folder = objFolder
That is it. Your job is done.

No comments:

Post a Comment