3

How can I update the field Created By(Author) programmatically in SharePoint Online.

  • 1
    You can not update any read-only field! – Atish Dipongkor - MVP Dec 30 '15 at 7:01
  • I agree with Atish, you can not change read-only fields... Its just that Created (date) Author (Created by) are not read-only. You can even change the Modified fields... but that makes no sense as SharePoints updates them after you made a change. – Danny '365CSI' Engelman Dec 30 '15 at 11:08
2

Below is the sample code that explains how to change the Created By field value using Client Object Model for SharePoint Online.

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

    }
}
  • Excellent code! I found this when already despaired with other advises. I can confirm that it really works. All possible credits to @Hardik !!! – Boris Zinchenko Jan 20 '18 at 14:08
0

Running backend code on SharePoint Online is a bit cumbersome.

On a ListView Page This code assigns all Items to the current user

Run it in F12 developer tools or as CEWP/SEWP or CSR file or anywhere you want to run JavaScript:

function setAuthor(ID, authorID) {
    var clientContext = new SP.ClientContext.get_current(),
        list = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList()),
        item = list.getItemById(ID);
    clientContext.load(item);
    item.set_item('Author', authorID);
    item.update();
    clientContext.executeQueryAsync(
        function () {
            AJAXRefreshView({currentCtx: ctx,csrAjaxRefresh: true}, 1);
        },
        function (s, a) {
            console.error(a.get_message());
        }
    );
}
ctx.ListData.Row.forEach(function (row) {
    console.log('ItemID:', row.ID, 'was Author', row.Author[0]);
    setAuthor( row.ID , _spPageContextInfo.userId );
});

Note

  • users have different IDs in different Site Collections

You can get the users in the current Web with:

function getWebUsers() {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    var siteUsers = web.get_siteUsers();
    var users=[];
    clientContext.load(siteUsers);
    clientContext.executeQueryAsync(
        function() {
            userEnum=siteUsers.getEnumerator();
            while(userEnum.moveNext()){
               var user=userEnum.get_current();
               var userObject={
                   id:user.get_id()
                    ,email:user.get_email()
                    ,title:user.get_title()
                    ,loginName:user.get_loginName()
                    ,isSiteAdmin:user.get_isSiteAdmin()
                    ,isHiddenInUI:user.get_isHiddenInUI()
               };
               users.push(userObject);
               console.log(userObject);
            };
        }
        ,function(e,a){console.error(a)}
       );
}
0

Please explore this answer for a weird hack necessary to update Author info in addition to valid code provided here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.