Skip to content
brad-wechter edited this page Sep 25, 2014 · 24 revisions

Install API module

To use a Better CMS API you need to install a BetterCms.Module.Api package:

Install-Package BetterCms.Module.Api

With BetterCms.Module.Api module installed, you can access Better CMS functionality. The following example is the easiest way to catch the main idea of how to get data from Better CMS.

Note: Do not forget to wrap the API code in using; the context object must be disposed after it was used:

using BetterCms.Module.Api;
using BetterCms.Module.Api.Infrastructure;
using BetterCms.Module.Api.Infrastructure.Enums;
using BetterCms.Module.Api.Operations.Pages.Widgets;
[...]
using (var api = ApiFactory.Create())
{
    var requestModel = new GetWidgetsModel();
    requestModel.Take = 5; // Retrieves 5 items
    requestModel.Skip = 3; // Skips 3 items

    requestModel.Order.Add("Name"); // Sorts by name ascending
    requestModel.Order.Add("CreatedOn", OrderDirection.Desc); // Then sorts by date descending

    requestModel.Filter.Connector = FilterConnector.And; // Filtering connector (optional, default value = FilterConnector.And)
    requestModel.Filter.Add("Name", "A", FilterOperation.StartsWith); // Filters by name: item should start with "A"
    requestModel.Filter.Add("Name", "B", FilterOperation.EndsWith); // Filters by name: item should start with "B"

    var innerFilter = new DataFilter(FilterConnector.Or); // Create inner filter
    innerFilter.Add("CreatedBy", "Name.Surname.1");
    innerFilter.Add("CreatedBy", "Name.Surname.2");
    requestModel.Filter.Inner.Add(innerFilter); // Add inner filter to filters list

    var request = new GetWidgetsRequest { Data = requestModel }; // Create request

    var widgets = api.Pages.Widgets.Get(request); // Execute query
}
[...]

The main idea is to create the API context and use the methods it provides.

All the available methods with requests and responses are explained in the API documentation.

Note: If you're having problems with ServiceStack versions, try installing ServiceStack dependencies in the provided order during package installation:

install-package ServiceStack.Text -version 3.9.71
install-package ServiceStack.Common -version 3.9.71
install-package ServiceStack.OrmLite.SqlServer -version 3.9.71
install-package ServiceStack.Redis -version 3.9.71
install-package ServiceStack -version 3.9.71

After these installations you can install API module: install-package BetterCms.Module.Api

Install Web API Module

To expose API via Web (Web API with REST), you need to install a BetterCms.Module.Api.Web package:

Install-Package BetterCms.Module.Api.Web

After web API is installed, API metadata can be reached under /bcms-api/metadata. If web API is not working, ensure that the handlers listed below are correctly inserted to web.config file:

<configuration>
  <system.web>
    <httpHandlers>
      <add path="bcms-api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <handlers>
      <add path="bcms-api*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
  </system.webServer>
</configuration>

Access API Data (Contracts Only)

To add a module with data contracts only you need to install a BetterCms.Module.Api.Abstractions package:

Install-Package BetterCms.Module.Api.Abstractions
Clone this wiki locally