Getting SharePoint Task Assignee
One of the more interesting problems I’ve been working with is trying to figure out how to get the user information for the assignee (whom the task is assigned to). Getting the user ID and the the user display name is easy enough, but then that entails another lookup to find the the login name for the user.
For example, here is some code which retrieves the user information, but as a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-family: Courier New;"><span style="color: #0000ff;">using </span><span style="color: #000000;">(SPSite site = </span><span style="color: #0000ff;">new </span><span style="color: #000000;">SPSite(siteUrl))</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">using </span><span style="color: #000000;">(SPWeb web = site.OpenWeb())</span> <span style="color: #000000;">{</span> <span style="color: #000000;">SPList tasks = web.Lists[</span><span style="color: #ff00ff;">"Tasks"</span><span style="color: #000000;">];</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">foreach </span><span style="color: #000000;">(SPListItem task </span><span style="color: #0000ff;">in </span><span style="color: #000000;">tasks.Items)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">string </span><span style="color: #000000;">assignedTo = </span><span style="color: #808000;">Convert</span><span style="color: #000000;">.ToString(task[</span><span style="color: #ff00ff;">"Assigned To"</span><span style="color: #000000;">]);</span></span> <span style="font-family: Courier New;"> <span style="color: #808000;">Console</span><span style="color: #000000;">.Out.WriteLine(</span><span style="color: #ff00ff;">"> {0}"</span><span style="color: #000000;">, assignedTo);</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span></span> |
This generates the following output:
1 2 3 4 5 |
> 37;#Brad Wright > 40;#Daniel OConnor > 46;#Charles |
As mentioned, this is only part of the information we’re after. To get the login name, we’d have to do some simple string parsing and then make a separate call to look up the user. Not a terrible amount of work, but more work, nonetheless.
Stepping through the debugger, I could see that the actual type of the field was SPFieldUserValue. My first attempt was to see if I could convert the value directly; no go. It turns out that the value retrieved (before calling the ToString()) is already a string. I ended up fumbling around with the very awkward GetFieldValue() method on the SPField class. It’s not at all intuitive on how this method is supposed to be used, so I’m hoping this is useful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span style="font-family: Courier New;"><span style="color: #0000ff;">using </span><span style="color: #000000;">(SPSite site = </span><span style="color: #0000ff;">new </span><span style="color: #000000;">SPSite(siteUrl))</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">using </span><span style="color: #000000;">(SPWeb web = site.OpenWeb())</span> <span style="color: #000000;">{</span> <span style="color: #000000;">SPList tasks = web.Lists[</span><span style="color: #ff00ff;">"Tasks"</span><span style="color: #000000;">];</span></span> <span style="font-family: Courier New;"> <span style="color: #0000ff;">foreach </span><span style="color: #000000;">(SPListItem task </span><span style="color: #0000ff;">in </span><span style="color: #000000;">tasks.Items)</span> <span style="color: #000000;">{</span> <span style="color: #0000ff;">string </span><span style="color: #000000;">fieldValue = </span><span style="color: #808000;">Convert</span><span style="color: #000000;">.ToString(task[</span><span style="color: #ff00ff;">"Assigned To"</span><span style="color: #000000;">]);</span></span> <span style="font-family: Courier New;"> <span style="color: #000000;">SPFieldUserValue assignedTo = (SPFieldUserValue)</span> <span style="color: #000000;">task.Fields[</span><span style="color: #ff00ff;">"Assigned To"</span><span style="color: #000000;">].GetFieldValue(fieldValue);</span></span> <span style="font-family: Courier New;"> <span style="color: #808000;">Console</span><span style="color: #000000;">.Out.WriteLine(</span><span style="color: #ff00ff;">"> {0} ({1})"</span><span style="color: #000000;">, </span> <span style="color: #000000;">assignedTo.User.Name, assignedTo.User.LoginName);</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span> <span style="color: #000000;">}</span></span> |
And here is the output
1 2 3 4 5 |
> Brad Wright (FP1\bwright) > Daniel OConnor (FP1\doconnor) > Charles (FP1\charles) |
The call is all kind of awkward and is completely non-intuitive. It’s easier to think of it as item.Fields[“Some Field”].ConvertToNativeOutputType(fieldValue). So you can see, this is a much more convenient way of retrieving the user from a list item (or any typed return value) once you get the hang of the weird nomenclature and usability issues.