Return List<myobject> using REST from SharePoint 2010


I’m having a good time playing with WCF tonight. This is a quick post on the setup for returning lists of objects defined in a WCF REST service from within SharePoint 2010 – a common scenario. For related information see the following posts

Its actually very simple but please see my previous posts for more detail. Assuming you have a separate file for the interface you can return the sample List<Project> by defining a class that looks something like the following

   public class Project {

       public string Name;

       public DateTime StartDate;

       public DateTime EndDate;

Obviously, this is a simple demonstration – next I implement the actual method as follows

public List<Project> EnumProjects()

            List<Project> projects = new List<Project>();

            // add a new project sample data
                new Project {
                    Name = "Project 2",
                    StartDate = DateTime.Parse("3/3/2012"),
                    EndDate = DateTime.Parse("12/15/2012")

            return projects;

The service itself looks as follows (in fact the WebGet attribute can be further simplified)

[WebGet(UriTemplate = "/EnumProjects", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
List<Project> EnumProjects(string webUrl);

For XML the result looks as follows,

– <ArrayOfProject xmlns=”” xmlns:i="">
– <Project>
  <Name>Project 2</Name>

For Json the result looks as follows,

[{"EndDate":"\/Date(1355547600000-0500)\/","Name":"Project 2","StartDate":"\/Date(1330750800000-0500)\/"}]


Crazy Simple!


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