0

I am having trouble aggregating info on all tasks from current user programmatically. This is what I got so far:

SPWeb web = SPContext.Current.Web;

SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = "<Lists ServerTemplate=\"107\" />";
query.Webs = "<Webs Scope=\"SiteCollection\" />";
query.ViewFields = "<FieldRef Name=\"Title\"/>" + "<FieldRef Name=\"AssignedTo\" />" + "<FieldRef Name=\"Status\" />";

DataTable results = web.GetSiteData(query);

This gets me all tasks from Tasks lists across the entire site collection which I can later filter. However it will not count the tasks that the user assigned to himself in his MySite for example.

Here is how I tried to get all the tasks from the user's MySite:

using (SPSite site = new SPSite("http://myhostname:8080/personal/username/"))
{
    using (SPWeb mySiteWeb = site.OpenWeb())
    {
        SPList list = mySiteWeb.Lists["WmaAggregatorList_User"];

        foreach (SPListItem listItem in list.Items)
        {
            this.Controls.Add(new LiteralControl("<br>" + listItem.Title + "(" + listItem.DisplayName + ") - " + listItem.ContentTypeId + "<br>"));
        }
    }
}

This works to a degree - It gives me a lot of empty fields and fields that are definitely not tasks:

((no title)) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 ((no title)) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 SPTaskListProvider(SPTaskListProvider) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 ((no title)) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 My Machine Name(My Machine Name) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 My Machine Name: Tasks(My Machine Name: Tasks) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 ((no title)) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 First Task(First Task) - 0x001CF7603F6B21D44FB39AE4F99715DEA7 Second Task(Second Task) - 0x001CF7603F6B21D44FB39AE4F99715DEA7

Only the last two are actually users' tasks. Would anybody have an idea of a good way to extract user's tasks from his mysite and filter out the unneeded data?

1

So this is what I could come up with - in the following code i check whether the list item is actually a task and if it is completed or not:

using (SPSite site = new SPSite("http://hostname:8080/personal/" + loginName + "/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["WmaAggregatorList_User"];

        foreach (SPListItem listItem in list.Items)
        {
            if (listItem["TypeIdField"] as int? == 0)
            {
                try
                {
                    if (listItem["TxIsCompleted"] as bool? != true)
                    {
                        IncompleteTasksCounter++;
                    }
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(
                        "Rafals Error",
                        TraceSeverity.Unexpected,
                        EventSeverity.Error),
                        TraceSeverity.Unexpected, ex.Message);
                }
            }
        }
    }
}

So far it works good.

-1

The list 'WmaAggregatorList_User' does contain other items as well. You can filter on contenttypeId to get only the tasks.

The mytask list on the mysite does not show completed tasks. So you might also need to implemented a check if the tasks are completed/done or not.

  • How do I filter it? The way I do it in the original post, it seems like all those fields have the same content type ID. Am I missing something here? – Rafał Saltarski Oct 31 '13 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.