update created by and modified by fields in sharepoint using client object model

Advertisement
In this article we can able to see, how to update the created by and modified user name using client object model.
We migrated a site from lotus notes to SharePoint for those items user name is coming as current system login user name, that to SharePoint site is in different environment, we are unable to do using PowerShell, so we done a sample client side object model to update the created by and modified by user names.

 Import Data from excel to SharePoint List using PowerShell
 How to add fields to list in sharepoint using powershell 
Copy value from one filed to another field using PowerShell - See more at: http://www.dotnetsharepoint.com/2015/07/copy-value-from-one-filed-to-another.html#sthash.QPHZ7Tjz.dpuf
 Copy value from one filed to another field using PowerShell 
Date formula in SharePoint calculated value
 Date formula in SharePoint calculated value 

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace updatecreatedby
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clicon = new ClientContext("http://dotnetsharepoint.com/sites/SharePoint");
            SP.List list =clicon.Web.Lists.GetByTitle("yourlistname");
            clicon.Load(list);
            SP.ListItem item = list.GetItemById(22); //here item ID updating a single item
            clicon .Load(item);
            item["Author"] = GetUsers(clicon, "domainname\\createdname ");
            item.Update();
            item["Editor"] = GetUsers(clicon, " domainname\\modifiedname ");
            item.Update();
            clicon.ExecuteQuery();
       
        }

        private static SP.FieldUserValue GetUsers(ClientContext clientContext, string UserName)
        {
            SP.FieldUserValue userValue = new SP.FieldUserValue();
            SP.User updateUser = clientContext.Web.EnsureUser(UserName);
            clientContext.Load(updateUser);
            clientContext.ExecuteQuery();
            userValue.LookupId = updateUser.Id;
            return userValue;
        }

    }
}


SharePoint Online Real Time Training Contact: JLAKSHMITULASI@GMAIL.COM
Advertisements
SHARE

Lakshmi Tulasi Jasti

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments:

  1. For me still its not updating 'Created By' field but its updating 'Modified By' field. Through powershell it was not updating both fields.

    ReplyDelete