I got multiple methods i developed in graph v5.6 But now i need to update them to v6.x i am stuck with these 2 methods
One to update metdada data of a file i found by query
protected static synchronized FieldValueSet postTagsToFile(GraphServiceClient graphClient, String driveId, String fileId, Map<String, String> metadata){
metadata.forEach((s, s2) -> {
if (!s2.isEmpty())
System.out.println(s + " : " + s2);
});
FieldValueSet fieldValueSet = new FieldValueSet();
metadata.forEach((key, value) -> fieldValueSet.additionalDataManager().put(key, new JsonPrimitive(value)));
return graphClient.drives(driveId).items(fileId).listItem().fields().buildRequest().patch(fieldValueSet);
}
Problem is that in the graph v6.x examples i need to have the list Id to update this, and i dont know how to have access to the listId in this method.
And one to upload a file to a location
public static String uploadFileToSharedSP(String fileName,String mimeType, String fileContent, String location) throws IOException {
DriveItem newItem = new DriveItem();
newItem.name = fileName;
newItem.file = new com.microsoft.graph.models.File();
newItem.file.mimeType = mimeType;
DriveRequestBuilder driverReq = graphClient.sites(SHARED_SITE_ID).drive();
DriveItemRequestBuilder driverRootReq = driverReq.root();
DriveItemRequestBuilder destination = driverRootReq.itemWithPath(location);
DriveItem createdDriveItem = destination.children().buildRequest().post(newItem);
driverReq.items(createdDriveItem.id)
.content()
.buildRequest()
.put(fileContent.getBytes());
return createdDriveItem.webUrl+"?web=1";
}
in this i dont know how to post the new file with the content to the location in the new api
There is no example i can find that i can follow.
I have been searching all online but i dont seem to be able to find a way to do this.
i was able to update the first one like this
public static synchronized FieldValueSet postTagsToFile(GraphServiceClient graphClient, String driveId, String fileId, Map<String, String> metadata){
ListItemRequestBuilder listItemReq = graphClient.drives().byDriveId(driveId).items().byDriveItemId(fileId).listItem();
ListItem listItem = listItemReq.get();
metadata.forEach((s, s2) -> {
if (!s2.isEmpty())
System.out.println(s + " : " + s2);
});
FieldValueSet fieldValueSet = new FieldValueSet();
fieldValueSet.setAdditionalData(Collections.unmodifiableMap(metadata));
SharepointIds sharepointIds = listItem.getSharepointIds();
FieldValueSet result = graphClient.sites()
.bySiteId(sharepointIds.getSiteId())
.lists().byListId(sharepointIds.getListId())
.items().byListItemId(listItem.getId())
.fields().patch(fieldValueSet);
return result;
}
My error was that i did not know that there is a new Propertie in the list item, that i could use to get the listId that the item belongs to, and with that i can post and update the fields.
and the second one my mistake was not adding a : at the end of the path
public static String uploadFileToSharedSP(String fileName,String mimeType, String fileContent, String location) throws Exception {
Drive drive = graphClient.sites().bySiteId(SHARED_SITE_ID).drive().get();
String destination = location+"/"+fileName;
DriveItemUploadableProperties properties = new DriveItemUploadableProperties();
properties.getAdditionalData().put("@microsoft.graph.conflictBehavior", "replace");
CreateUploadSessionPostRequestBody uploadSessionRequest = new CreateUploadSessionPostRequestBody();
uploadSessionRequest.setItem(properties);
UploadSession uploadSession = graphClient.drives()
.byDriveId(drive.getId())
.items()
.byDriveItemId("root:/"+destination+":")
.createUploadSession()
.post(uploadSessionRequest);
int maxSliceSize = 1_000_000;
InputStream uploadStream = new ByteArrayInputStream(fileContent.getBytes());
long streamSize = fileContent.getBytes().length;
LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<>(
graphClient.getRequestAdapter(),
uploadSession,
uploadStream,
streamSize,
maxSliceSize,
DriveItem::createFromDiscriminatorValue);
IProgressCallback callback = (current, max) -> logger.info("Uploaded {} bytes of {} total bytes", current, max);
UploadResult<DriveItem> uploadResult = largeFileUploadTask.upload(5, callback);
if (uploadResult.isUploadSuccessful()) {
String itemId = uploadResult.itemResponse.getId();
String webUrl = uploadResult.itemResponse.getWebUrl();
logger.info("Upload successful. itemId: {}, webUrl: {}", itemId, webUrl);
return webUrl+"?web=1";
} else {
logger.error("For Upload failed to {}", destination);
throw new IOException("To: "+destination+", Upload failed");
}
}