3

I'd to allow the user to option to change the "created by" value when created an item in a sharepoint list. It seems that it's the default to hide this value and auto-populate the current user. I'd like to give the user this option when creating or modifying an item and pre-populate with the current user, but also give the user the option to change this field..

Anyone have any suggestions or whom can point me in the right direction?

Thanks very much

Jesse Roper
  • 1,269
  • 7
  • 31
  • 56

2 Answers2

8

The internal name of 'Created By' column is Author.

Write an ItemEventReceiver and override the ItemUpdated method:

//USER_NAME is user account name that you want to set.

public override void ItemUpdated(SPItemEventProperties properties) { 
    SPSecurity.RunWithElevatedPrivileges(delegate   
    {     
        using (SPWeb web = properties.OpenWeb())     
        {
            web.AllowUnsafeUpdates = true;

            // Insert any other updates here

            SPUser spUser = web.EnsureUser("USER_NAME");
            string strUserId = spUser.ID + ";#" + spUser.Name;
            spListItem["Author"] = strUserId;
            spListItem.Update();

            // if you do not want to change the Modified or Modified By fields,
            // use spListItem.SystemUpdate() instead

        }
    });
}

EDIT: updated code; removed iterative update.

ukhardy
  • 2,084
  • 1
  • 13
  • 12
  • 2
    spList.Update and web.Update are redundant here, you do not need them. Also not sure why you are updating all the items when running under a single item's event, does not make much sense. In any case, I would think twice before using such code, as it makes the information stored in SharePoint less reliable, as you don't know who actually created the item. – Vladi Gubler Mar 22 '11 at 15:06
  • +1. @Vladi Gubler for raising the reliability point as well. Brilliant indeed. I revised the the blind update. My personal preference would also be not to update the sharepoint internal coumns. I will choose custom columns instead; however the OP is as such. – ukhardy Mar 22 '11 at 19:45
  • There is no need to create the `strUserId` variable. You can set user fields directly with SPUser objects. So you could just use `spListItem["Author"] = spUser;` – theChrisKent Apr 04 '11 at 20:41
-1

Thanks, guys but I should've mentioned I'm not using any code for this.

My solution was to rename the Created By field to Created By (unused), created a new Created By field and used some custom Javascript to populate the field on page load.

Jesse Roper
  • 1,269
  • 7
  • 31
  • 56
  • 1
    This is a work around and not a solution (you are not actually updating the created by field). @ukhardy provides an example solution that answers your original question and should be marked as the answer. – theChrisKent Apr 04 '11 at 20:44
  • Updating field with javascript will only work if users are creating items with web browser. In this case any 3rd party app using webservices or object model won't do it. – Janis Veinbergs Sep 24 '12 at 14:34