Generate C# classes for SharePoint REST API

16/05/2014

A few weeks ago I posted an article on how to generate C# classes from a odata rest endpoint, with a visual studio extension. In that example I used the Exchange REST endpoints, that is based on OData v4.

But if you like me try to use the same tool to generate C# classes for the SharePoint REST API you will find out that it fails. This is because SharePoint’s REST API is only based on Odata version 3, which the tool can’t handle. You can see that by inspecting the result of a call to http://yourdomain.sharepoint.com/_api/$metadata which will return:
[xml]
<edmx:Edmx Version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

[/xml]
The DataServiceVersion indicates that this schema is odata version 3.

Instead you can use datasvcutil.exe, but you have to be aware of what version of the tool you are using. If you like me are used to just starting a visual studio command prompt, and call the tool from there, then it won’t work. Because that is an old version of datasvcutil that only supports Odata v1 and v2.

Instead you have to download WCF Data Services 5.6.0 RTM Tools Installer that comes with an updated version of datasvcutil.exe that can generate classes based on odata version 3. Once install save your sharepoint metadata to a local file e.g. c:\spo.edmx (the file extension must be edmx) and call the tool with the following command line.

c:\Program Files (x86)\Microsoft WCF Data Services\5.6\bin\tools>datasvcutil /in:c:\spo.edmx /out:sharepoint.cs /version:3.0

Now sharepoint.cs contains nice classes for everything you can do with the SharePoint REST API, all ready to be used with the Office 365 API Tools, see my article on that topic here.

Here’s a final example of the SP.File class you get
[csharp]
/// <summary>
/// There are no comments for SP.File in the schema.
/// </summary>
/// <KeyProperties>
/// Name
/// </KeyProperties>
[global::System.Data.Services.Common.DataServiceKeyAttribute("Name")]
public partial class File
{
public File()

{

}
/// <summary>
/// Create a new File object.
/// </summary>
/// <param name="checkOutType">Initial value of CheckOutType.</param>
/// <param name="customizedPageStatus">Initial value of CustomizedPageStatus.</param>
/// <param name="exists">Initial value of Exists.</param>
/// <param name="length">Initial value of Length.</param>
/// <param name="level">Initial value of Level.</param>
/// <param name="majorVersion">Initial value of MajorVersion.</param>
/// <param name="minorVersion">Initial value of MinorVersion.</param>
/// <param name="name">Initial value of Name.</param>
/// <param name="timeCreated">Initial value of TimeCreated.</param>
/// <param name="timeLastModified">Initial value of TimeLastModified.</param>
/// <param name="uIVersion">Initial value of UIVersion.</param>
/// <param name="uniqueId">Initial value of UniqueId.</param>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public static File CreateFile(int checkOutType, int customizedPageStatus, bool exists, long length, byte level, int majorVersion, int minorVersion, string name, global::System.DateTime timeCreated, global::System.DateTime timeLastModified, int uIVersion, global::System.Guid uniqueId)
{
File file = new File();
file.CheckOutType = checkOutType;
file.CustomizedPageStatus = customizedPageStatus;
file.Exists = exists;
file.Length = length;
file.Level = level;
file.MajorVersion = majorVersion;
file.MinorVersion = minorVersion;
file.Name = name;
file.TimeCreated = timeCreated;
file.TimeLastModified = timeLastModified;
file.UIVersion = uIVersion;
file.UniqueId = uniqueId;
return file;
}
/// <summary>
/// There are no comments for Property CheckInComment in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string CheckInComment
{
get
{
return this._CheckInComment;
}
set
{
this.OnCheckInCommentChanging(value);
this._CheckInComment = value;
this.OnCheckInCommentChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _CheckInComment;
partial void OnCheckInCommentChanging(string value);
partial void OnCheckInCommentChanged();
/// <summary>
/// There are no comments for Property CheckOutType in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public int CheckOutType
{
get
{
return this._CheckOutType;
}
set
{
this.OnCheckOutTypeChanging(value);
this._CheckOutType = value;
this.OnCheckOutTypeChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private int _CheckOutType;
partial void OnCheckOutTypeChanging(int value);
partial void OnCheckOutTypeChanged();
/// <summary>
/// There are no comments for Property ContentTag in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string ContentTag
{
get
{
return this._ContentTag;
}
set
{
this.OnContentTagChanging(value);
this._ContentTag = value;
this.OnContentTagChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _ContentTag;
partial void OnContentTagChanging(string value);
partial void OnContentTagChanged();
/// <summary>
/// There are no comments for Property CustomizedPageStatus in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public int CustomizedPageStatus
{
get
{
return this._CustomizedPageStatus;
}
set
{
this.OnCustomizedPageStatusChanging(value);
this._CustomizedPageStatus = value;
this.OnCustomizedPageStatusChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private int _CustomizedPageStatus;
partial void OnCustomizedPageStatusChanging(int value);
partial void OnCustomizedPageStatusChanged();
/// <summary>
/// There are no comments for Property ETag in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string ETag
{
get
{
return this._ETag;
}
set
{
this.OnETagChanging(value);
this._ETag = value;
this.OnETagChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _ETag;
partial void OnETagChanging(string value);
partial void OnETagChanged();
/// <summary>
/// There are no comments for Property Exists in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public bool Exists
{
get
{
return this._Exists;
}
set
{
this.OnExistsChanging(value);
this._Exists = value;
this.OnExistsChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private bool _Exists;
partial void OnExistsChanging(bool value);
partial void OnExistsChanged();
/// <summary>
/// There are no comments for Property Length in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public long Length
{
get
{
return this._Length;
}
set
{
this.OnLengthChanging(value);
this._Length = value;
this.OnLengthChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private long _Length;
partial void OnLengthChanging(long value);
partial void OnLengthChanged();
/// <summary>
/// There are no comments for Property Level in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public byte Level
{
get
{
return this._Level;
}
set
{
this.OnLevelChanging(value);
this._Level = value;
this.OnLevelChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private byte _Level;
partial void OnLevelChanging(byte value);
partial void OnLevelChanged();
/// <summary>
/// There are no comments for Property MajorVersion in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public int MajorVersion
{
get
{
return this._MajorVersion;
}
set
{
this.OnMajorVersionChanging(value);
this._MajorVersion = value;
this.OnMajorVersionChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private int _MajorVersion;
partial void OnMajorVersionChanging(int value);
partial void OnMajorVersionChanged();
/// <summary>
/// There are no comments for Property MinorVersion in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public int MinorVersion
{
get
{
return this._MinorVersion;
}
set
{
this.OnMinorVersionChanging(value);
this._MinorVersion = value;
this.OnMinorVersionChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private int _MinorVersion;
partial void OnMinorVersionChanging(int value);
partial void OnMinorVersionChanged();
/// <summary>
/// There are no comments for Property Name in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string Name
{
get
{
return this._Name;
}
set
{
this.OnNameChanging(value);
this._Name = value;
this.OnNameChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _Name;
partial void OnNameChanging(string value);
partial void OnNameChanged();
/// <summary>
/// There are no comments for Property ServerRelativeUrl in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string ServerRelativeUrl
{
get
{
return this._ServerRelativeUrl;
}
set
{
this.OnServerRelativeUrlChanging(value);
this._ServerRelativeUrl = value;
this.OnServerRelativeUrlChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _ServerRelativeUrl;
partial void OnServerRelativeUrlChanging(string value);
partial void OnServerRelativeUrlChanged();
/// <summary>
/// There are no comments for Property TimeCreated in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public global::System.DateTime TimeCreated
{
get
{
return this._TimeCreated;
}
set
{
this.OnTimeCreatedChanging(value);
this._TimeCreated = value;
this.OnTimeCreatedChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private global::System.DateTime _TimeCreated;
partial void OnTimeCreatedChanging(global::System.DateTime value);
partial void OnTimeCreatedChanged();
/// <summary>
/// There are no comments for Property TimeLastModified in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public global::System.DateTime TimeLastModified
{
get
{
return this._TimeLastModified;
}
set
{
this.OnTimeLastModifiedChanging(value);
this._TimeLastModified = value;
this.OnTimeLastModifiedChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private global::System.DateTime _TimeLastModified;
partial void OnTimeLastModifiedChanging(global::System.DateTime value);
partial void OnTimeLastModifiedChanged();
/// <summary>
/// There are no comments for Property Title in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string Title
{
get
{
return this._Title;
}
set
{
this.OnTitleChanging(value);
this._Title = value;
this.OnTitleChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _Title;
partial void OnTitleChanging(string value);
partial void OnTitleChanged();
/// <summary>
/// There are no comments for Property UIVersion in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public int UIVersion
{
get
{
return this._UIVersion;
}
set
{
this.OnUIVersionChanging(value);
this._UIVersion = value;
this.OnUIVersionChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private int _UIVersion;
partial void OnUIVersionChanging(int value);
partial void OnUIVersionChanged();
/// <summary>
/// There are no comments for Property UIVersionLabel in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string UIVersionLabel
{
get
{
return this._UIVersionLabel;
}
set
{
this.OnUIVersionLabelChanging(value);
this._UIVersionLabel = value;
this.OnUIVersionLabelChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _UIVersionLabel;
partial void OnUIVersionLabelChanging(string value);
partial void OnUIVersionLabelChanged();
/// <summary>
/// There are no comments for Property UniqueId in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public global::System.Guid UniqueId
{
get
{
return this._UniqueId;
}
set
{
this.OnUniqueIdChanging(value);
this._UniqueId = value;
this.OnUniqueIdChanged();
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private global::System.Guid _UniqueId;
partial void OnUniqueIdChanging(global::System.Guid value);
partial void OnUniqueIdChanged();
/// <summary>
/// There are no comments for Author in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public User Author
{
get
{
return this._Author;
}
set
{
this._Author = value;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private User _Author;
/// <summary>
/// There are no comments for CheckedOutByUser in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public User CheckedOutByUser
{
get
{
return this._CheckedOutByUser;
}
set
{
this._CheckedOutByUser = value;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private User _CheckedOutByUser;
/// <summary>
/// There are no comments for ListItemAllFields in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public ListItem ListItemAllFields
{
get
{
return this._ListItemAllFields;
}
set
{
this._ListItemAllFields = value;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private ListItem _ListItemAllFields;
/// <summary>
/// There are no comments for LockedByUser in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public User LockedByUser
{
get
{
return this._LockedByUser;
}
set
{
this._LockedByUser = value;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private User _LockedByUser;
/// <summary>
/// There are no comments for ModifiedBy in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public User ModifiedBy
{
get
{
return this._ModifiedBy;
}
set
{
this._ModifiedBy = value;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private User _ModifiedBy;
/// <summary>
/// There are no comments for Versions in the schema.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public global::System.Collections.ObjectModel.Collection<FileVersion> Versions
{
get
{
return this._Versions;
}
set
{
if ((value != null))
{
this._Versions = value;
}
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private global::System.Collections.ObjectModel.Collection<FileVersion> _Versions = new global::System.Collections.ObjectModel.Collection<FileVersion>();
}
[/csharp]