Partilhar via


Code snippet : Rollup tasks list from sub-sites to the parent site

string strSite = "https://terminator:2004/sites/t2";
            SPSite oSite = new SPSite(strSite);
            DataTable data = new DataTable();
            bool flag = true;
            foreach (SPWeb oWeb in oSite.AllWebs )
            {
                SPListCollection lists = oWeb.Lists;
                foreach (SPList list in lists)
                {
                    if(String.Compare(list.Title.ToString(), "Tasks") == 0)
                    {
                        if(list.Items.Count > 0)
                        {
                            if(flag)
                            {
                                    data = list.Items.GetDataTable().Clone();
                                    flag = false;
                            }
                            foreach(DataRow dr in list.Items.GetDataTable().Rows)
                            {
                                DataRow newRow = data.NewRow();
                                newRow.ItemArray = dr.ItemArray;
                                data.Rows.Add(newRow);
                            }
                        }
                    }
                }
            }

            if(data.Rows.Count == 0)
                MessageBox.Show("No Data:(");
            else
            {
                dataGrid1.DataSource=data;
            }