IRestClient interface

Overview

The REST SDK works with the IRestClient interface and can be used through dependency injection. The IRestClient interface has the following primary methods for working with data:

  • GetItem
  • GetItems
  • CreateItem
  • UpdateItem
  • DeleteItem

Use in Sitefinity ASP.NET Renderer projects

The IRestClient interface is automatically registered and is initialized for each request.

To use it, you need to inject it in the constructor through dependency injection (DI), like in the following example:

using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.AspNetCore.Web;
using Progress.Sitefinity.RestSdk;
namespace Renderer.ViewComponents
{
public class DemoViewComponent : ViewComponent
{
private IRestClient restClient;
private IRenderContext renderContext;
/// Initializes a new instance of the DemoViewComponent class
public DemoViewComponent(IRestClient restClient, IRenderContext renderContext)
{
this.restClient = restClient;
this.renderContext = renderContext;
}
}
}

Use in custom implementations in Sitefinity ASP.NET Renderer projects

To use the IRestClient interface in custom controllers, you need to manually initialize the interface, like in the following example:

using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;
using Microsoft.Net.Http.Headers;
namespace Renderer.Controllers
{
public class TestController : Controller
{
private IRestClient restClient;
public TestController(IRestClient restClient)
{
this.restClient = restClient;
}
[HttpGet]
public async Task<IActionResult> GetTags()
{
var taxonType = RestClientContentTypes.GetTaxonType("Tags");
// necessary to initialize this. automatically done for page requests
// adds the current cookies, if the user is logged-in, so that the request is authenticated
var args = new RequestArgs();
var requestCookie = this.HttpContext.Request.Headers[HeaderNames.Cookie];
if (!string.IsNullOrEmpty(requestCookie))
args.AdditionalHeaders.Add(HeaderNames.Cookie, requestCookie);
await this.restClient.Init(args);
var result = await this.restClient.GetItems<TaxonDto>(new GetAllArgs()
{
Type = taxonType,
Fields = new[] { "Id", "Title" }
});
return this.Json(result);
}
}
}

Use in custom implementations in external ASP.NET Core projects

To use the IRestClient interface in external projects, which do not use Sitefinity ASP.NET Core Renderer, you need to manually:

  • Register the IRestClient interface on startup by adding the following services:

    // add the following services
    services.AddHttpClient("sfservice", (servicesProvider, client) =>
    {
    // mandatory: it is important not to skip the last backslash
    client.BaseAddress = new Uri("http://your.sitefinity.site/api/default/");
    // optional: the key is used for restriction of the Web Service. it is not used for managing content
    client.DefaultRequestHeaders.Add("X-SF-APIKEY", "your api key");
    }).ConfigurePrimaryHttpMessageHandler(configure =>
    {
    return new HttpClientHandler
    {
    AllowAutoRedirect = false,
    UseCookies = false,
    };
    })
    services.AddScoped<IRestClient>((x) =>
    {
    var factory = x.GetRequiredService<IHttpClientFactory>();
    var httpClient = factory.CreateClient("sfservice");
    var restClient = new RestClient(httpClient);
    return restClient;
    });
    view raw Program.cs hosted with ❤ by GitHub

  • Initialize the IRestClient interface for custom implementations, like in the example above.

Examples

The following examples demonstrate how to use the IRestClient interface:

  • Sample: Migrate data to Sitefinity CMS
  • Sample: Share data between widgets
  • Sample: Mega menu

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?