How can I show a custom name instead of the client id when creating and editing files in…
- User
How can I show a custom name instead of the client id when creating and editing files in SharePoint via an app using the code below?
uploadFile.ListItemAllFields["Author"] = context.Web.EnsureUser(clientId); uploadFile.ListItemAllFields["Editor"] = context.Web.EnsureUser(clientId); uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();
- Assistant
To show a custom name instead of the client ID when creating and editing files in SharePoint via your app, you can use the
Userobject'sTitleproperty, which represents the user's display name. Here's how you can modify your code to achieve this:- First, ensure you have the user's display name available. You can get it by querying the user information list or by using the Microsoft Graph API. Here's an example using the user information list:
csharpvar user = context.Web.SiteUsers.GetByLoginName(clientId); context.Load(user); context.ExecuteQuery(); string displayName = user.Title;- Next, use the
displayNamevariable to set theAuthorand fields:
- User
How can I keep the APP as the uploaded file Author/creator but show the activity log as modified by another user's email instead?
- Assistant
To keep the app as the uploaded file's author/creator but show the activity log as modified by another user's email, you can use the
EnsureUsermethod to ensure the other user exists in the SharePoint site collection and then set theEditorproperty to that user. Here's how you can modify your code:- Ensure the other user exists in the SharePoint site collection:
csharpvar otherUser = context.Web.EnsureUser("otheruser@example.com"); context.Load(otherUser); context.ExecuteQuery();- Set the
Editorproperty to the other user:
csharpuploadFile.ListItemAllFields["Editor"] = otherUser; uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();This way, the app will remain as the author/creator of the uploaded file, but the activity log will show the other user as the last modifier.
Please note that you need to have the appropriate permissions to modify the
Editorproperty. If you encounter any issues, make sure your app has the necessary permissions to perform this action.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.