We have a document library with views created based on a column. This views have documents spread out to different folders. How can i retrieve all the documents in a particular view using CAML query or any other approach programmatically?

2 Answers 2


Try Following code

          SPList List = _site.Lists.TryGetList("ListName");
          SPView view = List.Views["Name"];
          SPQuery query = new SPQuery();
          query.Query = view.Query;
          query.ViewAttributes = "Scope=\"Recursive\"";                        
          SPListItemCollection items = list.GetItems(query);                 
        using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    using (SPWeb web = site.OpenWeb())
                        web.AllowUnsafeUpdates = true;
                        SPList list = web.Lists.TryGetList("Candidates");
                        SPQuery oquery = new SPQuery();

                        oquery.Query = " <Where><IsNotNull><FieldRef Name='CandidateName' /></IsNotNull></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
                        SPListItemCollection coll = list.GetItems(oquery);
                        DataTable dt = coll.GetDataTable();
                        if (dt.Rows.Count > 0)
                            oGridView.DataSource = dt;
                        web.AllowUnsafeUpdates = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.