Modify CreateBy,UpdateBy SharePoint Lists & Document Library

on Sunday, September 27, 2009

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


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, "Milap Shah");

                oListItem["Author"] = oUser;//created by column value = "19;#domain\mishah"

                oListItem["Editor"] = oUser;//modified by column value = "19;#domain\mishah"               

                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; //"19;#domain\mishah";


oListItem.Update(); 

 

 

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.


To Modify CreateBy and UpdateBy fields of Document Library you could use following code base.

site = new SPSite("http://amishah:1982/");
//connectedUser = WindowsIdentity.GetCurrent();
Console.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
web = site.OpenWeb();
SPList list = web.Lists["Source"];
SPFieldUserValue oUser = new SPFieldUserValue(web,19,"domain\mishah");19;#domain\mishah
SPListItem newItem = list.RootFolder.Files.Add("a1" + DateTime.Now.ToString("MMddyyyyHHmmss"), new byte[] { 1 },oUser.User,oUser.User,DateTime.Now,DateTime.Now).Item;



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

 


0 comments: