0

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.

0

Make sure User has permission to the library. Please read the description of ShareType field

enter image description here

  • Hi, Please see the code I've posted. It is working if im not using a List – Sunil Reddy Sep 21 at 11:28
0

INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType] generally occurs when you are trying to share a file to a user who already have Collaborator permission. The user can explicitly view and edit the shared file.

Before inserting the contentdocumentlink, you need to check whether the file has been shared already with the user or not.

As a workaround, You need to create one more separate set of contentdocumentid to check if the user already having the access to that file.

Set<Id> documentIdsForUsr = new Set<Id>(); 
        List<ContentDocumentLink> cdl=[SELECT id,LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:userinfo.getuserid()];  
        for(ContentDocumentLink cdLink:cdl){  
            documentIdsForUsr.add(cdLink.ContentDocumentId);  
        } 


for(ID id : documentIds){
            if(!documentIdsForUsr.contains(id){                 
                ContentDocumentLink cd = new ContentDocumentLink();
                cd.ContentDocumentId = id;
                cd.LinkedEntityId = Userinfo.getUserId();
                cd.ShareType = 'C';
                cd.Visibility = 'InternalUsers';
                cdlList.add(cd); 
            } 

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.