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 achieve this, you can use the
Userobject'sLoginNameproperty, which represents the user's email address. Here's how you can modify your code to set theAuthorandEditorfields using the user's email address:- First, ensure you have the user's email address 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 emailAddress = user.LoginName;- Next, use the
emailAddressvariable to set theAuthorandEditorfields:
csharpuploadFile.ListItemAllFields["Author"] = context.Web.EnsureUser(emailAddress); uploadFile.ListItemAllFields["Editor"] = context.Web.EnsureUser(emailAddress); uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.