Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

SharePoint: Changing comments of document versions in code

During one SharePoint migration project I had problem with document versions comments. I needed some way to set these in my code. As I didn't found any normal way to do it through SharePoint API I worked out something I call dirty hack. But it works.

The Idea

As API seemed more and more hopeless in my context I checked out what's going on in database and I discovered that I can set those version comments also after importing all the data. My hack affected only two tables in SharePoint database and after solving some mysteries I got everything work as expected.

Database

The first thing we will look at is SharePoint database. We are interested in two tables: AllDocs and AllDocVersions. Version fields are UIVersion in first table and Version in second table. Both tables have field called CheckinComment - this is the fields we have to change.

Make sure you don't edit SharePoint lists or items through browser when using this solution - you may overwrite the changes you made through code.

Version Format

Let's look at the versions for a moment. We don't see nice version numbers like 0.1, 1.0, 2.2 in these tables. Instead we have some integers like 1, 512, 1026. There is little trick how to compute original versions to integers. You have to use the following equation:

version integer = major version * 512 + minor version

Version Class

Next thing is class called Version. I wrote about it blog entry Using Version Class. As Version is sealed class we are not able to extend it. But we can always use extension methods to visually add new methods to classes. Now let's add new extension method for Version class that returns Sharepoint version integer from current version.


public static class VersionExtender
{
    public static Int32 GetSharePointDocVersion(this Version ver)
    {
        return ver.Major * 512 + ver.Minor;
    }
}

Changing the comments

Now we are ready to change the comments. As an example I give a method that is very compact so it easier for you to take it and try it out. Of course it is possible to optimize and refactor this method many ways. You are free to do it.

The method takes three arguments: versionString, comment, and guid. versionString is version label of document we want to change, comment - of course - is comment for list item version and guid is GUID of list item.


void SaveComment(string versionString, string comment, Guid guid)
{
    Version version = new Version(versionString);
    Int32 uiVersion = version.GetSharePointDocVersion();

 

    SqlConnection cn = new SqlConnection("SP ConnStr");
    SqlCommand cmd;
    string cmdTxt;

    cn.Open();

    cmd = cn.CreateCommand();
    cmdTxt = "update alldocs set CheckinComment=@Comment Where ";
    cmdTxt+= "UIVersion=@Version and cast(id as varchar(100))=@id";
    cmd.CommandText = cmdTxt;
    cmd.CommandType = CommandType.Text;

    cmd.Parameters.AddWithValue("@Comment", comment);
    cmd.Parameters.AddWithValue("@Version", uiVersion);
    cmd.Parameters.AddWithValue("@id", guid.ToString());

    cmd.ExecuteNonQuery();
   
    cmd = cn.CreateCommand();
    cmdTxt = "update alldocversions set CheckinComment=@Comment ";
    cmdTxt+= "Where Version=@Version and ";
    cmdTxt+= "cast(id as varchar(100))=@id";
    cmd.CommandText = cmdtxt;
    cmd.CommandType = CommandType.Text;

    cmd.Parameters.AddWithValue("@Comment", comment);
    cmd.Parameters.AddWithValue("@Version", uiVersion);
    cmd.Parameters.AddWithValue("@id", guid.ToString());

    cmd.ExecuteNonQuery();

    cmd.Dispose();
    cn.Dispose();
}


Okay, no you should be able to edit comments of document versions through code. If you know some better way how to do it, please let me know.

Comments

Links (4/6/2008) « Steve Pietrek’s SharePoint Stuff said:

Pingback from  Links (4/6/2008) « Steve Pietrek’s SharePoint Stuff

# April 6, 2008 7:42 PM

Jamie McAllister said:

1. Direct database manipulation like the example you post moves you firmly into Unsupported territory with Microsoft.

2. I've seen content like this being manipulated through direct database access myself. That same content flipped back to it's original state after about 2 weeks. It seems likely that there are other data management processes at play.

3. It's certainly straightforward to set comments in code, when you check items in and publish them. Not so sure about version numbers. Also don't know of a way to change existing comments without a checkout. Presumably that wouldn't be allowed because it breaks an audit trail for the data?

Anyway, not rocket science, but here's a code sample from publishing page manipulation;

PublishingPage.CheckOut();

PublishingPage.CheckIn("My Checkin comments");

SPFile pageFile = PublishingPage.ListItem.File;

pageFile.Publish("My Publishing Comments");

pageFile.Approve("My Approval Comments");

# April 14, 2008 9:04 AM

DigiMortal said:

We had to migrate content from SP2001 to MOSS2007. It's the hard pain in the ass because tools available are just awful. One of those tools event worked a little bit, although I had to rewrite almost 1/3 of it's code. And this was the project supported by MS. Well, I needed some solution for comments that were inserted for previous versions of documents and I found no better way than inserting them directly to database.

# April 14, 2008 12:41 PM

Gunnar Peipman's ASP.NET blog said:

Another day, another kinky problem with SharePoint. This time I struggled with custom properties of my

# February 3, 2009 3:59 PM

SharePoint: temporary solution for GetCustomProperty and SetCustomProperty errors | Web Hosting and Domains said:

Pingback from  SharePoint: temporary solution for GetCustomProperty and SetCustomProperty errors | Web Hosting and Domains

# February 4, 2009 5:33 AM

Terry said:

How can i publish a document to a major version programatically?

Pls help. KInda stuck here.

Thanks

# September 26, 2009 11:58 AM

DigiMortal said:

SPFile has CheckIn method and you can provide versioning option that tells SharePoint what kind of version to create.

# September 26, 2009 7:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)