Skip to main content

How to change Checkin comment after save

If your company has very strict requirements on version numbering and want the comments to each of these versions to be correct, then you may run into a problem if you store the documents in SharePoint.

The version comment is determined when you check in your document, and after the checkin there is no supported way to change these comments.

If you really need to change the comments of a document and are willing to go the unsupported way of changing the content database directly then this little program can do the work for you:

using System;
using System.Data.SqlClient;
using Microsoft.SharePoint;
namespace VersionCommentFaker
{
    class Program
    {
        static int Main(string[] args)
        {
            if (args.Length != 3)
                return Usage();
            using (SPSite site = new SPSite(args[0]))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPFile file=web.GetFile(args[0]);
 
                    if (file.UIVersionLabel == args[1])
                        return ChangeCurrentComment(site, file.UniqueId, file.UIVersion, args[2]);
 
                    foreach (SPFileVersion fileVersion in file.Versions)
                    {
                        if (fileVersion.VersionLabel == args[1])
                            return ChangeComment(site, file.UniqueId, fileVersion.ID, args[2]);
                    }
                    Console.WriteLine("Version not found");
                }
            }
            return 1;
        }
 
        private static int ChangeCurrentComment(SPSite site, Guid FileId, int version, string newComment)
        {
            using (SqlConnection conn = new SqlConnection(site.ContentDatabase.DatabaseConnectionString))
            {
                SqlCommand cmd = new SqlCommand(
                    "UPDATE dbo.AllDocs SET CheckinComment=@comment "+
                    "WHERE SiteId=@SiteId AND Id=@FileId AND UIVersion=@VersionId", conn);
                cmd.Parameters.AddWithValue("@comment", newComment);
                cmd.Parameters.AddWithValue("@SiteId", site.ID.ToString("D"));
                cmd.Parameters.AddWithValue("@FileId", FileId.ToString("D"));
                cmd.Parameters.AddWithValue("@VersionId", version);
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                conn.Close();
                if (rows == 1)
                    Console.WriteLine("CheckinComment was updated");
            }
            return 0;
        }
        private static int ChangeComment(SPSite site, Guid FileId, int version, string newComment)
        {
            using (SqlConnection conn = new SqlConnection(site.ContentDatabase.DatabaseConnectionString))
            {
                SqlCommand cmd = new SqlCommand(
                    "UPDATE dbo.AllDocVersions SET CheckinComment=@comment "+
                    "WHERE SiteId=@SiteId AND Id=@FileId AND Version=@VersionId", conn);
                cmd.Parameters.AddWithValue("@comment", newComment);
                cmd.Parameters.AddWithValue("@SiteId", site.ID.ToString("D"));
                cmd.Parameters.AddWithValue("@FileId", FileId.ToString("D"));
                cmd.Parameters.AddWithValue("@VersionId", version);
                conn.Open();
                int rows=cmd.ExecuteNonQuery();
                conn.Close();
                if (rows==1)
                    Console.WriteLine("CheckinComment was updated");
            }
            return 0;
        }
        private static int Usage()
        {
            Console.WriteLine("This program can be used to change the checkin comment of an old version of a file");
            Console.WriteLine(" {0} fileurl version new_comment", Environment.GetCommandLineArgs()[0]);
            return 1;
        }
    }
}

VersionCommentFaker.exe

Copyright © 2007-2022