Extending SharePoint 2013 REST APIs

As promised, here is my blog post on how to extend SharePoint 2013 REST APIs! This post is similar in technical depth as the Extending Ceres engine post here.

The general premise is to add your own end points to the REST APIs in an on-premise environment.  This is a totally valid scenario similar to extending sharepoint with your own Service Applications.  This technique is used by the Project Server team (totally different team from the SharePoint team) to extend the ProjectServer and ProjectData REST end points.  I have published the sample project here. Here are the high level steps of what you must do:

  1. Create a class decorated with ClientCallableType
    1. Set the name to the same name as the class
    2. Generate a new ServerTypeId set it
    3. Set the FactoryType to a Object Factory that you will create next
  2. Add methods and properties to the class, decorate with ClientCallable, ClientCallableMethod, ClientCallableProperty
  3. Create the Object Factory that inherits from ClientCllableObjectFactory (this will create an class instance using an identifier)
    1. Decorate the class with the Clietn
    2. CallableObjectFactory attribute, set the ServerTypeId to the one you generated for the class
    3. Implement the GetObjectById method
  4. Create a ServerStub class that inherits from Microsoft.SharePoint.Client.ServerStub
    1. Decorate the class with the ServerStub attribute, set the type to the class type, set the TargetTypeId to the ServerTypeId
    2. Implement a public constructor
    3. Override the following properties and methods
      1. TargetType (returns typeof(class))
      2. TargetTypeId
      3. TargetTypeScriptClientFullName (the name of your client target type you will create next)
      4. ClientLibraryTargets (what clients can call your rest endpoint)
      5. GetProperty Method
      6. InvokeConstructor method (for both the CSOM and REST calls)
      7. Constructor implementations called from InvokeConstructor
      8. InvokeMethod (this calls the object's method)
      9. GetMethods (this is required to tell the $metadata endpoint what is available)
      10. GetProperties (similar to GetMethods)
  5. Create another class in a .Client namespace
    1. Decorate it with the ScriptType attribute, set the name and ServerTypeId
    2. Make it inherit from ClientObject
    3. Create the properties and methods that match to your server side class
  6. Create a ScriptTypeFactory that implements the IScriptTypeFactory interface
    1. Implement the IFromJson method, use a switch statement to generate a Client class object based on the scriptType string
  7. Create the ProxyLibrary.xxx.xml file
    1. Deploy to the {SharePointRoot}/ClientCallable directory
  8. Open the project's AssemblyInfo.cs file
    1. Add a UrlSegmentAliasMap attribute
    2. Add a ClientNamespaceMap attribute
    3. Add a ClientTypeAssembly attribute
  9. Deploy the Farm Solution
  10. Perform an IIS Reset
  11. Hit a "_/api/$metadata" endpoint, you should see your end point displayed!

Enjoy!
Chris