Strong typed list item classes in SharePhone

Usually when dealing with SharePoint through its object model, you get a SPListItem object which can be annoying to work with, especially when you want to databind in Silverlight. This is why I went for a strong typed version in SharePhone and hopefully it will be easier to use for non-SharePoint developers or those just starting out with SharePoint.

A SharePhone list item starts with the BaseItem class. When creating your own classes, you need to extend BaseItem. This class contains basic list item properties which are standard for all SharePoint list items (such as Title, Name, Created date, modified date, author, etc). If you only want to work with these basic properties, then you can use BaseItem as is and not implement your own class.

Let’s say you’ve created a content type which contains the text field MyField.

Create a new class, let’s call it MyContentType:

public class MyContentType : BaseItem{


Then we add our MyField property:

public class MyContentType : BaseItem{
    public string MyField { get; set; }

However, for SharePhone to know how to map this property to fields in your SharePoint list, you need to tag the property with some information.

public class MyContentType : BaseItem{
    [CoreField(FieldName="MyFieldName", Importance=FieldImportance.Optional, PopulateRule=CorePopulateRule.Sharepoint)]
    public string MyField { get; set; }

The CoreField attribute tells the SharePhone library a few key pieces of information:

  • FieldName specifies the internal name of the field in SharePoint
  • Importance is either Optional or Required, specifying whether or not the field requires a value or not.
  • PopulateRule states how the property should be treated

    SharePoint says the value of the property comes from SharePoint and allows for saving the value back.

    SharePointReadOnly says the value should be read from SharePoint but never written back.

    None states that this property should be ignored when reading/writing to SharePoint.

As BaseItem implements INotifyPropertyChanged, you can also support this in your custom properties like so:

private string myField;
[CoreField(FieldName = "MyFieldName", Importance = FieldImportance.Optional, PopulateRule = CorePopulateRule.Sharepoint)]
public string MyField {
    get {
        return myField;
    set {
        myField = value;

(I had a transparent way of dealing with this before so that custom properties would automagically support this, but due to WP7 lacking the System.Reflection.Emit namespace, I had to remove this feature – hopefully I can add it back in at some point).

Now, to use our new class, we make use of our generic methods in SharePhone:

web.Lists.GetListByName<MyContentType>("TestList", (SPList<MyContentType> list) => {
    string myText = list.Items[0].MyField; 

Remember, to get the items you first need to load them into the collection using list.Items.LoadAllItems();

I was going to show an example of reading and writing data from/to SharePoint but I’ll save that for the next post so this one doesn’t turn into a lengthy read.


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: