Sanjaya on SharePoint/Microsoft technologies

a blog on technologies like .net, MOSS 2007, wss 3.0 etc

  • Flickr Photos

Posts Tagged ‘tasks webpart’

Single web part to display tasks assigned to current user and current user’s groups

Posted by sNjY on March 21, 2008

The kind of requirement is understood from the title itself, but moss 2007, as such, donot have any out of the box webparts to achieve this.

We do have ‘by my groups’ view to display tasks assigned to current user’s groups.

and ‘my taks’ view to display tasks assinged to current user.

After googling for some time to see if i have any webparts for free or if somebody has already done it and posted his ideas, i ended up writing one on my own.

Here is the source code for the base version.

using System;

using System.Collections.Generic;

namespace sa.WebParts.WebPartToDisplayGroupTasks{ [DefaultProperty(“Text”),ToolboxData(“<{0}:SPWebPart runat=server></{0}:SPWebPart>”),

XmlRoot(Namespace = “sa.WebParts.WebPartToDisplayGroupTasks”)]public class SPWebPart : Microsoft.SharePoint.WebPartPages.WebPart

{private const EnumStatus _defaultStatus = EnumStatus.NotCompleted;private EnumStatus _enumStatus = _defaultStatus;

GridView GridView1 = new GridView();public enum EnumStatus

{NotStarted = 0,

InProgress = 1,

Completed=2,

NotCompleted=3

}

[Browsable(true),Category(“Configuration”),

DefaultValue(EnumStatus.NotCompleted),WebPartStorage(Storage.Personal),

FriendlyName(“Status”),Description(“Select Status.”)]

public EnumStatus Status{get { return _enumStatus; }set

{_enumStatus = value;}}

public SPWebPart(){

this.Title = “Webpart to display Group Tasks”;} protected override void CreateChildControls(){Microsoft.SharePoint.SPList listSiteSetup = SPContext.Current.Web.Lists[“Tasks”];Microsoft.SharePoint.SPQuery query = new Microsoft.SharePoint.SPQuery();Microsoft.SharePoint.

SPListItemCollection items = listSiteSetup.Items;DataTable dtTasks = items.GetDataTable();

DataColumn dtLink = new DataColumn(“link”);dtTasks.Columns.Add(dtLink);//Columns to Display

string[] link = new string[1];link[0] = “link”;

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”;GridView1.Columns.Add(hlfTitle);GridView1.Columns.Add(bfStatus);

GridView1.Columns.Add(bfStartDate);

GridView1.Columns.Add(bfDueDate);

GridView1.Columns.Add(bfAssignedTo);

GridView1.AutoGenerateColumns = false;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”;GridView1.DataSource = dv.ToTable();GridView1.DataBind();

if (dv.ToTable().Rows.Count == 0){ Label1.Visible = true;}else

Label1.Visible = false; this.Controls.Add(GridView1);this.Controls.Add(Label1);

base.CreateChildControls();}}

}

The approach is very obvious from the code above: yet again here it is in short:

1. Fetch the tasks from the tasks list.

2.Get the datatable version of tasks.

3.Decide on the columns to be shown and get the data from the datatable.

4.Apply necessary filters to the data ( Assigned to equal to Current user or current users groups.

5.Render the grid view control after binding the datasource.

6.THERE YOU Go!

7.There is Status filter also, which is a cusmizable property of the webpart and can be set to Completed, Not completed, In progress or Not started.

Posted in Technology | Tagged: , , , | 29 Comments »