[SalesForce] Assign sharing reasons from Dynamically from APEX

Was wondering if there is way to assign the Sharing Reason aka Rowcause Dynamically. Well tried to implement using this "Schema.MyCustomObject__Share.rowCause.My_Sharing_Reason__c" but here I have to fix the sObject and the reason. Is there anyway to assign them dynamically?

Best Answer

You can't assign them completely dynamically.

You need to create them from the point and click interface (Setup->Create->Object Name->Apex Sharing Reasons) or via the meta-data api.

myObject__share share = new myObject__share();
share.put('RowCause','APIOfMyApexSharingReason__c');
share.put('ParentId','...');