Creating API for the ONLYOFFICE Workspace custom modules

Introduction

If you created your own module for ONLYOFFICE and added it to ONLYOFFICE Workspace following these instructions, you can also create API for this module.

How to create API for your own module

  1. Create a Class Library (ASC.Api.Sample) project and put it to the ...module\ASC.Api\ASC.Api.Sample folder.
    IMPORTANT!!! The output dll file name must be "ASC.Api.*.dll";
  2. Connect the required references from ...\web\studio\ASC.Web.Studio\bin\:
    ASC.Api.dll
    ASC.Web.Sample.dll
  3. Create the SampleApi class and implement the IApiEntryPoint interface:
    public class SampleApi : IApiEntryPoint{public string Name{get { return "sample"; }}}
  4. Create public methods with the specific attributes:
    [Attributes.Create("create", false)]public SampleClass Create(string value){return SampleDao.Create(value);}

    The attribute specifies the type of method, the path by which this method will be called, the authorization and verification of the tariff plan in it. The possible options are shown below:

    CreateAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "POST" requestUpdateAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "PUT" requestDeleteAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "DELETE" requestReadAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "GET" request

    the requiresAuthorization, checkPayment parameters are optional and have a value of true by default.

  5. Set the output path in the project properties as:
    <OutputPath>..\..\..\web\studio\ASC.Web.Studio\bin\</OutputPath><DocumentationFile>..\..\..\web\studio\ASC.Web.Studio\bin\ASC.Api.Sample.XML</DocumentationFile>

    so that the builds were created at the web\studio\ASC.Web.Studio\bin folder.

  6. The project can be built manually or using the builder. For the latter add the following lines to the build\msbuild\build.proj file:
    <ProjectToBuild Include="$(ASCDir)module\ASC.Api\ASC.Api.Sample\ASC.Api.Sample.csproj"/>

    and run the build\Build.bat file.

  7. IMPORTANT!!! Add ASC.Api.Sample.SampleApi to the web\studio\ASC.Web.Studio\web.autofac.config file:
    <componenttype="ASC.Api.Sample.SampleApi, ASC.Api.Sample"service="ASC.Api.Interfaces.IApiEntryPoint, ASC.Api"name="sample"/>
  8. Build the project, run the website and test the method by making a query with jQuery:
    $.ajax({
        type: "POST",
        url: "http://localhost:port/api/2.0/sample/create.json",
        data: {value: "create"}});

Host ONLYOFFICE Workspace on your own server

Article with the tag:
Browse all tags