/// <summary> /// Creates and initializes an SPSiteDataQuery object for a given scope. /// </summary> public SPSiteDataQuery CreateSiteDataQuery(CAML.QueryScope scope) { var query = new SPSiteDataQuery { Query = QueryXml, ViewFields = ViewFieldsXml, Lists = ListsXml }; // Only use the Webs clause if the scope is different than the default. if (scope != CAML.QueryScope.WebOnly) { query.Webs = CAML.Webs(scope); } return(query); }
/// <summary> /// Executes the query against a website for a given scope and then binds to a specified object class. /// </summary> public IList <T> Fetch <T>(SPWeb web, CAML.QueryScope scope) where T : new() { IList <T> result = new List <T>(); const BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; foreach (var item in Fetch(web, scope)) { var t = new T(); foreach (var info in t.GetType().GetFields(flags)) { foreach (var field in (ICamlField[])info.GetCustomAttributes(typeof(CamlField), false)) { field.SetValue(t, item, info); } } result.Add(t); } return(result); }
/// <summary> /// Retrieves a collection of list items from a SharePoint web using the attached query. /// </summary> /// <remarks>The query considers</remarks> /// <param name="web">the web from which to fetch the items</param> /// <param name="scope">specifies the desired query scope</param> /// <returns>a list containing the resulting items</returns> public IList <SPListItem> Fetch(SPWeb web, CAML.QueryScope scope) { var items = new List <SPListItem>(); try { // Create the query. var query = CreateSiteDataQuery(scope); // Execute the query. DataTable table = web.GetSiteData(query); // Convert the resulting table into a generic list // of SPListItem instances. foreach (DataRow row in table.Rows) { var listGuid = new Guid(row["ListId"].ToString()); var itemIndex = int.Parse(row["ID"].ToString()) - 1; try { var itemList = web.Lists[listGuid]; items.Add(itemList.Items[itemIndex]); } catch (Exception x) { Debug.WriteLine(x.Message); } } } catch (Exception x) { Debug.WriteLine(x.Message); } return(items); }
/// <summary> /// Executes the query and binds the results to a gridview. /// </summary> public void DataBind(SPGridView gridView, SPWeb web, CAML.QueryScope scope) { DataBind(gridView, web, scope, true); }
/// <summary> /// Executes the query and binds the results to a gridview. /// </summary> /// <remarks> /// Optionally initializes the gridview columns using the fields associated /// with this query. /// </remarks> /// <param name="gridView">the gridView to bind to</param> /// <param name="web">the web containing the data</param> /// <param name="scope">the desired query scope</param> /// <param name="initColumns">whether to initialize the gridview columns</param> public void DataBind(SPGridView gridView, SPWeb web, CAML.QueryScope scope, bool initColumns) { BindToGridView(web, gridView, CreateSiteDataQuery(scope), initColumns); }