1

How can we upload file in ContentFolder? I am able to upload file in Library directly but not in ContentFolder via apex.

For uploading file in library , I used the below Apex code and It works fine for me.

contentDocumentlink link = new contentDocumentlink();
link.ContentDocumentId  = '0696E000000IXTgQAO';
link.LinkedEntityId  ='07H6E000000021KUAQ';
link.ShareType  = 'I';
insert link;

Note :: 07H6E000000021KUAQ is Library Id and 0696E000000IXTgQAO is Salesforce File Id.

1

As per my understanding, you want to add a file in Content Folder and it's not possible to use folder id in LinkedEntityId field. Please follow these below steps:

  1. Insert ContentVersion.
  2. Insert ContentDocumentlink and use required Library Id in LinkedEntityId field.
  3. Query on ContentFolderMember Where ChildRecordId is your ContentVersion file Id and ParentContentFolderId is the RootContentFolderId of library(ContentWorkspace).
  4. Now update the ContentFolderMember record's ParentContentFolderId field with your required Folder Id.

Example:

ContentFolderMember updateFolderMember = new ContentFolderMember(
                                        Id = ContentFolderMemberId,
                                        ChildRecordId = ID_of_the_file,
                                        ParentContentFolderId = ParentContentFolderId
                                   );
update updateFolderMember;
1

You can try below code:

ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.ContentDocumentId = contentDocumentId;
cv.VersionData = EncodingUtil.base64Decode(base64Data);
cv.Title = fileName;
cv.PathOnClient = filename;
insert cv;

ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
cdl.Visibility = 'AllUsers';
cdl.LinkedEntityId = parentId;
cdl.ShareType = 'I';
insert cdl;
  • 1
    Sandip, thanks for contributing an answer here! In the future, pleaseuse the code format button ({}) instead of using HTML. Also, if you haven't already, please read more about How to Answer. We look forward to your future contributions! – sfdcfox Jul 11 '18 at 5:58
  • 1
    Also try to add some description with your answer, This will be helpful for reader as well as help you in gaining reputation quickly :) – Mr.Frodo Jul 11 '18 at 6:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.