Apexでファイルをライブラリ経由して公開方法
概要図
ファイルの登録
ContentDocument直接書き込みできないので、ContentVersionにレコードを登録して、ContentDocumentにデータを自動登録できる。
getAlertDescriptionFile
public static ContentVersion getAlertDescriptionFile() {
ContentVersion contentVersion_1 = new ContentVersion(
Title = "Test",
PathOnClient = 'Penguins.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true
);
return contentVersion_1;
}
ライブラリを作成
getContentworkspace
public static Contentworkspace getContentworkspace() {
return new Contentworkspace(
name='test for share file with library'
);
}
ファイルをライブラリに格納
getContentWorkspaceDoc
public static ContentWorkspaceDoc getContentWorkspaceDoc(ContentDocument cd, ContentWorkspace cws) {
return new ContentWorkspaceDoc(
contentdocumentId = cd.id,
ContentWorkspaceId = cws.id
);
}
ライブラリ権限の作成
getContentWorkspacePermission
public static ContentWorkspacePermission getContentWorkspacePermission() {
return new ContentWorkspacePermission(
Name='test view',
PermissionsAddContent = true,
PermissionsTagContent = true
);
}
ライブラリと権限とユーザに割り当て
getContentWorkspaceMember
public static ContentWorkspaceMember getContentWorkspaceMember(ContentWorkspace cws, ContentWorkspacePermission cwsp, User usr) {
return new ContentWorkspaceMember(
ContentWorkspaceId = cws.id,
ContentWorkspacePermissionId = cwsp.id,
MemberId = usr.id
);
}