Object versioning and recycle bin


List of examples:

Versioning

Creating a new version of an object



// Gets an object (Email widget in this case)
EmailWidgetInfo emailWidget = EmailWidgetInfo.Provider.Get("Image", 1);
if (emailWidget != null)
{
    // Checks if object versioning is allowed for widget objects on the current site
    if (ObjectVersionManager.AllowObjectVersioning(emailWidget))
    {
        // Sets the properties for the widget version
        emailWidget.EmailWidgetDisplayName = emailWidget.EmailWidgetDisplayName.ToLowerCSafe();

        // Adds the version to the history of the widget (does not affect the current version)
        ObjectVersionManager.CreateVersion(emailWidget, CMSActionContext.CurrentUser.UserID, true);
    }
}

> Back to list of examples

Rolling an object back to a previous version



// Gets an object (Email widget in this case)
EmailWidgetInfo emailWidget = EmailWidgetInfo.Provider.Get("Image", 1);
if (emailWidget != null)
{
    // Gets version "1.1" of the given widget
    ObjectVersionHistoryInfo version = ObjectVersionHistoryInfo.Provider.Get()
                                                                            .WhereEquals("VersionObjectID", emailWidget.EmailWidgetID)
                                                                            .WhereEquals("VersionObjectType", emailWidget.TypeInfo.ObjectType)
                                                                            .WhereEquals("VersionNumber", "1.1")
                                                                            .TopN(1)
                                                                            .FirstOrDefault();

    if (version != null)
    {
        // Rolls the widget back to version 1.1
        ObjectVersionManager.RollbackVersion(version.VersionID);
    }
}

> Back to list of examples

Permanently deleting an object version



// Gets an object (Email widget in this case)
EmailWidgetInfo emailWidget = EmailWidgetInfo.Provider.Get("Image", 1);
if (emailWidget != null)
{
    // Gets the latest version of the object
    ObjectVersionHistoryInfo version = ObjectVersionManager.GetLatestVersion(emailWidget.TypeInfo.ObjectType, emailWidget.EmailWidgetID);

    if (version != null)
    {
        // Permanently deletes the latest version of the object
        ObjectVersionManager.DestroyObjectVersion(version.VersionID);
    }
}

> Back to list of examples

Clearing the version history of an object



// Gets an object (Email widget in this case)
EmailWidgetInfo emailWidget = EmailWidgetInfo.Provider.Get("Image", 1);
if (emailWidget != null)
{
    // Clears the version history for the specified object
    ObjectVersionManager.DestroyObjectHistory(emailWidget.TypeInfo.ObjectType, emailWidget.EmailWidgetID);
}

> Back to list of examples

Recycle bin

Deleting an object to the recycle bin



// Gets the object (Email widget in this case)
EmailWidgetInfo deleteWidget = EmailWidgetInfo.Provider.Get("Image", 1);

if (deleteWidget != null)
{
    // Checks if the recycle bin is enabled for objects of the given type on the current site
    if (ObjectVersionManager.AllowObjectRestore(deleteWidget))
    {
        // Deletes the object (to the recycle bin)
        EmailWidgetInfo.Provider.Delete(deleteWidget);
    }
}

> Back to list of examples

Permanently deleting (destroying) an object



// Gets the object (Email widget in this case)
EmailWidgetInfo deleteWidget = EmailWidgetInfo.Provider.Get("Image", 1);
if (deleteWidget != null)
{
    // Prepares an action context for permanently deleting objects
    using (CMSActionContext context = new CMSActionContext())
    {
        // Disables creation of object versions for all code wrapped within the context (including recycle bin versions)
        context.CreateVersion = false;

        // Permanently deletes (destroys) the object, without creating any records in the recycle bin
        EmailWidgetInfo.Provider.Delete(deleteWidget);
    }
}

> Back to list of examples

Restoring objects from the recycle bin



// Gets all Email widgets from the recycle bin that were deleted by the current user
var deletedWidgetVersions = ObjectVersionHistoryInfo.Provider.Get()
                                        .WhereEquals("VersionObjectType", EmailWidgetInfo.OBJECT_TYPE)
                                        .WhereEquals("VersionDeletedByUserID", MembershipContext.AuthenticatedUser.UserID)
                                        .OrderBy("VersionDeletedWhen DESC");

// Loops through individual deleted widget objects
foreach (ObjectVersionHistoryInfo widgetVersion in deletedWidgetVersions)
{
    // Restores the object from the recycle bin, including any child objects
    ObjectVersionManager.RestoreObject(widgetVersion.VersionID, true);
}

> Back to list of examples