+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Share content document link with specific user group or profile

So in this example for inserting Content docuemnt link.  How can I limit access to a specifc user group or profile?  And how could I use a CMT so that an admin can update or add groups so that the sahring is not hard coded in the apex?
//Get attachment
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1];
//Insert ContentVersion
ContentVersion cVersion = new ContentVersion();
cVersion.ContentLocation = 'S';
cVersion.PathOnClient = attach.Name;
cVersion.Origin = 'H';
cVersion.OwnerId = attach.OwnerId;
cVersion.Title = attach.Name;
cVersion.VersionData = attach.Body;
Insert cVersion;
//After saved the Content Verison, get the ContentDocumentId
Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
//Insert ContentDocumentLink
ContentDocumentLink cDocLink = new ContentDocumentLink();
cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId
cDocLink.ShareType = 'I';
cDocLink.Visibility = 'InternalUsers';
Insert cDocLink;
Andrew GAndrew G
Your reference would be here
ContentDocumentLink | Object Reference for Salesforce and Lightning Platform | Salesforce Developers (https://developer.salesforce.com/docs/atlas.en-us.228.0.object_reference.meta/object_reference/sforce_api_objects_contentdocumentlink.htm)
cDocLink.ShareType = 'V';
cDocLink.Visibility = 'SharedUsers';
cDocLink.LinkedEntityId = someGroup.id; 
Remember there can be multiple CDLs for a given ContentDocument

Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply Andrew. 
Can I have something like this:
cDocLink.LinkedEntityId = someGroup.id;
cDocLink.LinkedEntityId = someGroup.id; 
cDocLink.LinkedEntityId = childRecord.id;

The file needs to be shared with a specifc group/profile and I am also copying the file to another record.
Phuc Nguyen 18Phuc Nguyen 18
Andrew, can I control visibilty of the file with visibilty or sharing?  
Andrew GAndrew G
The File is the Content Document

The CDL (Content Document Link) is each individual sharing of the Content Document with which ever group.

So ti will be 
//Pseudo code

//Create the Content Document record

//Create the CDL to share with group 1

//Create the CDL to share with group 2

//create the CDL to share with individual A

The ShareType defines what level of Access (e.g. View)

The Visibiloity is who can see.

Phuc Nguyen 18Phuc Nguyen 18
Thanks for the example Andrew.  The issue I am running into is that there are users with different Profiles have access to a record.  But I do not want all of these users to see the file.  It appears that by default the visibilty is set to 'All Users'.  Any thoughts on how to hide the file from some users eventhough they have access to the record where the file is attached?