Sanjaya on SharePoint/Microsoft technologies

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

  • Flickr Photos

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.

29 Responses to “Single web part to display tasks assigned to current user and current user’s groups”

  1. Vini said

    hi,
    how do you do grouping of tasks in your webpart?

  2. Davy said

    Can this custom webpart fetch all the tasks from the site collection? Because I use a content by query filter webpart (out-of-the-box) that filters on content type Tasks Assigned to [Me] from all sites of the site collection. The big disadvantage is that it doesn’t show tasks assigned to the user’s groups. I also noticed that it generates errors when I assign a task to multiple users. Can this webpart handle this?

  3. sNjY said

    vini,

    i hope the grid view control has grouping..Need to check.

    Davy,

    yes we can address ur requirement with this web part with a bit of extra coding.Rt. now i fetch tasks from Tasks list of root web only and filter them. for site collection, loop through all the task lists across all the webs in site collection and then filter them.

    Thanks!

  4. Davy said

    Hi,

    I have another question.
    You say that SharePoint has ‘by my groups’ view to display tasks assigned to current user’s groups.
    Is this function available in the out-of-the-box version?
    Because I can only create a view where assigned to equals [Me] but I don’t know how to create a view to display items (cfr. tasks) assigned to the current user’s groups. How/where do you apply this?

    Thanks in advance,

    Best regards,

    Davy

  5. sNjY said

    yes ‘by my groups’ view is available OUT OF THE BOX.

  6. sNjY said

    AND can’t recall the page name rt. now.

    its available at this location:

    http://sitename/lists/task/$$$$.aspx

    Pls Check the site pages there using SPD.
    its somehting like *grptasks*.aspx.This page has a LVWP to display tasks assigned to current user’s groups.

  7. mygrtsks.aspx said

    Lists/Tasks/mygrtsks.aspx

  8. Davy said

    Hello,

    thanks for the location of mygrptsks.aspx.
    I have another question. the ‘by my groups’ view is limited to one particular tasklist where you’ve implemented that view. it works fine.
    What I need is a tasklist that contains all the tasks from all the separate tasklists in the SharePoint site collection. Is there a way to achieve this out-of-the-box?Then I could use that general tasklist to filter all the tasks by ‘my user’ or ‘by my groups’

    Thanks in advance;

    Best regards,

    Davy

  9. sNjY said

    davy,

    i don’t think we can achieve that functionality out-of-the-box, but ofcourse with little bit of work, such as

    One way to achieve that is through a custom web part with little bit of custom coding as explained above. (you would still need to fetch tasks from multiple tasks lists across many webs)

    Another way could be to bind an event handler in all the sub sites task list, to copy a new task item created to root web task list (supposing that you’ll use root webs task list for your view). Also, i hope that you have same users and groups across all the webs in your site collection. This way although the tasks are created in sub sites, they will be copied to root web task list and you will have a single view.

  10. […] https://sanjayapdl.wordpress.com/2008/03/21/single-web-part-to-display-tasks-assigned-to-current-user… […]

  11. Kirshna Prasad said

    Hi,

    I’m pretty sure that we can achieve this using out of the box functionality.
    One way of doing this is converting listview webpart to Dataview webpart and then apply formatting(conditional) and filters to the web part using Sharepoint Designer.

  12. sNjY said

    Krishna,
    that would be very nice.
    but how will you get all the names of the groups current user belongs to when you apply filters (for assigned to) to the data view web part in sharepoint designer?
    This was what i could not get, so i thought of my custom web part.
    I would appreciate your answer.
    Thanks.

  13. Karen said

    This can be done without coding.
    1. Create a web part for the tasks
    2. Go to ‘Modify Shared Web Part’
    3. From Selected View drop down list, select ‘By My Groups’
    Then this web part shows the tasks assigned to the groups that the current user is in.

  14. sNjY said

    You are right Karen!
    and the ‘By My Groups’ view exists and it displays the tasks assigned to the groups the current user is in, but the question (as per the article) is about displaying all the tasks assigned to the current user plus current user’s groups at one place, which can not be achieved out of the box.
    I hope you are with me.

  15. Terry said

    Is the ByMyGroup view only available in the Task List? I need to use the Issues Tracking Task List which does not have the By My Group when you first create it? Is there a way to create that view in the Issues Tracking Tast list? If so, can you tell me how?

  16. Thomas said

    I have a DataSheet View webpart. Is there any method to filter it dynamically by reading value from querystring?

    Thanks,
    Thomas

  17. sNjY said

    Terry,

    I didn’t find ByMyGroup view for Issues Tracking list.
    However, If the group name /names you want to filter by are fixed, then a filter can be applied at design time through sharepoint GUI or sharepoint designer for a list view.

    Thanks,
    Sanjaya

  18. sNjY said

    Thomas,

    If your DataSheet view webpart is a custom webpart and you have access to its code, you can apply filter through code by picking up the query string value.

    and if its an out of the box list view web part or data form web part then you can apply filter through sharepoint designer.

    Thanks!

  19. Thomas said

    Hi Sanjaya,

    Mine is an out of the box List View WebPart – converted to ‘DataSheet View’. Option for adding parameters for dynamic filtering was not available. But the same is available for ‘DataView’ WebPart. I need ‘DataSheet View’ itself since that is the requirement
    Please help me. I need the solution very badly.

    You mentioned about Datasheet View custom webpart. How to do that? any demo link?

    Thank You

    Thomas

  20. Munira said

    I created a task list & added to it some existing tasks lists web parts for the purpose of grouping the permissions. But the issue is that the view which shows the list of different filters in a dropdowm list at the standard toolbar is not displayed on the toolbar of the added task list web parts

  21. Rens said

    I created something like this: http://easytasklist.codeplex.com/

  22. preddi said

    Hey Hi sanjaya,

    I really liked your post on webparts to display tasks, but have a small question, does this whole process of creating web-part implies with WSS or with “MOSS” product, because i am specifically looking to create this process using WSS.

    Thanks

  23. preddi said

    I would really appreciate if you could navigate in creating webpart that can display the assigned tasks for user through out the site collection.

    Thanks.

  24. sNjY said

    Preddi,

    yes, i believe it applies to WSS too

    and with a little bit of change in the code, you can query the tasks from many site collections and merge them to a single datatable before binding to a data grid.

    Thanks.

  25. preddi said

    Thank you Sanjaya,

    So you say i CAN create the web-part without using MOSS?

  26. preddi said

    Can You please tell me if i can deploy the above webpart without using MOSS?

    Thanks,

  27. Christophe said

    I know this post is a little bit old, but the following may help:

    1/ display tasks assigned to current user and current user’s groups
    This can be done with a CAML query, Marc Anderson just provided an answer on his blog (also see the comment):

    CAML Filter for Current Group Membership

    2/ fetch all tasks from a site collection: I expect this to be possible with a DVWP in crosslist mode. I plan to add the option to my tasks lists roll-up soon:
    http://www.pathtosharepoint.com/sharepoint-user-toolkit/Pages/Tasks-Lists-Rollup.aspx

  28. tash said

    It’s a out-of-box functionality.
    You can use the DataFormWebPart with SPdataSource like this:

    This way you will get the items from that list that are assigned to the User directly or to his group.
    If you need aggregated information from several list – just add AggregateDataSource

  29. alex said

    Hi Sanjay,
    I found ur blog extremely useful.

    Could u tell me how do I modify this in order that it will show me the Tasks across all the SiteCollections of a Web Application..?? and not just from a single SiteCollection.

    Thanks,
    Alex

Leave a reply to Munira Cancel reply