Staging


List of examples:

Staging servers

Creating a staging server



// 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

Updating a staging server



// 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

Updating multiple staging servers



// 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

Deleting a staging server



// 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

Staging tasks

Synchronizing staging tasks



// 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

Deleting all staging tasks that target a server



// 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

Running code without logging staging tasks



// 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

Logging staging tasks under specific task groups



// 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