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;
                }
            }

Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Change Language for current user with JSOM in SharePoint Online

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

SharePoint 2010 CSS Chart