Can we update the values of "Created By", "Modified By" columns in SharePoint lists ?

Can we update the values of "Created By", "Modified By" columns in SharePoint lists ?

  • Comments 38

Ofcourse, you can. Created By & Modified By columns are "Person or Goup" type columns. In SharePoint all the lists has these columns by default. You can't update the values of these columns from UI. But, you can do it through SharePoint APIs.

I have created .NET windows based application to update the "created by" and "modified by" columns using SharePoint APIs. I am giving the sample code snippet below. 

/******** Code snippet for modifying the Created by and Modified by column values of a SharePoint List *******/

/******** Written in .Net Windows Based Application **********/

 

private void button3_Click(object sender, EventArgs e)

        {

            SPSite oSite = new SPSite("http://<SiteName>/");

            SPWeb oWeb = oSite.OpenWeb();

            SPList oList = oWeb.Lists["TestCustomList"];

            SPListItemCollection oListCollection = oList.Items;

            foreach (SPListItem oListItem in oListCollection)

            {

                SPFieldUserValue oUser = new SPFieldUserValue(oWeb, oWeb.CurrentUser.ID, oWeb.CurrentUser.LoginName);

   // or you can hard code the value like this,

 SPFieldUserValue oUser = new SPFieldUserValue(oWeb, 14, "Sowmyan Soman");

                oListItem["Author"] = oUser;//created by column value = "14;#Sowmyan Soman"

                oListItem["Editor"] = oUser;//modified by column value = "14;#Sowmyan Soman"               

                oListItem.Update();               

            }

          

            oWeb.Update();

         }

 

//alternate method

SPSite oSite = new SPSite("http://<site URL>");

SPWeb oWeb = oSite.OpenWeb();

SPList oList = oWeb.Lists["TestDocLibrary"];

SPListItem oListItem = oList.Items.GetItemById(5);

oListItem["Editor"] = oWeb.CurrentUser.ID; //"20;#Sowmyan";

oListItem.Update(); 

 

Updated : November 13 - 2008

 

The above code will not update the "created by" column of document library type SharePoint lists. The above code (both) will work just fine for all the lists and even it will update the "Modified by" column in SharePoint document libraries. One of my MS colleagues Vedant has posted a work-around to accomplish this update and you can find it out here

 

If anyone one want to know how we can do this same functionality using Powershell (codename : Monad) please see Tedd's post : http://blogs.msdn.com/tadd/archive/2008/05/22/updating-the-created-by-and-modified-by-columns-in-sharepoint-lists.aspx

 

Comments
  • Thanks much for noticing this...actually the sample code I have posted here is without testing it with the document library. I have tested it out and saw the behaviour, I am investigating it and will update you definitily once I got the root cause.

    Thanks again !

  • One of the unanticipated effects of Mark Miller&#39;s recent (and much appreciated) plugging of SharePoint

  • I want to submit the form annonymously. i:e have no value in the "created by" field.

    Is that possible?

  • If you run your site in anonymous access, then the site will be running with the account IUSR_Machinename as the current user. So, it can't identify who is actually submitting the form and thus we can't update that field.

  • Ok, I've scoured the web for an easy way to do this without writing any code.  Here's my solution:

    1) Open the file (make a copy beforehand):

    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL\XML\ONET.XML

    2) Browse to the part where it says <BaseType Title="Generic List" (in my case, I needed to change the "created by" and "modified by" attributes of a list)

    3) Scroll down a few lines to where it says FieldID and then "ColName="tp_Author" (created by) and also the linke "ColName="tp_Editor" (modified by)

    4) Remove ReadOnly="TRUE" from the line

    5) recycle the sharepoint IIS website and application pool

    6) Now open your list in a datasheet list view where you've added the 2 columns "modified by" and "created by".

    These fields should now be editable with simple cut/paste, etc..

    Hope this helps out some other frustrated sharepoint admins who are not trained developers!

    PS - don't forget to reset the attributes back to read only when you're done with the modifications...

  • Refer following blog to programmatically modify "Created By" and "Modified By" field values in SharePoint list:

    http://etechplanet.com/post/2008/12/06/How-to-change-the-values-of-Created-By-and-Modified-By-fields-in-SharePoint-list.aspx

  • Have you ever ran into when the Modified will switch to non RO, but the Created stays RO? I am using the Absense template from MS, and I cannot get the created to go NON RO, we would like to be able to change the field if we are putting in vacation for someone other that yourself.

  • to be frank I didn't get what is meant by "RO"/ "non RO" :), could you please help me to understand what is it?

  • have you found a workaround on how to get the "created by" field in document libraries updated/edited?

    is the remove read-only on created by applicable to both lists and document libraries? and what do you mean when you said

    5) recycle the sharepoint IIS website and application pool

  • linusangelo: If you don't understand that, I strongly advise against you even trying this workaround.

  • Nice,

    So you start your article saying "Of course you can" to end with a little comment "Well.. actually it doesn't work with document libraries"

    Good!

  • Thanks for your suggestion Bob :), well, actually it does work through a work around as I have mentioned, though it will not work with the sample code that I have posted here.

    I have modfied the last paragraph. Thanks for your suggestions.

    Thanks,

    Sowmyan

  • Direkter Download: SPPD-099-2008-06-06 Aktuell SharePointCommunityCamp September 2008 (15.9./16.9.2008

  • Hi Sowmyan,

    the code looks fine, but if a user doesnt have Edit access then you have to elevate the user permission , at that time code runs under system account , thou you keep current user id outside of the scope , but when the code reach to the oListItem.Update() then the final update will be done under system account , so the Author filed may be given user but modified by will be by system account . please let me know if i am wrong,

    Thanks & Regards,

    Vikash Shamra

  • If you're like me and don't have access to the servers to use the API or change the XML files directly then just use the Lists.asmx web service. No IIS reset required! Of course you have to be familiar with writing web service calls.

    1. Get the List XML definition for all columns (GetList).

    2. Pull out the Author and Editor XML for the columns.

    3. Change ReadOnly="TRUE" to ReadOnly="FALSE" on both.

    4. Submit the web service request method UpdateList with the your XML in the update fields parameter.

    5. Edit your list in DataSheet view with the Modified By and Created By columns on there. Or you can use the web service again (UpdateListItems).

    6. Do step 3 and 4 to change it back to ReadOnly="TRUE"

    Check out the Lists.asmx MSDN reference to get your calls right.

Page 2 of 3 (38 items) 123
Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post