IVersionableItem.PutUnderVersionControl Method 

IT Hit WebDAV for .NET

Puts or removes current item from version control.

[Visual Basic]
Function PutUnderVersionControl( _
   ByVal enable As Boolean _
) As WebDAVResponse
[C#]
WebDAVResponse PutUnderVersionControl(
   bool enable
);

Return Value

Remarks

By default items in the repository are not under version control. When item is being put under version control engine calls PutUnderVersionControl method passing true as a parameter. In your PutUnderVersionControl implementation you must create a new version. The content and properties of the new version must be copied from this item. After the call to PutUnderVersionControl VersionHistory property must point to the object implementing IHistory interface that will contain single version. The CheckedOut property must return false;

If item is under version control it mast always have at last one version in its versions list.

If AutoPutUnderVersionControl is true and item is not under version control prior to any item content or properties update PutUnderVersionControl will be called.

When item is being removed from version control engine calls PutUnderVersionControl method passing false as a parameter. In your implementation you will usually delete all versions. VersionHistory property must return null after this call.

Example

public WebDAVResponse PutUnderVersionControl(bool enable)
{
    if (enable && this.VersionHistory == null)
    {
        // Create new version. The content and properties of the new version is being copied from this item.
        string autoVerMode = ConfigurationManager.AppSettings["AutoVersionMode"];
        this.AutoVersion = (AutoVersion)Enum.Parse(typeof(AutoVersion), autoVerMode);

        using (SqlConnection conn = new SqlConnection(fConnStr))
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText =
                "SET @Identity = NEWID()"
                + " INSERT INTO Version"
                + " (ItemId, VersionId, VersionNumber, Name, Comment, CreatorDisplayName, ContentType, Created)"
                + " SELECT @ItemId, @Identity, 1, Name, Comment, @CreatorDisplayName, ContentType, GETUTCDATE()"
                + " FROM Item"
                + " WHERE ItemId = @ItemId";

            cmd.Parameters.Add("@ItemId", SqlDbType.UniqueIdentifier).Value = this.fItemId;
            cmd.Parameters.Add("@CreatorDisplayName", SqlDbType.NVarChar).Value = CurrentUserName;
            SqlParameter idParam = cmd.Parameters.Add("@Identity", SqlDbType.UniqueIdentifier);
            idParam.Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();

            Guid newID = (Guid)idParam.Value;

            cmd.CommandText =
                "INSERT INTO VersionProperty"
                + " (VersionId, Name, Namespace, PropVal)"
                + " SELECT @VersionId, Name, Namespace, PropVal"
                + " FROM Property"
                + " WHERE ItemID = @ItemID";

            cmd.Parameters.Clear();
            cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = newID;
            cmd.Parameters.Add("@ItemID", SqlDbType.UniqueIdentifier).Value = this.fItemId;
            cmd.ExecuteNonQuery();

            this.SetResourceCheckedOut(false);

            File.Copy(this.fFilePath, this.fItemDir + "\\1");
        }
    }
    else if (!enable)
    {
        // Delete all versions
        using (SqlConnection conn = new SqlConnection(fConnStr))
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "DELETE FROM Version WHERE ItemId = @ItemId";
            cmd.Parameters.Add("@ItemId", SqlDbType.UniqueIdentifier).Value = this.fItemId;
            cmd.ExecuteNonQuery();
        }
        string[] files = Directory.GetFiles(this.fItemDir);
        foreach (string fileName in files)
            if (!fileName.EndsWith("current"))
                File.Delete(fileName);

        return new NoContentResponse();
    }
    return new OkResponse();
}
 

See Also

IVersionableItem Interface | ITHit.WebDAV.Server.DeltaV Namespace