0

I want to share all records with admin user using next trigger:

trigger ContentVersionTrigger on ContentVersion (after insert) {
    system.debug('sdfsdf' + Trigger.new);
    for (ContentVersion item : Trigger.new) {

        ContentDocumentLink link = new ContentDocumentLink();
        link.LinkedEntityId = Id.valueOf('005E0000007Sew5IAC');
        link.ContentDocumentId = item.ContentDocumentId;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        insert link;

    }

}

It throws next exception:

Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type V: [ShareType]

Who knows how to fix it?

Update: If I use 'I' instead of 'V' I have another error:

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Document with ID: 0690L000003rtLV is already linked with the entity with ID: 005E0000007Sew5: Linked Entity ID: [LinkedEntityId]

But it's not true, such record doesn't exist

  • 2
    Who knows how to fix it? ---> "what have you tried or researched in order to fix this" ? – glls Sep 13 '17 at 15:03
  • Can you try this code Trigger ContentVersionTrigger on ContentDocumentLink (before insert) { for(ContentDocumentLink l:Trigger.new) l.Visibility='AllUsers'; } – Muzammil Bajaria Sep 13 '17 at 15:38
  • well first off - what if this is a new version to an already shared file? i don't see where resharing this with the same user will fix this. What other options have you explored? ContentDocumentLink trigger? – Ronnie Jul 13 '18 at 3:36
  • How did you resolve this issue? – Dinesh May 11 at 5:06
2

Using visibility to all users:

link.Visibility = 'AllUsers';

You should also set the share type to this:

link.ShareType = 'I';

Which means:

Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library.

You can find more info here

  • This discussion is good but it seems that the standard lightning UI always sets the share type to 'V' and can't be shared to external portal users. How would one set the share type to I and visibility to all users using the standard upload documents UI? – Scott Morrison Nov 13 '18 at 17:56
  • I can change the visibility and share type in apex code but there doesn't seem to be a way to do it in the UI which is really irritating. Guess I'll make a button that will share the documents publicly – Scott Morrison Nov 13 '18 at 18:13
0

It sounds like from the exception, INSUFFICIENT_ACCESS_OR_READONLY, that you do not have the correct permissions to access ShareType. I would look at the profile or permission set that your user has and check that field t make sure that it has write access.

  • It would be true if I weren't system administrator with all permissions. – user49158 Sep 13 '17 at 15:25
0

I think you are already success,move insert out of the loop ,try to use a list for insert, and donot hardcode the Id Try another time

trigger ContentVersionTrigger on ContentVersion (after insert) {
    system.debug('sdfsdf' + Trigger.new);
    List <ContentDocumentLink> ContentDocumentLinkList = New 
     List <ContentDocumentLink>();
    for (ContentVersion item : Trigger.new) {

        ContentDocumentLink link = new ContentDocumentLink();
        //should be Unique
        link.LinkedEntityId = Id.valueOf('005E0000007Sew5IAC');
        link.ContentDocumentId = item.ContentDocumentId;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        ContentDocumentLinkList.add(link);
        //insert link;

    }
    insert ContentDocumentLinkList;
}

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.