Uploading large file to SharePoint with metadata using Microsoft Graph SDK for java

Viewed 292

Uploading a large file to SharePoint Online (Document library) via the MS Graph SDK (Java) works for me, but adding also metadata on an upload seems to be hard

I tried the to add the metadata inside the DriveItemUploadableProperties, because I didn't find any hints where the right place should be

DriveItemUploadableProperties value = new DriveItemUploadableProperties();
value.additionalDataManager().put("Client",  new JsonPrimitive("Test ABC"));
    
var driveItemCreateUploadSessionParameterSet = DriveItemCreateUploadSessionParameterSet.newBuilder().withItem(value);

UploadSession uploadSession = graphClient.sites(SPValues.SITE_ID).lists(SPValues.LIST_ID).drive().root().itemWithPath(path).createUploadSession(driveItemCreateUploadSessionParameterSet.build()).buildRequest().post();
LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<>(uploadSession, graphClient, fileStream, streamSize, DriveItem.class);
    
LargeFileUploadResult<DriveItem> upload = largeFileUploadTask.upload(customConfig);

This results in a 400 : Bad Request response

How can I add metadata on an upload the right way?

1 Answers

AFAIK, you cannot add metadata while uploading to Sharepoint. You will have to make two separate requests, one to upload the file, and one to add additional metadata to the file that you just uploaded.

Before adding your own custom metadata, you must register the facets / schema to OneDrive. Refer to this doc :

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/custom-metadata-facets?view=odsp-graph-online

But you should be aware that because custom facets are a feature in preview, at the time of this post you have to literally contact an MS email and get the custom facet manually approved, there is no automatic API to do this unfortunately.

If you somehow manage to get the custom facet approved :

DriveItemUploadableProperties has preset fields such as filename, size, etc. meant to represent the upload task and basic details about the file, there are no options to add additional metadata to it. Refer to the documentation for DriveItemUploadableProperties :

https://docs.microsoft.com/en-us/graph/api/resources/driveitemuploadableproperties?view=graph-rest-1.0

I assume that when you say, "Uploading a large file to SharePoint Online (Document library) via the MS Graph SDK (Java) works for me", you are able to successfully upload the file and obtain the item ID in the response from the uploaded file. You can use the item ID to update the metadata of the file via a second request. Specifically, refer to the update driveitem here :

https://docs.microsoft.com/en-us/graph/api/driveitem-update?view=graph-rest-1.0&tabs=http

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

DriveItem driveItem = new DriveItem();
driveItem.name = "new-file-name.docx";

graphClient.me().drive().items("{item-id}")
    .buildRequest()
    .patch(driveItem);

Edit :

As additional information, you can use a ListItem rather than a DriveItem resource and input custom fields there. However, you should be aware that unlike custom facets that I mention above, custom metadata stored in these fields are not indexed and is not meant to be queried / filtered on large datasets, which is the most common use case for metadata. When querying for these fields you must include the

Prefer : HonorNonIndexedQueriesWarningMayFailRandomly

in the request header, and as the header says you should be aware that the query may fail randomly in large datasets.

Related