How to Retain/Preserve "Modified By" while checking in a file

SPSite

mySite = new SPSite("<SiteURL>");

SPWeb myWeb = mySite.OpenWeb();

SPFolder myList = myWeb.Folders["<DocLibName>"];

SPFile myFile = myList.Files[0];

SPUser

myUser = myWeb.Users["<User_Name>"];

CheckInFileByUser(myFile,

"Checkin Comments", SPCheckinType.MajorCheckIn, myUser);

public static void CheckInFileByUser(SPFile file, string checkinComment,SPCheckinType checkinType,SPUser modifiedByUser)

{

MethodInfo mi = typeof(SPFile).GetMethod("CheckIn",BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(string), typeof(SPCheckinType), typeof(bool), typeof(SPUser) }, null);

try

{

mi.Invoke( file,

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

 

}

catch (Exception ex)

{

MessageBox.Show(ex.StackTrace);

}

}

Before calling this code you will need to Check out the file, otherwise you will get an error.

Comments