---
title: Using the Xperience API externally
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This page describes how to configure external ASP.NET applications so that they support the Kentico Xperience API. Using Xperience functionality externally allows you to create scenarios such as:

- Separate applications integrated with your main Xperience website
- Custom websites or applications that use Xperience as a content repository

Start Visual Studio and open your external application's project. You need to perform the following steps:

1. [Connect to the Xperience database](#connecting-to-the-xperience-database)
2. [Integrate the Xperience API libraries](#integrating-xperience-api-libraries)
3. [Initialize the Xperience application](#initializing-the-xperience-application)

> **Note:** **License requirements**
>
> The Xperience API performs license checks even when running in an external application. For applications that run under a different domain than your main site (web applications, WCF services, etc.), you need to have a valid [license](https://docs.kentico.com/13/configuring-xperience/managing-sites/managing-site-license-keys.md) for the given domain.
>
> Add the license via the administration interface of the connected Xperience application – the license is available through the shared database.
>
> In some cases, e.g., when an external web application needs to modify data in Xperience, you may also need to add the application's domain as a [domain alias](https://docs.kentico.com/13/configuring-xperience/managing-sites/setting-domain-names-for-sites.md#adding-domain-aliases) of your main site.

## Connecting to the Xperience database

<!-- dev-model:mvc start -->

**MVC 5 development model.** Applies only when building with ASP.NET MVC 5. If this page also covers ASP.NET Core, that version is in its own block.

To be able to access the Xperience database, you need to specify the appropriate connection string in your application's **web.config** or **app.config** file.

Add the connection string into the _configuration/connectionStrings_ section using an __ element named **CMSConnectionString**.

```html

 <configuration>
  <connectionStrings>

    ...

    <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=Xperience;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;" />

  </connectionStrings>

```

> **Tip:** **Tip**: We recommend copying the exact connection string from the web.config file of your Xperience administration web project.

<!-- dev-model:mvc end -->

<!-- dev-model:core start -->

**ASP.NET Core development model.** Applies only when building with ASP.NET Core. If this page also covers MVC 5, that version is in its own block.

To be able to access the Xperience database, you need to specify the appropriate connection string in your application's configuration file.

By default, the system looks for application settings in the following root-level file:

- web applications: **appsettings.json**
- other (e.g., Console applications): **app.config**

Add the connection string into the _ConnectionStrings_ section under the **CMSConnectionString** key:

```js

"ConnectionStrings": {
    "CMSConnectionString": "Persist Security Info=False;database=Xperience;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;"
 }

```

> **Tip:** **Tip**: We recommend copying the exact connection string from the configuration file of your Xperience application.

<!-- dev-model:core end -->

## Integrating Xperience API libraries

Before you can use Xperience functionality in your application, you need to add the libraries containing the required code:

1. Right-click your application's project in the Visual Studio **Solution Explorer** and select **Manage NuGet Packages**.
2. Search for the [Kentico.Xperience.Libraries](https://www.nuget.org/packages/Kentico.Xperience.Libraries) package.
3. Install the _Kentico.Xperience.Libraries_ version that matches the version of the connected Xperience database.

> **Note:** **Maintaining Kentico.Xperience.Libraries**
>
> If you [upgrade or hotfix](https://docs.kentico.com/13/external-utilities/kentico-xperience-hotfix-and-upgrade-utility/upgrading-and-hotfixing-an-instance.md) the related Xperience project and its database to a newer version, you also need to update the NuGet packages to a matching version in your external application.

## Initializing the Xperience application

<!-- dev-model:mvc start -->

**MVC 5 development model.** Applies only when building with ASP.NET MVC 5. If this page also covers ASP.NET Core, that version is in its own block.

You need to initialize the system before making calls to its API from an external project.

**Web applications**

1. Edit your application's [Global.asax](http://msdn.microsoft.com/en-us/library/1xaas8a2%28v=vs.71%29.aspx) file (create the file if necessary).
2. Execute the **CMSApplication.Init** method in the **Application\_BeginRequest** method:

   ```csharp

   using CMS.DataEngine;

   ...

   void Application_BeginRequest(object sender, EventArgs e)
   {
       CMSApplication.Init();
   }

   ```

**Non-web applications (for example Console applications)**

1. Edit your application's **Main** method.
2. Execute the **CMSApplication.Init** method at any point before the first call of the Xperience API.

You can now use the Xperience API in your external application. The API allows you to perform any action available in the standard administration interface.

<!-- dev-model:mvc end -->

<!-- dev-model:core start -->

**ASP.NET Core development model.** Applies only when building with ASP.NET Core. If this page also covers MVC 5, that version is in its own block.

You need to initialize the system before making calls to its API from an external project.

**Web applications**

Call the  **CMSApplication.Init**  method before you begin interacting with the API (for example during application startup or within requests that make use of the API). The initialization sequence is performed only once and skipped during subsequent calls :

```csharp

using CMS.DataEngine;

...

CMSApplication.Init();

```

**Non-web applications (for example Console applications)**

1. Edit your application's **Main** method.
2. Execute the **CMSApplication.Init** method at any point before the first call of the Xperience API.

You can now use the Xperience API in your external application. The API allows you to perform any action available in the standard administration interface.

<!-- dev-model:core end -->

> **Note:** **Using custom code that requires discovery**
>
> For external projects that compile into a different output type than a DLL assembly (for example Console applications), do NOT directly add any custom classes that need to be detected during the API initialization. This includes classes registered using attributes, such as [generated wrapper classes](https://docs.kentico.com/13/developing-websites/generating-classes-for-xperience-objects.md) for objects, [custom module classes](https://docs.kentico.com/13/custom-development/creating-custom-modules.md), custom implementations of interfaces or providers, etc. The system is only able to discover such code within assemblies.
>
> Instead, add the code into a _Class Library_ project with the _AssemblyDiscoverable_ attribute and reference the assembly from your project. See [Adding custom assemblies](https://docs.kentico.com/13/custom-development/adding-custom-assemblies.md).

**WCF services**

If implementing custom WCF services within the Xperience web project, you need to initialize the application before calling the Xperience API within the service code.

Otherwise you may encounter errors if the first request that starts the application is destined for the WCF service. The standard initialization does not occur, because WCF requests are not processed by the ASP.NET HTTP runtime. For more information, see the [WCF Services and ASP.NET](https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet) article.

For example, you can execute the **CMSApplication.Init** method within the constructor of your WCF service class.

## Additional configuration and tips

### Sharing project files with the Xperience application

If you need to call API that works with a specific folder in the Xperience project, you can map the physical path used by the Xperience API in your external application to a specific Xperience project. For example, you can use this approach if you wish to externally update or rebuild the files of [locally stored search indexes](https://docs.kentico.com/13/configuring-xperience/setting-up-search-on-your-website/using-locally-stored-search-indexes.md).

Set the **SystemContext.WebApplicationPhysicalPath** property to the path of your Xperience project's **CMS** folder. Map the path _before_ you initialize the Xperience API (i.e. call _CMSApplication.Init_), for example in the **Application\_Start** method of _Global.asax,_ or in your application's **Main** method.

```csharp title="Example"

// Maps the physical path used by the Xperience API in the external application to the folder of the connected Xperience project
CMS.Base.SystemContext.WebApplicationPhysicalPath = "C:\\inetpub\\wwwroot\\Xperience\\CMS";

```

### Working with the user context

When working with the Xperience API in an external application, the default Xperience user context is not available. The standard API for getting the current user ( _**CMS.Membership.MembershipContext.AuthenticatedUser**_ ) always returns the "public" user when called externally.

To use API that relies on the context of a specific user, enclose the code into a **using** statement with a declared **CMSActionContext** instance and the appropriate _UserInfo_ object as a parameter.

```csharp title="Example"

using CMS.Base;
using CMS.Core;
using CMS.Membership;

...

// Gets an object representing a specific Xperience user
UserInfo user = UserInfo.Provider.Get("Andy");

// Sets the context of the user
using (new CMSActionContext(user))
{
    // Logs an information event into the Xperience event log, with "Andy" as the user who caused the event
    Service.Resolve<IEventLogService>().LogEvent(EventType.INFORMATION, "External Application", "Event_Code", "Details");
}

```

<!-- dev-model:core start -->

**ASP.NET Core development model.** Applies only when building with ASP.NET Core. If this page also covers MVC 5, that version is in its own block.

### Handling application settings in ASP.NET Core console applications

When calling the Xperience API outside of web applications (e.g., console applications), the system is by default configured to look for application settings in a root-level **app.config** file.

If you wish to store application settings in a different file or location, create a custom [code-only module](https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) and substitute the default implementation with a custom  _[IConfiguration](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1)_  provider during the [module's preinitilization](https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md). For example:

```csharp

using Microsoft.Extensions.Configuration;
using CMS.Core;

...

protected override void OnPreInit()
{
    // Registers a configuration provider configured to look
    // for application settings in an 'appsettings.json' file.
    // This overrides the default registration.
    Service.Use<IConfiguration>(() => new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", true, true)
        .Build());
}

```

<!-- dev-model:core end -->
