The Black Knight Sings

Songs about SharePoint and other adventures by Per Jakobsen


Date: # Wednesday, June 25, 2008

Title: 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 (7.5 KB)



Wednesday, June 25, 2008 1:11:53 PM (Romance Standard Time, UTC+01:00)  #    Comments [0]

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. And all information or programs are without warranty. Use at your own risk


© Copyright 2013 Send mail to the author(s) Per Jakobsen Feed your aggregator (RSS 2.0)