Creating API for custom modules

Introduction

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

creating an API for a custom module

  1. Create a Class Library (ASC.Api.Sample) project and place it in the ...module\ASC.Api\ASC.Api.Sample folder.
    IMPORTANT!!! The output DLL file name must be ASC.Api.*.dll;
  2. Add 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" request
    UpdateAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "PUT" request
    DeleteAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "DELETE" request
    ReadAttribute(string path, bool requiresAuthorization = true, bool checkPayment = true) //corresponds to the "GET" request

    The requiresAuthorization and checkPayment parameters are optional and defqult to true.

  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 builds are output to 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:
    <component
              type="ASC.Api.Sample.SampleApi, ASC.Api.Sample"
              service="ASC.Api.Interfaces.IApiEntryPoint, ASC.Api"
              name="sample"/>
  8. Build the project, start 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"}
    });
Article with the tag:
Browse all tags