1

I want to Update the Author field of Folder property/item property. I have seen many posts online, I tried different link but I didn't get the Success. I followed This Link. I didn't get any error while Updating the item. But the values of Author field has not been updated in Document Library?

FYI - I have created a Console Application and working with SharePoint 2016. I don't have option to use the Server side object model. I want to Update it with the CSOM only.

The same way I can update the Editor field.

3 Answers 3

1

Try change:

  • item.Update(); //Apply the change to item.SystemUpdate(); //Apply the change

or try this code (tested and a little edited):

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                UpdateListItem();
            }

        }
        public static void UpdateListItem()
        {
            var siteURL = "https://sharepoint16.dev.cz/tweb";
            var listName = "Set";
            var listItemId = 1;
            var createdBy = "[email protected]";
            var modifiedBy = "[email protected]";

            ClientContext context = new ClientContext(siteURL);
            List list = context.Web.Lists.GetByTitle(listName);
            ListItem item = list.GetItemById(listItemId);
            FieldUserValue author = GetUsers(context, createdBy);
            FieldUserValue editor = GetUsers(context, modifiedBy);
            item["Author"] = author;
            item["Editor"] = editor;
            item.Update();
            context.ExecuteQuery();
        }

        //get user by username
        public static FieldUserValue GetUsers(ClientContext clientContext, string UserName)

        {
            FieldUserValue userValue = new FieldUserValue();
            User newUser = clientContext.Web.EnsureUser(UserName);
            clientContext.Load(newUser);
            clientContext.ExecuteQuery();
            userValue.LookupId = newUser.Id;
            return userValue;
        }
    }
}

Before: enter image description here

After: enter image description here

If its not working for you, make sure you have appropriate permissions (Site Collection Administrator or Site Admin) and you are set createdBy and modifiedBy variables with correct login name.

2
  • No. this is also not working for Author field. And SystemUpdate() is not working, because I'm working in SharePoint 2016. Feb 1, 2019 at 3:24
  • I edited the answer, test the code, it's working for me, so make sure you have appropriate permissions and set correct login name. Feb 1, 2019 at 8:11
0

I got it working by Update Author and Editor both the fields together. Firstly I was just updating the Author field that was not working.

So to update the Author field, you must need to Update the Editor field as well, otherwise it will not work.

0

For those who are still struggling with this, here's how I finally got it to work on a Document Set item's document :

User user = context.Client.Web.EnsureUser(userLogin);
context.Client.Load(user);
context.Client.ExecuteQuery();
DateTime creationDate = DateTime.Now;
listItem[SPSettings.CREATED] = creationDate;
listItem[SPSettings.MODIFIED] = creationDate;
listItem[SPSettings.AUTHOR] = user;
listItem[SPSettings.EDITOR] = user;
ListItemFormUpdateValue createdByField = new ListItemFormUpdateValue
{
    FieldName = "Created_x0020_By",
    FieldValue = user?.LoginName
};
ListItemFormUpdateValue modifiedByField = new ListItemFormUpdateValue
{
    FieldName = "Modified_x0020_By",
    FieldValue = user?.LoginName
};
var updatedValues = new List<ListItemFormUpdateValue> { modifiedByField, createdByField };
listItem.ValidateUpdateListItem(updatedValues, true, "Ignored on explicit checkin/checkout");
context.Client.ExecuteQuery();

I used help from the following blog post, although I didn't do exactly the same: https://bugfree.dk/blog/2015/02/03/how-to-set-document-library-item-created-by-and-modified-by-fields-using-sharepoint-csom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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