[SalesForce] How to get ContentVersion Public Link

I am trying to get ContentVersion i.e Files public link URL of File Attached to any object which I need to pass in XML. So that it is visible to anyone without logging to salesforce(Want public link URL of File Attached to any object.)

What I have tried so far:

        cdlist = new List<ContentDocumentLink>([Select ContentDocumentID,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId IN: pulicationIdsSet]);

        Map<Id,Id> linkedDocsMap = new Map<Id,Id>();
        for(ContentDocumentLink cl:cdlist){
            linkedDocsMap.put(cl.ContentDocumentID,cl.LinkedEntityId);
        }
        cvlist = new List<ContentVersion>([Select Id,ContentDocumentId,title FROM ContentVersion where ContentDocumentId in:linkedDocsMap.keySet() order by CreatedDate DESC]);       

        contentVerMap = new Map<ID, List<ContentVersion>>();
        for(ContentVersion file : cvlist){
            Id linkId = linkedDocsMap.get(file.ContentDocumentId);
            if(!contentVerMap.containsKey(linkId))
                contentVerMap.put(linkedDocsMap.get(file.ContentDocumentId),new List<ContentVersion>());
            contentVerMap.get(linkId).add(file);
        }

Best Answer

I'm still trying to figure this out myself, but I believe what you are looking for is the DistributionPublicUrl field on the ContentDistribution object. I think the query would be something like:

select DistributionPublicUrl from ContentDistribution where ContentDocumentId = :file.ContentDocumentId

It looks like the ContentDistribution record is what gets created when you generate a public link to a file.