I have a library called Test, which has a folder in it called 123. The folder contains a single pdf file.

enter image description here

I'm able to successfully query that file using the following SOQL statement.

SELECT Id, Title, FileExtension FROM ContentFolderItem WHERE ParentContentFolderId IN (SELECT Id FROM ContentFolder WHERE Name = '123')

Is there anyway to add to this query the condition to include the library name Test?

I was able to create the library, folder, and upload the pdf using the UI. How can I do all that with apex?

This is what I've done in my test code, but I can't navigate the relationships of all the sObjects involved.

    // create Library
    System.runAs ( new User(Id = UserInfo.getUserId()) ) {
        ContentWorkspace cw = new ContentWorkspace();
        cw.Name = 'Test';

        insert cw;        

    // creat file
    ContentVersion cv = new ContentVersion();
    cv.Title = 'dummy';
    cv.PathOnClient = 'dummy.pdf';
    cv.VersionData = EncodingUtil.base64Decode('test');

    insert cv;

     // create folder. There is no clear way to add this folder to a library using apex...
     ContentFolder f = new ContentFolder();
     f.Name = '123';

     insert f;

Then there needs to be some dml usage using ContentFolderItem and ContentFolderLink I believe...

After creating a folders, I've tried this code:

    ContentWorkspace cw = [SELECT Id FROM ContentWorkspace WHERE Name = 'Test'];

    Database.SaveResult[]  results = Database.insert(folders, true);

    List<ContentFolderLink> folderLinks = new List<ContentFolderLink>();

    for(Database.SaveResult r : results) {
        if(r.isSuccess()) {
            ContentFolderLink cfl = new ContentFolderLink();
            cfl.EnableFolderStatus = 'S';
            cfl.ParentEntityId = cw.Id;
            cfl.ContentFolderId = r.Id;


put I get a bunch of Field is not writeable errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.