Wednesday, August 30, 2017

office/Extending SharePoint 2013

Download

Introduction

Ever wanted to deploy your own REST endpoints to the same _api endpoint as other SharePoint server REST endpoints to bypass cross-domain issues?  Well, this project will show you how!

Building the Sample

This sample is a full Visual Studio SharePoint Farm Solution project.  You simply need to change the deploy server url and right-click the project and select "Deploy"

Description

This sample has a fully working and deployable instance of extending SharePoint REST!

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!

 

C#
Edit|Remove
namespace CJG.ServerStub   {       [ClientCallableType(Name="CJG", ServerTypeId="{E1BB82E8-0D1E-4e52-B90C-684802AB4EF7}", FactoryType=typeof(CJGObjectFactory))]       public class CJG       {           public CJG()           {              }              [ClientCallableProperty]           public string FirstName { getset; }              [ClientCallableMethod]           public int ReturnInt()           {               System.Random r = new Random();               return r.Next();           }              [ClientCallable]           public DateTime ReturnDate()           {               return DateTime.Now;           }       }   }

Source Code Files

  • CJG.cs - has both the Server and Client side class (ideally this would be in two seperate assemblies).
  • CJGObjectFactory.cs - the class that generated instances of a CJG object
  • CJGServerStub.cs - the wrapper around the server object model class that tells the REST engine what endpoints are available in the EDMX mode.

More Information

Original blog post is here.  You can always email me at chris@architectingconnectedsystems with questions!

No comments:

Post a Comment