---
title: Staging
---

> 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).

List of examples:

## Staging servers

### Creating a staging server

```csharp

// Creates a new staging server object
ServerInfo newServer = new ServerInfo();

// Sets the server properties
newServer.ServerDisplayName = "New target server";
newServer.ServerName = "NewTargetServer";
newServer.ServerEnabled = true;
newServer.ServerSiteID = SiteContext.CurrentSiteID;
newServer.ServerURL = "http://localhost/Kentico/";
newServer.ServerAuthentication = ServerAuthenticationEnum.UserName;
newServer.ServerUsername = "admin";
newServer.ServerPassword = "pass";

// Saves the staging server to the database
ServerInfo.Provider.Set(newServer);

```

[> Back to list of examples](#toc)

### Updating a staging server

```csharp

// Gets the staging server
ServerInfo updateServer = ServerInfo.Provider.Get("NewTargetServer", SiteContext.CurrentSiteID);
if (updateServer != null)
{
    // Updates the server properties
    updateServer.ServerDisplayName = updateServer.ServerDisplayName.ToLowerCSafe();

    // Saves the updated server to the database
    ServerInfo.Provider.Set(updateServer);
}

```

[> Back to list of examples](#toc)

### Updating multiple staging servers

```csharp

// Gets all staging servers defined on the current site whose code name starts with 'New'
var servers = ServerInfo.Provider.Get()
                                    .WhereStartsWith("ServerName", "New")
                                    .WhereEquals("ServerSiteID", SiteContext.CurrentSiteID);

// Loops through individual servers
foreach (ServerInfo server in servers)
{
    // Updates the server properties
    server.ServerDisplayName = server.ServerDisplayName.ToUpper();

    // Saves the updated server to the database
    ServerInfo.Provider.Set(server);
}

```

[> Back to list of examples](#toc)

### Deleting a staging server

```csharp

// Gets the staging server
ServerInfo deleteServer = ServerInfo.Provider.Get("NewTargetServer", SiteContext.CurrentSiteID);

if (deleteServer != null)
{
    // Deletes the staging server
    ServerInfo.Provider.Delete(deleteServer);
}

```

[> Back to list of examples](#toc)

## Staging tasks

### Synchronizing staging tasks

```csharp

// Gets a staging server
ServerInfo server = ServerInfo.Provider.Get("NewTargetServer", SiteContext.CurrentSiteID);

if (server != null)
{
    // Gets all staging tasks that target the given server
    var tasks = StagingTaskInfoProvider.SelectTaskList(SiteContext.CurrentSiteID, server.ServerID, null, null);

    // Loops through individual staging tasks
    foreach (StagingTaskInfo task in tasks)
    {
        // Synchronizes the staging task
        string result = new StagingTaskRunner(server.ServerID).RunSynchronization(task.TaskID);

        if (string.IsNullOrEmpty(result))
        {
            // The task synchronization was successful
        }
        else 
        {
            // The task synchronization failed
            // The 'result' string returned by the RunSynchronization method contains the error message for the given task
        }
    }         
}

```

[> Back to list of examples](#toc)

### Deleting all staging tasks that target a server

```csharp

// Gets the staging server
ServerInfo server = ServerInfo.Provider.Get("NewTargetServer", SiteContext.CurrentSiteID);

if (server != null)
{
    // Gets all staging tasks that target the given server
    var tasks = StagingTaskInfoProvider.SelectTaskList(SiteContext.CurrentSiteID, server.ServerID, null, null);
    
    // Loops through individual staging tasks
    foreach (StagingTaskInfo task in tasks)
    {                       
        // Deletes the staging task
        StagingTaskInfo.Provider.Delete(task);
    }
}

```

[> Back to list of examples](#toc)

### Running code without logging staging tasks

```csharp

// Prepares an action context for running code without logging of staging tasks
using (new CMSActionContext() { LogSynchronization = false })
{
    // Creates a new role without logging any staging tasks
    RoleInfo newRole = new RoleInfo();
    newRole.RoleDisplayName = "New role";
    newRole.RoleName = "NewRole";
    newRole.SiteID = SiteContext.CurrentSiteID;
    
    RoleInfo.Provider.Set(newRole);
}

```

[> Back to list of examples](#toc)

### Logging staging tasks under specific task groups

```csharp

// Gets a "collection" of task groups (in this case one group whose code name is equal to "Group_Name")
var taskGroups = TaskGroupInfo.Provider.Get().WhereEquals("TaskGroupCodeName", "Group_Name");

// Prepares a synchronization action context
// The context ensures that any staging tasks logged by the wrapped code are included in the specified task groups
using (new SynchronizationActionContext() { TaskGroups = taskGroups })
{
    // Creates a new role object
    RoleInfo newRole = new RoleInfo();

    // Sets the role properties
    newRole.RoleDisplayName = "New role";
    newRole.RoleName = "NewRole";
    newRole.SiteID = SiteContext.CurrentSiteID;

    // Saves the role to the database
    RoleInfo.Provider.Set(newRole);
}

```

[> Back to list of examples](#toc)
