I want to retrieve File Ids for which new entry has been added or removed from child ContentDocumentLinks relation. Following is the parent to child relationship query I made for retrieving Ids, title and child records of all ContentDocuments using Admin token.
/services/data/v32.0/query?q=SELECT+Id,+Title,+(+SELECT+Id,+ContentDocumentId+FROM+ContentDocumentLinks+)+FROM+ContentDocument
Is there any way to detect that a new entry has been made or removed from ContentDocumentLink object for each ContentDocument. Triggers are not allowed on ContentDocumentLink object else it would be an easy cookie to crack.
Whereas while experimenting on ContentVersion Triggers, it turns out sharing with an entity(example User, record, Group) initiates after update trigger, sharing with a public link or internally with the entire company doesn't initiate the same after update trigger.
To sum up, in a real-time scenario, an added entry reflects share activity on a particular File and a removed entry reflects unshare activity in ContentDocumentLink object. Any clue will be helpful. Thanks.
Best Answer
Triggers can fire on ContentVersion when new ContentDocuments are created or updated. See my answer to Trigger on ContentDocument not working for more on this. It appears to me, based on what I've italicized below, that you need to be querying by getting the ContentDocumentId from the ContentVersion Object instead of the ContentDocument Object, especially since I'm guessing you're likely looking for a version of the document. Everything below is taken from the Object Reference.
The following is said about ContentDocumentLink:
Edit - More on ContentDocumentLink:
Here's some of what you'll find under ContentVersion:
And this is what's said about ContentDocument:
Edit - Content Objects Schema:
Edit - User Sharing Permissions Schema:
Edit - In the Object Reference, under PermissionSet, I found the following and more:
Child Objects
Edit: Final Comments
The above note confirms your observation that record IDs are lost when once a document has been unshared. ContentDocumentLink only has 4 fields that aren't standard System Fields. Those fields tell very little and are mostly picklists. It appears to me that the ContentDocumentLink is going to tell you very little in terms of sharing.
If detecting changes in sharing is your primary objective, I recommend you study the example queries in PermissionSets section of the Object Reference Document to gain further insight into how to achieve your desired results. There were too many query examples for me to cite them all in this answer.
It's my sense that your solution will likely include some combination of Permission Sets, Document Libraries, Sharing Groups and ContentVersion/ContentDocument. I hope what I've provided helps provide some clarity for you.