I want to share/post a document to a Community user through APEX . Suggest some work around for this through APEX code.

4 Answers 4

Attachment myAttachment =[SELECT Id, Name,Body,Description,ContentType FROM  Attachment where Id =:myFile.Attachment_ID__c];

String communityId = 'myCommunityId';

ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = shareComment; 


ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;

ConnectApi.NewFileAttachmentInput fileIn = new ConnectApi.NewFileAttachmentInput();
fileIn.title = fetchDigitalChatterFile.Attachment_Name__c;                   
input.attachment = fileIn;

Blob userFile = myAttachment.Body; 

ConnectApi.BinaryInput feedBinary = new ConnectApi.BinaryInput(userFile,myAttachment.ContentType,myAttachment.name);

// PARAMETERS(communityId, feedType, subjectId, input,filebody);
ConnectApi.FeedItem feadItem = ConnectApi.ChatterFeeds.postFeedItem(communityId, ConnectApi.FeedType.Record,'share To Id',input,feedBinary);

OP's issue which is Community users can only access files that were uploaded INTO that community. (ie when an internal user with a full SFDC license uploads a file, Community users can't see it, even though the community users have access to view the record, AND prior to enabling Files, Community users could see Attachments that internal users uploaded.)

This single line of code for an APEX Trigger worked to flag all uploaded files so that all users in the Community could see them:

UpdateContentDocumentLinkVisibilityOnInsert on ContentDocumentLink (before insert) { for(ContentDocumentLink l:Trigger.new)l.Visibility='AllUsers'; }
  • ?? because you included your email address and phone# ??
    – cropredy
    Commented Dec 9, 2016 at 2:50
  • According to this document having contact info isn't specified as casue for deletion: salesforce.stackexchange.com/help/deleted-answers neither is it referenced here: meta.stackexchange.com/questions/5221/… Commented Dec 9, 2016 at 19:46
  • I am curious as to if I've violated a TOU or code of contact. Please provide information supporting the valid reason for the deletion of my answer. Commented Dec 10, 2016 at 1:28
  • I have no idea, @MattLacey (a moderator) deleted it - I was just guessing as to the reason - the 'contact us' is not typically seen on this site
    – cropredy
    Commented Dec 10, 2016 at 2:16
  • It was flagged as spam, and generally when I'm working through flags if I see a first ever post that includes details for a company and a line like "call us for help" I tend to just hit the delete button. Self promotion is against the values of the site salesforce.stackexchange.com/help/behavior I'll edit some context out of this because it won't make sense to people as it is. Sometimes the flags list gets pretty big, and if I study everything in crazy detail it'd quickly become a full time job! I have to rely on the community for context. Apologies BMFC, and welcome to the site.
    – Matt Lacey
    Commented Dec 16, 2016 at 1:34

Per the Apex Docs, Apex sharing is not available for Communities. Sharing sets are available as an alternative except in the case of Partner Communities. You don't say what kind of Community you have or the kind of Document you need to share, so further guidance is difficult to provide.

  • I am using Customer Community and scenario is a logged in user can share a document (word/pdf files) to any user present in the same community.
    – Arabinda
    Commented Oct 24, 2014 at 1:51
  • Currently in Chatter "chatter:feed component" to share/post a document to another user , the logged in user has to go the Chatter of that user and can share any document by posting it in chatter.Instead of this I want to create a VF page where a logged in user directly post/share any document to any user in the community.
    – Arabinda
    Commented Oct 24, 2014 at 1:57
  • Since file and record sharing in a Customer Community is managed primarily through sharing groups and permission sets, unless they happen to be the only members of the same sharing group, using Chatter or providing a link via email to an external file is the only way I can find that one Customer Community User can share a document with another User. In addition to the SF Help files, one reference you may want to look at is Getting Started with Communities.
    – crmprogdev
    Commented Oct 24, 2014 at 13:40

A client of mine already had a ton of files uploaded by internal users that they wanted to make available to partner community users. I was able to use the code below in the developer console with execute anonymous.

// Gather existing opportunity file links
List<ContentDocumentLink> existingOpportunityFileLinks = [
    FROM ContentDocumentLink
        LinkedEntityId IN (SELECT Id FROM Opportunity) // All opportunitities
        Visibility != 'AllUsers' // Don't need to reshare these; they're already shared
    LIMIT 100 // Do a few at a time and test

// Create new opportunity file links
List<ContentDocumentLink> newOpportunityFileLinks = new List<ContentDocumentLink>();
for(ContentDocumentLink existingFileLink : existingOpportunityFileLinks) {
    ContentDocumentLink newOpportunityFileLink = existingFileLink.clone();
    newOpportunityFileLink.Visibility = 'AllUsers'; // Make the file visibile to all users, both internal and external, who already have access to the linked opportunity records

// Delete existing file links since we can't change the visibility field on already saved records
delete existingOpportunityFileLinks;

// Save new opportunity file links
insert newOpportunityFileLinks;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .