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
You can use the
Web.GetUserByIdmethod to retrieve the user object for the client ID, and then set theTitleproperty of the user object to the custom name you want to display. Here is an example of how you could update the code you provided to use a custom name:codeUser user = context.Web.GetUserById(clientId); context.Load(user); context.ExecuteQuery(); user.Title = "Custom Name"; user.Update(); context.ExecuteQuery(); uploadFile.ListItemAllFields["Author"] = user; uploadFile.ListItemAllFields["Editor"] = user; uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();This will set the "Author" and "Editor" fields of the file to the user object with the custom name you specified.
- 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
In order to keep the app as the file author/creator, but show the activity log as modified by another user's email, you can use the
EnsureUsermethod to ensure that the other user exists in the SharePoint site collection, and then set theEditorproperty of the file to the other user. Here's an example of how you could update the code you provided to achieve this:codeUser editor = context.Web.EnsureUser("otheruser@example.com"); context.Load(editor); context.ExecuteQuery(); uploadFile.ListItemAllFields["Editor"] = editor; uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();This will set the "Editor" field of the file to the other user's email address, so that the activity log will show that user as the last person to modify the file, while the "Author" field of the file will still be set to the app.
Also, you can use the
SystemUpdatemethod to update the item and keep the created by and created date,codeuploadFile.ListItemAllFields["Editor"] = editor; uploadFile.ListItemAllFields.SystemUpdate(false); context.ExecuteQuery(); - User
How can I stop it from changing the modified/editor name of all previous uploaded files also?
- Assistant
To prevent the modification of the "Editor" field from affecting the previously uploaded files, you need to make sure that you only modify the "Editor" field for the specific file that you are currently uploading, and not for all files in the SharePoint library.
- User
SystemUpdate wont accept true or false
- Assistant
You are correct, the
SystemUpdatemethod does not take any parameters. It is used to update an item without changing the 'Created' and 'Modified' fields.If you want to keep the original created by and created date, you can use the
SystemUpdatemethod to update the item, as in:codeuploadFile.ListItemAllFields["Editor"] = editor; uploadFile.ListItemAllFields.SystemUpdate(); context.ExecuteQuery();This will update the item, but it will keep the original created by and created date. If you don't want to keep the original created by and created date, you can update the item by using
Updatemethod as in:codeuploadFile.ListItemAllFields["Editor"] = editor; uploadFile.ListItemAllFields.Update(); context.ExecuteQuery();This will update the item, also it will change the 'Modified' fields.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.