CSOM - can't update the listItem["Editor"] (Modified by) in .NET Core

natacat 1 Reputation point
2020-08-27T05:35:26.117+00:00

The issue is related to .NET Core with CSOM.
In .NET Core the authentication for SharePoint Online changed and it’s no longer supporting the “SharePointOnlineCredentials” class.
I followed the official documentation successfully : https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard
This code runs in Azure function V3, and the API is triggered by a user button click in the SharePointOnline UI. This API creates a new SP item.

However, in the previous versions I could update the sharepoint item “Modified by” system field easily with a new User object.
In this version since the authentication changed, each ExceuteQuery action, the access token for the admin user is requested, and the “Modified by” field is the admin and not the User as I defined in my code.

I wish the current user will be defined as the item creator/editor. and not the user defined for the access token.

Please assist how can I perfom this action.

Thanks,

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,250 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Jerryzy 10,566 Reputation points
    2020-08-28T04:10:08.11+00:00

    Hi @natacat ,

    I can reproduce the same issue in .NET Core Solution.

    As the authencation way changed, this could affect to update Editor field to other values, seems the new CSOM libraray have some comptible issue with .NET Core solution.

    You could submit a UserVoice in SharePoint Online Dev Platform about this issue:

    SharePoint Dev Platform

    And if possible, I suggest you could switch to .NET Framework instead of .NET Core to update author/editor field value.


    If the response is helpful, please click "Accept Answer" and upvote it :)

    1 person found this answer helpful.

  2. sadomovalex 3,626 Reputation points
    2020-08-27T15:15:33.237+00:00

    hello,
    one option is to pass current user's login name from Sharepoint Online to button click handler and then to Azure function. This is how login name of current user can be retrieved in javascript:

    var ctx = SP.ClientContext.get_current();
    var user = web.get_currentUser();
    ctx.load(user);
    ctx.executeQueryAsync(
    Function.createDelegate(this, function(sender, args) {
        // pass user.get_LoginName() to AF
    }),
    Function.createDelegate(this, function(sender, args) {
        // handle error
    })
    );
    

    and then inside Azure function update Modified By field like that:

    var ctx = ...
    var user = ctx.Web.EnsureUser(loginName);
    ctx.Load(user);
    ctx.ExecuteQuery();
    
    var item = ...
    item["Editor"] = user;
    item.Update();
    ctx.ExecuteQuery();
    

  3. Michaël Maillot 1 Reputation point
    2021-01-21T13:04:36.367+00:00

    Hi,

    Does your problem still persist?

    I'm also using an Azure Function v3 and I can update the Editor field, through an AAD Application, in AppOnly Context. But you have to know that you must consent the highest SharePoint Permissions ("Sites.FullControl.All") to make that work.