0

i want to create a public link for a file , Class :

public void execute(Database.BatchableContext BC, List<Challenge__c> scope) {
        Set<Id> testIsd = new  Set<Id>();
        Set<Id> associationPackAccountIds = new Set<Id>();
        Map<Id , Id> mContentDocumentIdLinkedId = new  Map<Id , Id>();

        for(Challenge__c ass : scope){
            testIsd.add(ass.Id);
        }

        List<ContentDocumentLink> lstDocLink = new List<ContentDocumentLink>();
        List<ContentDistribution> lstcontentDisToInsert = new List<ContentDistribution>();
        List<Id> listContentDocuIds= new List<Id>();
        List<Id> listContentDocIds = new List<Id>();
        Set<Id> finalList = new Set<Id>();

        List<ContentDocumentLink> contentDocumentLinksToUpdate = new 
       List<ContentDocumentLink>();


        for(ContentDocumentLink cd : [SELECT ContentDocumentId  , Visibility,LinkedEntityId
        FROM ContentDocumentLink 
        WHERE LinkedEntityId IN :testIsd  order by  SystemModstamp ]){
            cd.Visibility = 'AllUsers';
            cd.ShareType = 'I';

        contentDocumentLinksToUpdate.add(cd);
        listContentDocIds.add(cd.ContentDocumentId);
        mContentDocumentIdLinkedId.put(cd.ContentDocumentId , cd.LinkedEntityId) ;
        }
        if (!contentDocumentLinksToUpdate.isEmpty()) {
            update contentDocumentLinksToUpdate;
        }
        if (listContentDocIds.size() > 0){
            for(ContentVersion cv : [SELECT Id , ContentDocumentId
                    FROM ContentVersion
                    WHERE ContentDocumentId IN :listContentDocIds
                    AND FileType = 'PDF' AND IsLatest =true ]){
                        listContentDocuIds.add(cv.ContentDocumentId);
            }
        }
        List<ContentDistribution> lstConDis = [SELECT Id, ContentDownloadUrl, ContentVersionId, ContentDocumentId FROM ContentDistribution WHERE ContentDocumentId IN :listContentDocuIds];
        Set<Id> contentDocumentIdsToRemove = new Set<Id>();

        for (ContentDistribution cdd : lstConDis) {
            if (mContentDocumentIdLinkedId.containsKey(cdd.ContentDocumentId)) {
                contentDocumentIdsToRemove.add(cdd.ContentDocumentId);
            }
        }
        List<Id> newListContentDocuIds = new List<Id>();
        for (Id id : listContentDocuIds) {
            if (!contentDocumentIdsToRemove.contains(id)) {
                newListContentDocuIds.add(id);
            }
        }
        
        if(newListContentDocuIds.size()>0){
            for(Id str : newListContentDocuIds)    {
                // Generate the public link for each ContentDocumentLink
                ContentDistribution contentDis = generatePublicLink(str); 
                    if(contentDis != null){
                        lstcontentDisToInsert.add(contentDis) ;
                    }               
            }
        }
     

        if(lstcontentDisToInsert.size()>0){  

        Database.SaveResult[] srList = Database.insert(lstcontentDisToInsert, false);

        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                System.debug('Successfully inserted content Delivery ' + sr.getId());
            }
            else {
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Is error: ' + sr.getId());
                }
            }
        }
        }
    }

    global void execute(SchedulableContext ctx){
        Batch_CreatePublicLinkChallenge BatCreatePublicLink = new 
        Batch_CreatePublicLinkChallenge(); 
        Id batchId = Database.executeBatch(BatCreatePublicLink,100);
    }

    public static ContentDistribution  generatePublicLink(Id contentDocumentId){
        ContentVersion cv = [SELECT Id, Title FROM ContentVersion WHERE ContentDocumentId = 
        :contentDocumentId limit 1];
        ContentDistribution conDis = new ContentDistribution();
        conDis.Name = cv.Title;
        conDis.ContentVersionId = cv.Id;
        conDis.PreferencesAllowViewInBrowser= true;
        return conDis;
    }

it throws an error INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: because The sharing type is managed by a library and apparently I don't have access to the library ..

I'm attempting to set up users to create public links from Files added to Libraries. They are able to create public links to Files before they are added to a Library, but lose that option once the File is in a Library. I've been reading that this is defined on an individual Library basis, but can't locate the library permissions. Even when I switch to Classic, nothing comes up when I type "content permissions" in the setup quick search and I cannot locate the Library Permissions

1
  • to start with, you need to make the running user a member of the library to gain access to it. Even Modify All Data is not enough. Related
    – cropredy
    Commented Jun 6 at 21:49

0

You must log in to answer this question.