locked
How to change the value of "Created By" field in sharepoint list? RRS feed

  • Question

  • Experts,

    I have a SharePoint list to store user information. Admin will populate this list means will load the data of all the users in the list. So in the 'Created By' field for each list item admin login ID is stored.

    So what I want is to modify the 'Created By' field value for each list item and set it to the respective user's login ID.

    Please help.

    Regards,
    Avaneet
    • Moved by Mike Walsh FIN Tuesday, December 2, 2008 7:19 AM wrong forum (Moved from SharePoint - Design and Customization to SharePoint - Development and Programming)
    Tuesday, December 2, 2008 5:24 AM

Answers

  •  

    SPSite site = new SPSite(siteUrl);

    SPWeb web = site.OpenWeb();

    web.AllowUnsafeUpdates = true;

    SPList list = web.Lists["Test"];

    SPListItemCollection collection = list.Items;

    foreach (SPListItem item in collection )

    {

    SPUser user = web.EnsureUser(UserName Login Name);

    //1073741823;#System Account   //User name values are in this format ID;#Login Name
    string
    value1 = user.ID + ";#" + user.Name; //Create in same format

    item["Author"] = value1; //for Created By field

    item["Editor"]=value1 ;  //Modified By field

    item.Update();  //Update the item

    }

    list.Update();

    web.Update();


    Its not necessary that same user should be logged in. No relation with which user is logged in.

    • Marked as answer by Avaneet Tuesday, December 2, 2008 1:36 PM
    Tuesday, December 2, 2008 1:16 PM

All replies

  • Not a customization question.

    Moving to programming 


    This an internal field and you can't do this using the UI, so Programming is the only possibility left.
    Tuesday, December 2, 2008 7:19 AM
  • Even throgh programming I doubt you will be able to change "Created By".
    Tuesday, December 2, 2008 9:54 AM
  •  U can change the Created By filed through code :

    foreach(SPListItem item in listColl.Items)
    {

            item[
    "Created"]=UserName;
            item.Update();
    }

    Hope this helps.
    Shobha

    • Proposed as answer by Shobha P Tuesday, December 2, 2008 11:51 AM
    • Unproposed as answer by Mike Walsh FIN Wednesday, April 22, 2009 1:17 PM
    Tuesday, December 2, 2008 11:51 AM
  • Hi Shobha,
    I already tried this code, but no luck.
    Tuesday, December 2, 2008 12:16 PM
  • It worked for me. Are u getting any error?
    Tuesday, December 2, 2008 12:19 PM
  • Hi Shobha,

    Can you please send me the type of "UserName" used in your code? Is it the logged in user OR we can assign any user?
    Tuesday, December 2, 2008 12:25 PM
  • U can assign any user(Account Name). User should be there in your web appliaction, otherwise it will give throw error : Invalid data, updating read only fields.
    Tuesday, December 2, 2008 12:33 PM
  • I was also trying the same means trying to update the the user which is not the logged in user but added in the sharepoint people group with contribute permissions.

    I am getting the same error you told "Invalid data, updating read only fields".

    Can you provide me the full code snippet?
    Tuesday, December 2, 2008 12:42 PM
  •  

    SPSite site = new SPSite(siteUrl);

    SPWeb web = site.OpenWeb();

    web.AllowUnsafeUpdates = true;

    SPList list = web.Lists["Test"];

    SPListItemCollection collection = list.Items;

    foreach (SPListItem item in collection )

    {

    SPUser user = web.EnsureUser(UserName Login Name);

    //1073741823;#System Account   //User name values are in this format ID;#Login Name
    string
    value1 = user.ID + ";#" + user.Name; //Create in same format

    item["Author"] = value1; //for Created By field

    item["Editor"]=value1 ;  //Modified By field

    item.Update();  //Update the item

    }

    list.Update();

    web.Update();


    Its not necessary that same user should be logged in. No relation with which user is logged in.

    • Marked as answer by Avaneet Tuesday, December 2, 2008 1:36 PM
    Tuesday, December 2, 2008 1:16 PM
  • Thanks a lot Shobha. 
     Its working fine.
    Tuesday, December 2, 2008 1:36 PM