I have a lightning component which executes an apex class on load. In apex class, I'm getting some list of files and providing 'Collaborator' access to the logged-in user if he is eligible. When I'm inserting a single contentdocumentlink with shareType='c' it works but doesn't if the same record is in the List.
Error : System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType]
Code :
@AuraEnabled
public static void createContentDocLink(String recordId){
Set<Id> documentIds = new Set<Id>();
List<ContentDocumentLink> cdl=[SELECT id,LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN:recordId];
for(ContentDocumentLink cdLink:cdl){
documentIds.add(cdLink.ContentDocumentId);
}
List<ContentDocumentLink> cdlList = new List<ContentDocumentLink>();
for(ID id : documentIds){
ContentDocumentLink cd = new ContentDocumentLink();
cd.ContentDocumentId = id;
System.debug('cd.ContentDocumentId :'+id);
cd.LinkedEntityId = Userinfo.getUserId();
System.debug('Userinfo.getUserId() :'+Userinfo.getUserId());
cd.ShareType = 'C';
cd.Visibility = 'InternalUsers';
cdlList.add(cd);
system.debug('cdlList :'+cdlList);
insert cdlList;
//Below commneted code works
/* ContentDocumentLink cd = new ContentDocumentLink();
cd.ContentDocumentId = '06**************';
cd.LinkedEntityId = '005***********';
cd.ShareType = 'C';
system.debug('cD '+cd);
insert cd;*/
}
}
Note : getting the same error even though the list contains single record.