我得到了在 graph v5.6 中开发的多种方法 但现在我需要将它们更新到 v6.x 我坚持使用这两种方法
更新我通过查询找到的文件的metdada数据
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);
}
问题是,在图表 v6.x 示例中,我需要列表 Id 来更新它,并且我不知道如何在此方法中访问 listId。
还有一个将文件上传到某个位置
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";
}
在此我不知道如何将包含内容的新文件发布到新 api 中的位置
我找不到可以遵循的例子。
我一直在网上搜索,但我似乎无法找到一种方法来做到这一点。
我能够像这样更新第一个
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;
}
我的错误是我不知道列表项中有一个新的属性,我可以用它来获取该项目所属的listId,然后我可以发布和更新字段。
我的第二个错误是没有在路径末尾添加 :
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");
}
}