Showing posts with label Change Created By value. Show all posts
Showing posts with label Change Created By value. Show all posts

Thursday, December 20, 2012

How to change value of Created By field or Modified By field?


You can change Created By column value or Modified By column value. To do this use below given code :

            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("MyList");
                    SPListItem item = list.GetItemById(1);
                    web.AllowUnsafeUpdates = true;

                    //You can use user id (integer) 
                    //item[SPBuiltInFieldId.Author] = SPContext.Current.Web.CurrentUser.ID;

                    //You can use SPUser object also.
                    item[SPBuiltInFieldId.Author] = SPContext.Current.Web.CurrentUser;

                    item.Update();

                    web.AllowUnsafeUpdates = false;
                }
            }