How can I make a list with all Tasks? It is possible to use the MyTasks webpart, this is after changing a list of all task within the sitecollection.
But i want a list of all tasks of multiple sitecollections,a global my task list.
try this:
SPSiteDataQuery q = new SPSiteDataQuery();
q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.Lists = "<Lists BaseType='0' />";
q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";
DataTable results = new DataTable();
using (SPSite site = new SPSite("http://sharepoint"))
{
using (SPWeb web = site.OpenWeb("subsite"))
{
results = web.GetSiteData(q);
SPList newList = null;
Guid newListGuid = web.Lists.Add("AllTaskList", "Show all task for user from all sites", SPListTemplateType.GenericList);
newList = web.Lists[newListGuid];
foreach (DataColumn dc in results.Columns)
{
newList.Fields.Add(dc.ColumnName, SPFieldType.Text, false);
}
newList.Update();
SPListItem newItem = null;
foreach (DataRow row in dt.Rows)
{
newItem = newList.AddItem();
foreach (DataColumn dc in dt.Columns)
{
newItem[dc.ColumnName] = row[dc].ToString();
}
newItem.Update();
}
}
}
the code above would need some tweaking to get what you want but the bare bones are there to get you more than going! I dont think I need to explain it as its self explanitory!... query to get all tasks from site collection, add it to a datatable, from the datatable create a list, get all items from the datatable and populate the new list.... if you want to populate a current list its even easier but ill leave that up to you ;) .
if you want to get all task list from a webapplication that contains all the site collections you can use:
using (SPSite stsite = new SPSite(SPContext.Current.Web.Url))
{
SPWebApplication spWebApp = stsite.WebApplication;
foreach (SPSiteCollection spColl in spWebApp.Sites)
{
foreach (SPSite spSit in spColl)
{
using (SPWeb siteWeb = spSit.OpenWeb())
{
SPList listSiteSetup1 = siteWeb.Lists["Tasks"];
/*
Some code goes here
*/
}
}
}
}
//Gets the collection of all "Task" lists that are contained in the WebSite.
SPList listSiteSetup = SPContext.Current.Web.Lists["Tasks"];
//Get the datatable version of tasks.
SPListItemCollection items = listSiteSetup.Items;
DataTable dtTasks = items.GetDataTable();
DataColumn dtLink = new DataColumn("link");
//for (int i = 0; i <= dtTasks.Columns.Count; i++)
//{
// string name = dtTasks.Columns[i].ColumnName;
//}
dtTasks.Columns.Add(dtLink);//Columns to Display
string[] link = new string[1];
link[0] = "link";
BoundField bfPriority = new BoundField();
bfPriority.HeaderText = "Priority";
bfPriority.DataField = "Priority";
HyperLinkField hlfTitle = new HyperLinkField();
hlfTitle.DataTextField = "Title";
hlfTitle.HeaderText = "Title";
hlfTitle.DataNavigateUrlFields = link;
BoundField bfStatus = new BoundField();
bfStatus.HeaderText = "Status";
bfStatus.DataField = "Status";
BoundField bfStartDate = new BoundField();
bfStartDate.DataField = "StartDate";
bfStartDate.HeaderText = "Start Date";
BoundField bfDueDate = new BoundField();
bfDueDate.HeaderText = "Due Date";
bfDueDate.DataField = "DueDate";
BoundField bfAssignedTo = new BoundField();
bfAssignedTo.DataField = "AssignedTo";
bfAssignedTo.HeaderText = "Assigned To";
//working but not required
//BoundField bfPercentComplete = new BoundField();
//bfPercentComplete.DataField = "PercentComplete";
//bfPercentComplete.HeaderText = "PercentComplete";
gvTasks.Columns.Add(hlfTitle);
gvTasks.Columns.Add(bfStatus);
gvTasks.Columns.Add(bfStartDate);
gvTasks.Columns.Add(bfDueDate);
gvTasks.Columns.Add(bfAssignedTo);
gvTasks.Columns.Add(bfPriority);
//gvTasks.Columns.Add(bfPercentComplete);
gvTasks.AutoGenerateColumns = false;
//this loop attaches the link to the hyperlinkField to the Title column of every Task
foreach (DataRow dr in dtTasks.Rows)
{
dr["link"] = SPContext.Current.Web.Url + "/Lists/Tasks/DispForm.aspx?ID=" + Convert.ToString(dr["ID"]) + "&Source=" + SPContext.Current.Web.Url;
}
SPUser user = SPContext.Current.Web.CurrentUser;
string rowFilter = "AssignedTo = '" + user.Name + "'";
SPGroupCollection userGroupsColle = user.Groups;
foreach (SPGroup grp in userGroupsColle)
{
rowFilter = rowFilter + " OR AssignedTo = '" + grp.Name + "'";
}
string statusFilter = null;
DataView dv = new DataView(dtTasks, rowFilter, null, DataViewRowState.CurrentRows);
switch (Status)
{
case EnumStatus.NotCompleted:
{
statusFilter = "Status='In Progress' OR Status='Not Started'";
break;
}
case EnumStatus.Completed:
statusFilter = "Status='Completed'";
break;
case EnumStatus.InProgress:
statusFilter = "Status='In Progress'";
break;
case EnumStatus.NotStarted:
statusFilter = "Status='Not Started'";
break;
default:
break;
}
dv = new DataView(dv.ToTable(), statusFilter, null, DataViewRowState.CurrentRows);
Label Label1 = new Label();
Label1.Text = "There are no tasks to be completed by you or your group";
gvTasks.DataSource = dv.ToTable();
gvTasks.DataBind();
if (dv.ToTable().Rows.Count == 0)
{
Label1.Visible = true;
}
else
Label1.Visible = false;
this.Controls.Add(gvTasks);
this.Controls.Add(Label1);
base.CreateChildControls();
you need to put above code into a webpart
http://go4answers.webhost4life.com/Example/fetching-task-list-logged-user-142648.aspx
You can think of using Content Query web part and query all tasks list within your site collection.
To view multiple fields from task list you can think of creating your own XSL Stylesheet and attach it with Content Query Web Part.
You can read more about this technique in A Little More about CQWP by Ankur Chourasia
Hope this helps.
You can always show information only from one specific site collection. Cross Site Collection Queries can only be done by using the search. You can configure a fixed keyword query for this.
It is possible to use SharePoint search as a central place to pull this information from. The challenge is that depending on your indexing schedule the information may not be as up to date as the source lists.
Another alternative is to use execute an SPSiteDataQuery against SPWeb's GetSiteData() method. The challenge here is that it may not scale well if you have a lot of site collections you need to query. Caching can help, but there are still limits.
I worked with an example of both in my book SP 2010 Business Application Blueprints and discussed the pros and cons to both.