question

natacat-5459 avatar image
0 Votes"
natacat-5459 asked MichaelMaillot commented

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

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://docs.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,


office-sharepoint-onlineazure-functions
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@natacat-5459 Thank you for the question. I am currently looking into it and share updates at the earliest.

0 Votes 0 ·
sadomovalex avatar image
0 Votes"
sadomovalex answered natacat-5459 commented

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();



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Unfortunately line 7 item["Editor"] = user; isn't working.
In the ctx.ExecuteQuery(); it goes to the authentication class for access token, and updates the Editor with the admin user.

0 Votes 0 ·
Jerryzy avatar image
1 Vote"
Jerryzy answered JayaC-MSFT commented

Hi @natacat-5459,

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 :)


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I must use the .NET Core since this code runs in Azure Function V3. Azure Function V3 is in .NET Core.

0 Votes 0 ·

Hi @natcat-5459,

Unfortunately, currently, .NET Core is not compatible to update author/editor.

You can create a Azure Function with 1.x version so that you can use normal .NET Framework and use .NET Framework CSOM :)

0 Votes 0 ·

Hi @natacat-5459 , Did you get a chance to check with .Net framework?

If this answer has helped you ,please 'Accept as answer' and ‘Up-vote’ so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·

@natacat-5459 Please let us know if the answered provided helped.

0 Votes 0 ·
MichaelMaillot avatar image
0 Votes"
MichaelMaillot answered MichaelMaillot commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The issue still occurs.
I wish to set the current user and not an admin user from the JWT token.

0 Votes 0 ·

If the Azure function has the App Service Authentication enabled, you could get the current issuer (eg. the user that has triggered the button), ensure it and set it as the editor of the list item, no?

0 Votes 0 ·