Second Life of a Hungarian SharePoint Geek

May 9, 2013

Extending the classes generated by SPMetal to access the login name of the users

Filed under: LINQ, SP 2010 — Tags: , — Peter Holpar @ 13:25

Recently I was working on a console application that collects data from SharePoint list using LINQ for further analysis. Assume we have a list called Developers (no specific Content Type, only the default Item) with a field User (Person or Group, Allow multiple selections: No, People Only). Of course, we have in fact a lot more lists and columns, but they do not affect the lesson of this post.

I’ve generated the object model for LINQ using SPMetal, using a configuration XML for the Developers list like this fragment:

<List Name="Developers">
  <ContentType Name="Item" Class="Developer" />
</List>

Problem: We need the login name of the users in our report, however, using the default generated classed only the display name (User property) and the ID (UserId property) of the user are accessible.

Solution: Fortunately, we can extend the generated partial classes, for example, in our case we should add a new LoginName property to the Developer class. To achieve that, we have to implement the ICustomMapping interface.

Code Snippet
  1. public partial class Developer : ICustomMapping
  2. {
  3.     [CustomMapping(Columns = new String[] { "LoginName" })]
  4.     public void MapFrom(object listItem)
  5.     {
  6.         this.LoginName = null;
  7.  
  8.         SPListItem item = (SPListItem)listItem;
  9.         if (item["User"] != null)
  10.         {
  11.             using (SPWeb web = item.Web)
  12.             {
  13.                 SPFieldUserValue usr = new SPFieldUserValue(web, (string)item["User"]);
  14.                 this.LoginName = (usr.User != null) ? usr.User.LoginName : null;
  15.             }
  16.         }
  17.     }
  18.  
  19.     public void MapTo(object listItem)
  20.     {
  21.         throw new NotImplementedException();
  22.     }
  23.  
  24.     public void Resolve(RefreshMode mode, object originalListItem, object databaseObject)
  25.     {
  26.     }
  27.  
  28.     public string LoginName { get; internal set; }
  29. }

Note: In our case, we need only to read the account name, so (as you can see from the code above) I’ve implemented only the MapFrom method, but not the MapTo and Resolve methods. A solution that is able to write back the value might be a bit more complex, especially when one considers various validation and error handling scenarios.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: