2

My issue is that I can't seem to query more than one record at a time from the ContentVersion or ContentDocument if I'm not the owner of the file. Here is what works for me:

    Map<Id,ContentVersion> docIds = new Map<Id, ContentVersion>();
    for(Id curId : versionIds)
    {
        List<ContentVersion> cDoc = [select Id,ContentDocumentId
                                     From ContentVersion 
                                     Where Id = :curId ];
        for(ContentVersion cd :cDoc)
            docIds.put(cd.Id, cd);
    }

Here are various things I tried:

  1. Get it from a for loop:

     List<ContentVersion> cDoc = [select Id,ContentDocumentId 
                                 From ContentVersion 
                                 Where Id in :versionIds ];
    
     for(ContentVersion cd :cDoc)
           docIds.put(cd.Id, cd);
    
     for(ContentVersion cd : [select Id,ContentDocumentId 
                              From ContentVersion 
                              Where Id in :versionIds ])
           docIds.put(cd.Id, cd);
    
  2. Get it as a Map query:

    Map<Id,ContentDocument> docIds 
         = new Map<Id, ContentDocument>([select Id,LatestPublishedVersionId
                      From ContentDocument 
                      Where LatestPublishedVersionId in :versionIds ]);
    
  3. Same this, but broken out into a look, JIC the last one was having issues:

    Map<Id,ContentDocument> docIds = new Map<Id, ContentDocument>();
    {
        List<ContentDocument> cDoc 
              = [select Id,LatestPublishedVersionId
                 From ContentDocument
                 Where LatestPublishedVersionId in :versionIds ];
    
        for(ContentDocument cd :cDoc)
            docIds.put(cd.LatestPublishedVersionId, cd);
    
    }
    

So in all of these 3 queries, nothing comes back if I'm not the owner.

I'm wondering two things:

  1. Is there a way to do this without resorting to selecting one record at a time?
  2. Why does this work like it does?
1
  • What is the sharing level for the class invoking the queries? A shot in thr dark but this may be the result of sharing enforcement.
    – dsharrison
    Nov 19, 2015 at 2:50

1 Answer 1

1

I figured out how to do this - There is no default sharing for an organization, so you have to add a record for it. If you wanted to share everything, you'd add a ContentDocumentLink record for the Org ID. Here is example code of doing that for all ContentDocuments:

Id orgId = UserInfo.getOrganizationId();

List<ContentDocumentLink> existingCDL = 
     [SELECT Id, LinkedEntityId, ContentDocumentId, ShareType, Visibility 
      FROM ContentDocumentLink Where LinkedEntityId = :orgId];

Set<Id> alreadyLinked = new Set<Id>();
for(ContentDocumentLink cd : existingCDL)
    alreadyLinked.add(cd.ContentDocumentId);

List<ContentDocumentLink> cdl = new List<ContentDocumentLink>();
for(ContentDocument cd : [Select Id From ContentDocument])
{
    if(! alreadyLinked.contains(cd.Id))
        cdl.add(new ContentDocumentLink(
             LinkedEntityId = UserInfo.getOrganizationId(), 
             ContentDocumentId=cd.Id, 
             ShareType = 'C', 
             Visibility = 'AllUsers'));
}
insert cdl;

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.