Search code examples
javaspring-bootsharepointmicrosoft-graph-apimicrosoft-graph-sdks

I got error 404 Not Found when adding metadata to file in sharepoint via Microsoft Graph API SDK Java


Framework
Springboot 2.6.7

Used libraries
microsoft-graph 5.0.0, azure-identity 1.11.0

Context
After my authentication via azure-identity, I am using microsoft graph api via Java SDK to upload file to SharePoint then adding metadata to it.

Problem
The upload function works well, but the adding metadata does not, it always return 404:NOT FOUND
I did verify that :

  • The file exists in SharePoint
  • The metadata feild exists in Sharepoint (i added it before working, with the same name: testMetadataId)
  • Both functions uses the same env variables

My Debug
I am using the same API via postman to add metadata and it works fine with the same URL and on the same file.
When i run the app in debug mode i find that the called URL to be the exact same as my Postman test URL.

Resources
Upload function code

public void uploadFile(String fileName, byte[] fileContent) {
        graphClient
                .sites(sharePointParameters.getSiteId())
                .drives(sharePointParameters.getDriveId())
                .root()
                .itemWithPath(sharePointParameters.getFolderPath() + "/" + fileName)
                .content()
                .buildRequest()
                .put(fileContent);
    }

Add metadata function code

public void updateMetaData(String fileName, String testMetadataId) {
        FieldValueSet fields = new FieldValueSet();
        fields.additionalDataManager().put("testMetadataId",
                new JsonPrimitive(testMetadataId));

        graphClient
                .sites(sharePointParameters.getSiteId())
                .drives(sharePointParameters.getDriveId())
                .root()
                .itemWithPath(sharePointParameters.getFolderPath() + "/" + fileName)
                .listItem()
                .fields()
                .buildRequest()
                .patch(fields);
    }

Postman metadata API that is working

https://graph.microsoft.com/v1.0/sites/{{site-id}}/drives/{{drive-id}}/root:/{{folder-path}}/{{filename}}:/listItem/fields

Solution

  • Problem solved !
    It turns out that in Microsoft Graph API SDK for Java: the methode for updating Metadata in SharePoint does not support nested files

    So instead of uploading the file in a nested folder with the path sharePointParameters.getFolderPath() + "/" + fileName we did change it to upload the file directly into the documents drive.

    So for those facing the same problem here is a code that works:

    public void uploadFile(String fileName, byte[] fileContent) {
            graphClient
                    .sites(sharePointParameters.getSiteId())
                    .drives(sharePointParameters.getDriveId())
                    .root()
                    .itemWithPath(fileName)
                    .content()
                    .buildRequest()
                    .put(fileContent);
        }
    
    public void updateMetaData(String fileName, String testMetadataId) {
            FieldValueSet fields = new FieldValueSet();
            fields.additionalDataManager().put("testMetadataId",
                    new JsonPrimitive(testMetadataId));
    
            graphClient
                    .sites(sharePointParameters.getSiteId())
                    .drives(sharePointParameters.getDriveId())
                    .root()
                    .itemWithPath(fileName)
                    .listItem()
                    .fields()
                    .buildRequest()
                    .patch(fields);
        }
    

    Issue created in Github : https://github.com/microsoftgraph/msgraph-sdk-java/issues/2031