Thursday, May 7, 2009

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

SPSite mySite = new SPSite(""); SPWeb myWeb = mySite.OpenWeb(); SPFolder myList = myWeb.Folders[""]; SPFile myFile = myList.Files[0]; SPUser myUser = myWeb.Users[""]; 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.

No comments:

Post a Comment