SharePoint Internals – Hristo Pavlov’s Blog

17 June, 2008

Preserving the last “Modified By” when checking in a file

Filed under: SharePoint — Tags: , , , — hristopavlov @ 4:38 am

I’ve heard people saying that you cannot check in a file specifying explicitely who the “Modified By” user should be. Actually this is not true and you can do this easily through the object model using reflection and the code below:

public static void CheckInFileByUser(

    SPFile file,

    string checkinComment,

    SPCheckinType checkinType,

    SPUser modifiedByUser)


    MethodInfo mi = typeof(SPFile).GetMethod(


        BindingFlags.Instance | BindingFlags.NonPublic,


        new Type[] { typeof(string), typeof(SPCheckinType), typeof(bool), typeof(SPUser) },







            new object[] { checkinComment, checkinType, false, modifiedByUser }



    catch (TargetInvocationException invokeEx)


        throw invokeEx.InnerException;



Before calling this code you should check-out the file. If you don’t do so it will rethrow the “The file …. is not checked out.” SPException which will be thrown by SharePoint. The code will also bring all other SharePoint exceptions to you. And of course if you want to preserve the last “Modified By” user when updating your file using this function, just pass the file.ModifiedBy as the user argument retrieved before the file was checked out.



  1. […] Preserving the last “Modified By” when checking in a file […]

    Pingback by Links (6/17/2008) « Steve Pietrek - Everything SharePoint — 18 June, 2008 @ 1:35 am

  2. Hello Hristo,

    Excellent code! I’ve been looking for a way to preserve the ModifiedBy field for ages, since it kept changing into the user running the code all the time. I had almost given up… but this code works fine! 🙂

    It’s of very good use if you have to manually copy or move a file between sites while maintaining its version history and metadata.

    Thank you very much for this post!


    Comment by @ Zele — 26 June, 2008 @ 8:00 am

  3. Ah, quite fascinating, that reflection magic. It always seems when poking with Reflector that there are all sorts of things that might be easier if you could just call some of SharePoint’s private methods. Anyway, this is good stuff and fixed up a point of contention very nicely. Thanks!

    Comment by Chris — 21 July, 2008 @ 9:38 pm

  4. Hey Hristo!

    Fantastic! Very nice Snippet! Thank you! You are my savior! 🙂


    Comment by Philip — 29 October, 2008 @ 9:57 am

  5. Very nice. An excellent example of .Net Reflection + creative thinking.

    Comment by Brad Smith — 30 June, 2009 @ 12:07 am

  6. Hai,

    Superb. This post helped me alot and I came to know the power of reflection.


    Comment by Rama — 2 February, 2010 @ 11:57 am

  7. Hi,
    I know this is an old post. But it helped me a lot, so far, so thanks for that!
    I’m still having a problem. I want to preserve the file.Item[“Editor”] field when I call file.Publish(). I can set the field programatically and then call file.Item.Update() but that creates a new draft version. Calling file.Publish() will reset the field to the calling user.
    Any ideas?
    Thanks in advance,

    Comment by Matthijs — 26 August, 2010 @ 3:43 pm

  8. @Matthijs:

    There is a cheesy way. You can declare a variable and populate it with original editor. Now you can publish it under this user instead of calling user.

    It’s a thought not proven. I am not sure you can even pass additional variables when publish.

    Comment by Moe — 6 April, 2011 @ 4:33 pm

  9. This is TRULY Awesome!!! I was stuck in code where no matter what I did (item.SystemUpdate() or item.UpdateOverwriteVersion() – which worked before turning “Require CheckOut” ON) the Modified by user stayed as System Account. Your code worked flawlessly to help update the Modified By User correctly!!! Nice.

    Comment by Mayur — 6 July, 2012 @ 7:23 pm

  10. Looks very interesting, can you provide more information, as how cna i use same or similar code snippet to acheive to change the ModifyBy user and Modified date of all version of the same document file.

    Comment by Prashant Chaudhari — 4 December, 2012 @ 6:00 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: