Sunday, March 26, 2017

Advanced Security in Dynamics 365 CRM Using CRM SDK- Grant Access to User for a custom field(Encrypted – FLS enabled) to individual CRM records.



Advanced Security in Dynamics CRM Using CRM SDK- Grant Access to User for a custom field(Encrypted – FLS enabled) to a specific record.

In Dynamics CRM most of the time we assign Field Security profile to grant access to encrypted(FLS) fields which will provide access to the records that users have based on the security role. For certain scenarios we want to limit access to the users to only specific records (not all the records that they have access to) based on the business rule. Below is the snippet which grant access to users for individual CRM records:
if (CAattributeId == Guid.Empty)
                {
                    RetrieveAttributeRequest attrRequest = new RetrieveAttributeRequest()
                    {
                        EntityLogicalName = "opportunity",
                        LogicalName = "new_confidential",
                        RetrieveAsIfPublished = true
                    };
                    RetrieveAttributeResponse attrResponse = (RetrieveAttributeResponse)ImperService.Execute(attrRequest);
 
                    CAattributeId = attrResponse.AttributeMetadata.MetadataId.Value;
                }
                // Share the access
                Entity poaa = new Entity("principalobjectattributeaccess");
                poaa.Attributes.Add("attributeid", CAattributeId);
                poaa.Attributes.Add("objectid", new EntityReference("opportunity", Opptyid));
                poaa.Attributes.Add("principalid", new EntityReference("systemuser", userId));
                poaa.Attributes.Add("readaccess", true);
                poaa.Attributes.Add("updateaccess", false);
// updateaccess = true will allow users to update the field
 
ImperService.Create(poaa);
 


No comments:

Post a Comment