Nick's profileSharePoint DevelopmentBlogListsNetwork Tools Help

Blog


    August 31

    Change Document Library Created By and Modified By fields

    After a solid week of banging my head off my desk, I have finally mashed up a way to programmatically assign the Created By and Modified By fields in a SharePoint Document Library.  In my previous post, I mentioned moving a file from one Document Library to a newly created Document Library in a separate Site Collection.  That is all well and good.  Inserting the document worked perfectly, except for the fact that Created By and Modified By were both ‘System Account’.  This was not a proper solution.  The correct result would be the originator of the workflow’s LoginName being assigned to those columns.

    In the end, I realized that simply using RootFolder.Files.Add() meant that the Created By and Modified By fields were Read-Only.  After some more forum trolling I was able to mash up a method that took SPUser (the originator) of the workflow and pop their LoginName into both of those fields.  I ended up having to use the SPFileCollection object and after obtaining the newly created file, I had to edit the Item of that File.  The method is shown below:

    image

    Using objFolder.Files.Add(strFiletoUpload, contents, userCreatedBy, userModifiedBy, timeCreated, timeModified) would throw an error due to Created By and Modified By being Read-Only at the time of insert.  Modifying the item of the file after the insert proved to be the right solution.  Don’t forget to update the item!

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://customsharepoint.spaces.live.com/blog/cns!41D760DBB8E4E638!188.trak
    Weblogs that reference this entry
    • None