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 an app, you can use the following code:
csharp// Get the user's display name from the client id string displayName = context.Web.EnsureUser(clientId).User.DisplayName; // Set the custom name for the author and editor fields uploadFile.ListItemAllFields["Author"] = displayName; uploadFile.ListItemAllFields["Editor"] = displayName; // Update the list item and execute the query uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();In this code, we first retrieve the user's display name using the
EnsureUsermethod and accessing theUser.DisplayNameproperty. Then, we assign the display name to the "Author" and "Editor" fields of the list item. Finally, we update the list item and execute the query to save the changes.By using the user's display name instead of the client id, the custom name will be displayed when creating and editing files in SharePoint via the app.
- 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 author/creator but show the activity log as modified by another user's email, you can use the following code:
csharp// Get the user's email from the client id string userEmail = context.Web.EnsureUser(clientId).User.Email; // Set the app as the author/creator uploadFile.ListItemAllFields["Author"] = context.Web.EnsureUser(clientId).User.DisplayName; // Set the user's email as the modifier uploadFilesListItemAllFields["Editor"] = userEmail; // Update the list item and execute the query uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();In this code, we first retrieve the user's email using the
EnsureUsermethod and accessing theUser.Emailproperty. Then, we set the app as the author/creator by assigning the user's display name to the "Author" field. Next, we set the user's email as the modifier by assigning it to the "Editor" field. Finally, we update the list item and execute the query to save the changes.By using this approach, the app will be recognized as the uploaded file author/creator, while the activity log will show the modification made by the user's email.
- User
How can I stop it from changing the modified/editor name of all previous uploaded files also?
- Assistant
To prevent the modified/editor name from changing for all previous uploaded files, you can modify the code as follows:
csharp// Get the user's email from the client id string userEmail = context.Web.EnsureUser(clientId).User.Email; // Check if the file already exists if (uploadFile.ListItemAllFields.Exists) { // If the file exists, set the user's email as the modifier uploadFile.ListItemAllFields["Editor"] = userEmail; } else { // If the file doesn't exist, set the app as the author/creator uploadFile.ListItemAllFields["Author"] = context.Web.EnsureUser(clientId).User.DisplayName; } // Update the list item and execute the query uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();In this modified code, we first check if the file already exists using the property of the object. If the file exists, we set the user's email as the modifier by assigning it to the "Editor" field. If the file doesn't exist, we set the app as the author/creator by assigning the user's display name to the "Author" field.
- User
SystemUpdate wont accept true or false
- Assistant
You're right, the
SystemUpdateproperty is read-only and cannot be set totrueorfalse. To prevent the modified/editor name from changing for all previous uploaded files, you can use theSystemUpdateproperty to check if the file has been previously modified, and only update the "Editor" field if it has. Here's the updated code:csharp
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.