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.

2 Answers 2


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.


ContentFolderMember updateFolderMember = new ContentFolderMember(
                                        Id = ContentFolderMemberId,
                                        ChildRecordId = ID_of_the_file,
                                        ParentContentFolderId = ParentContentFolderId
update updateFolderMember;
  • ChildRecordId is ReadOnly Field. Feb 6, 2020 at 5:25
  • How does the ContentFolderMemberId variable get created ?
    – VanAlfredo
    Mar 9, 2022 at 4:34
  • This worked for me except when querying the ContentFolderMember I had to filter on the ChildRecordId = ContentDocument_ID (not the ContentVersion) Apr 18, 2023 at 22:14

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, 2018 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, 2018 at 6:25
  • it's only working with record ids. use parentId as the record id. FolderContentId and other ids not working with ContentDocumentLink. Feb 6, 2020 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .