Cannot update File's term (managed metadata) property if the file is a .docx, .xlsx, ect.
Category
- [x] Bug
Describe the bug
I use the following code to obtain the file properties and load the relevant data to edit its properties.
SharePoint.IFile file;
await using (Stream fileStream = await fileClient.OpenReadAsync())
{
file = await folder.Files.AddAsync(fileClient.Name, fileStream, true);
}
await file.LoadAsync(f => f.ListItemAllFields.QueryProperties(
listItem => listItem.All,
listItem => listItem.ParentList.QueryProperties(
list => list.Title,
list => list.Fields.QueryProperties(
field => field.InternalName,
field => field.FieldTypeKind,
field => field.TypeAsString,
field => field.Title
)
)
));
I use the following code to edit the TermFieldName property.
file.ListItemAllFields["TermFieldName"] = "4f3f1ac3-f9b4-4b50-979a-82e97108390f" // single term (managed metadata)
file.ListItemAllFields["Subject"] = file.Name // single line text
await file.ListItemAllFields.UpdateOverwriteVersionAsync();
I have tested this on the file extensions pdf, jpg, txt, odt, rtf, doc, docx and xlsx. All extensions other than docx and xlsx have their files update both the TermFieldName and Subject. Docx and xlsx updates the Subject but not the TermFieldName.
Steps to reproduce
Use ListItemAllFields to update a term (managed metadata) alongside any other fields. When attempting on a docx or xlsx file, the term field will not be populated, but other fields are in the same UpdateOverwriteVersionAsync() call. file.ListItemAllFields.Values shows the term field guid.
Expected behavior
The TermFieldName will be populated.
Environment details (development & target environment)
- SDK version: 1.12.0 / 1.13.0
- OS: Windows 10
- SDK used in: Azure WebJob (manually triggered)
- Framework: .NET Core 6.0
- Browser(s): Console
- Tooling: Visual Studio 2022
- Additional details: The more context you can provide, the easier it is (and therefore quicker) to help.
Additional context
No error is displayed. All other fields are updated, however the term fields are ignored. Both fields are required. There are other optional fields and required fields which are not always populated. The file is newly created using PnP Core SDK prior to attempting to edit it.
The issue occurs using any of SystemUpdateAsync() and UpdateOverwriteVersionAsync(). Using UpdateAsync() causes the following error on ALL files (not just docx / xlsx): SharePointRestServiceException: There was an exception while writing field TermFieldName. Verify you're using the correct InternalName value for the field you want to write to.
Out of curiosity to make sure I'm not going insane.. I am changing the extension of the file via code to .tmp. The .tmp file allows the term to be set.
I ran the following code after to see what the client sees..
IFile editedFile = pnpRepositoryContext.Web.GetFileByServerRelativeUrl(addedFile.ServerRelativeUrl);
await editedFile.LoadAsync(f => f.ListItemAllFields.QueryProperties(
listItem => listItem.All,
listItem => listItem.ParentList.QueryProperties(
list => list.Title,
list => list.Fields.QueryProperties(
field => field.InternalName,
field => field.FieldTypeKind,
field => field.TypeAsString,
field => field.Title
)
)
));
await file.LoadAsync(f => f.ListItemAllFields.QueryProperties(
listItem => listItem.All,
listItem => listItem.ParentList.QueryProperties(
list => list.Title,
list => list.Fields.QueryProperties(
field => field.InternalName,
field => field.FieldTypeKind,
field => field.TypeAsString,
field => field.Title
)
)
));
editedFile.ListItemAllFields Values contains a null entry for the term fields, while file.ListItemAllFields Values related to the terms remain populated.
@IllumiC : is this currently still an issue? Feels much like a temporary system glitch...
I have come across this issue with Microsoft documents before, even just uploading them in the web ui. Check that your document doesn't have any embedded metadata already. When it does, it seems to "win".