Create a truly personal field with Field Level Security
Since Microsoft Dynamics CRM 2011 was released, I never had the chance to really use Field Level Security feature. Last week, I was asked to implement a secured field that should be accessed only by the owner of the record, even if the record itself can be accessed by anyone.
This can be achieved with one field level security profile and one plugin.
Field Level Security Profile
The field level security profile basically restrict Read and Update of the field to all users (or all teams) but allows Create. This way, anyone can create a record with access to the secure field. When created, as all users can’t read or update, the field is not accessible anymore.
Plugin
The plugin will handle this problem. On PostCreate event, it will share the secured field with the owner. For this, we use the entity PrincipalObjectAttributeAccess to give Read and Update access to the secured field to the owner of the record.
public void ShareSecureFieldWithOwner(Entity record) { // Any method that helps you find the AttributeMetadata Id var attributeId = FindSecuredAttribute(); if (attributeId != Guid.Empty) { var userAccess = new PrincipalObjectAttributeAccess { AttributeId = attributeId, ObjectId = record.ToEntityReference(), PrincipalId = record.OwnerId, UpdateAccess = true, ReadAccess = true }; context.AddObject(userAccess); context.SaveChanges(); } }
VoilĂ ! You have now a secure field accessible only for the owner of the record!
Comments
Can you tell me how FindSecuredAttribute() gets the attributeId?
Thank you :)
I am using CRM online and I saw that I needed to use entities like that :
Entity poaa = new Entity("principalobjectattributeaccess");
poaa["attributeid"] = attributeId;
poaa["objectid"] = objectIdReference;
poaa["readaccess"] = true;
poaa["updateaccess"] = true;
poaa["principalid"] = principalIdReference;
service.Create(poaa);
I have a problem. I can't create a new principalobjectattributeaccess because the service I use is connected to the user creating the new entity. Or this user does not have the rights to modify PrincipalObjectAttributeAccess. Therefore I have a privilege problem. Do you have any ideas about that?
I have one last question. When I create a Field Security Profile, if I do not connect it with any users or teams, it does not work. I have to explicitly select all users or all teams to make it work.
The thing is that in my solution, if I want to add a new team or user, I also have to add it to the security profile which is a huge constrain, do you have any idea to make it work? Or maybe it should work without connecting the Field Security Profile but my solution is not working properly...
Thank you anyway :)
Everything is explained in the post and the comments
Entity poa = new Entity("principalobjectattributeaccess");
can you explain how to create the plugins step by step.
Message
Pipeline
Class
appreciate your support
thanks
Sharepoint Online Training | Microsoft Dynamics CRM Online Training