Search code examples
javamicrosoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-files

Adding Metadata to Sharepoint File with ListItem and FieldValueSet


I am trying to add some metadata to my file I uploaded into Sharepoint. In Sharepoint I added "columns" for "DocumentType" and "User". But when I run my code to add the values to the file that was uploaded I get an error saying

com.microsoft.graph.http.GraphServiceException: Error code: invalidRequest Error message: Invalid request

or when I was using patch() I get

com.microsoft.graph.http.GraphServiceException: Error code: invalidRequest Error message: Field 'id' is not recognized

I am not ever adding an 'id' field, and I don't see one already in the FieldValueSet.

private addMetadataToFile(PlatformOidcUser platformOidcUser, ListItem file, String itemPath, Map<String, String> metaData) {
    FieldValueSet fieldValueSet = file.fields
    metaData.each {data -> {
        fieldValueSet.additionalDataManager().put(data.key, new JsonPrimitive(data.value))
    }}

    graphServiceClient.groups(platformOidcUser.defaultGroupId)
            .drive()
            .root()
            .itemWithPath(itemPath)
            .listItem()
            .fields()
            .buildRequest()
            .put(fieldValueSet)
}

I've also tried calling patch(fieldValueSet). Also with no values in the map to add, such that the fieldValueSet has all the values the ListItem had when I retrieved it from the Graph API with the following code.

private ListItem retrieveListItemFromFile(PlatformOidcUser platformOidcUser, String itemPath) {
    return graphServiceClient.groups(platformOidcUser.defaultGroupId)
            .drive()
            .root()
            .itemWithPath(itemPath).listItem()
            .buildRequest().get()
}

I can't figure out what is wrong, why it doesn't work. I have spent over 6 hours on this one thing. And I have lost all my hair now. ;)


Solution

  • Found the answer here.

    UpdateAysnc throws "Field 'id' is not recognied" error

    Basically, you send just the new metadata values, not the entire already existing FieldList, because id is read-only there.

    But now I get an invalid request, even though it is valid, as the GET with the exact same code works, but the PUT/PATCH doesn't