How to assign security role to a Form programmatically in Dynamics 365 CRM

By | June 24, 2019

Introduction

In Dynamics 365 we can update security role of Form through customization. You have to just follow the given steps:

Go to Setting → Customization → Customize the System → Components → Entities → Forms

Open Form and click on “Enable Security Roles” in Home tab to Assign Security Role to selected Form.

However, in few cases you can also assign security role to a Form programmatically.

In this blog, we will discuss about how to assign security role to a Form programmatically.

The steps to assign security role programmatically are as follows:-

  • Retrieve Forms from CRM

//object of form Collection

EntityCollection forms = new EntityCollection();

//Query Expression Object

QueryExpression queryExp = null;

queryExp = new QueryExpression

{

EntityName = “systemform”,

ColumnSet = new ColumnSet(“formid”, “type”, “formxml”, “name”)

};

FilterExpression conditions = new FilterExpression(LogicalOperator.And);

conditions.Conditions.Add(new ConditionExpression(“name”, ConditionOperator.Equal, “Account”));

queryExp.Criteria.AddFilter(conditions);

//Retrieve form collection

forms = _service.RetrieveMultiple(queryExp);

  • Create formXML (XDocument) and DisplayCondition(XElement) objects

//parse formXML into XDocument

XDocument formXML = XDocument.Parse(forms.Entities[0].Attributes[“formxml”].ToString());

Note: In this code we have only used one Form, but you can also perform same operation for multiple Forms using for loop or for each statement.

//Get displayCondtion element from form XML if Exist

XElement displayCondition = formXML.Descendants(“DisplayConditions”).FirstOrDefault();

//Create new displayCondition Object

XElement displayConditionAttribute;

  • Retrieve role details from CRM

//Create role collections

string[] roleNames = { “Activity Feeds”, “Sales Manager” };

Note: You can pass collection of Roles dramatically through Input Parameter in Plugin (using SecureConfig and UnsecureConfig)/ Assembly (Input parameters)

string condition = string.Empty;

//Create role condition

if (roleNames!= null)

{

foreach (string role in roleNames)

{

condition = condition + $”<condition attribute = ‘name’ operator= ‘eq’ value='{role}’/>”;

}

}

string fetchXML = string.Empty;

fetchXML = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>” +

“<entity name=’role’>” +

“<attribute name=’roleid’/>” +

“<filter type=’or’>” + condition + “</filter>” +

“</entity>” +

“</fetch>”;

var query = new FetchExpression(fetchXML);

EntityCollection roles = new EntityCollection();

//Collection of Role

roles = _service.RetrieveMultiple(query);

  • Check weather display condition exists or not. If not then create new display condition and update it otherwise, just update the existing display condition.

//Check DispalyConditon Exist or not

if (displayCondition != null)

{

displayConditionAttribute = formXML.Descendants(“DisplayConditions”).FirstOrDefault();

}

else

{

//create display condition

displayConditionAttribute = new XElement(“DisplayConditions”);

//to set a form as follbackform

displayConditionAttribute.Add(new XAttribute(“FallbackForm”, true));

//set form order

displayConditionAttribute.Add(new XAttribute(“Order”, “1”));

XElement form = formXML.Descendants(“form”).FirstOrDefault();

//add display condition on form element

form.LastNode.AddAfterSelf(displayConditionAttribute);

//get display condition on form

displayConditionAttribute = formXML.Descendants(“DisplayConditions”).FirstOrDefault();

}

//specify that the form is visible to everyone

XElement Everyone = new XElement(“Everyone”);

//add Everyone element into displaycondition

displayConditionAttribute.Add(Everyone);

Note: If you want to display Form to everyone then use the above code. If not then use the below code.

//specify that the security role to view a form

if (roles != null && roles.Entities.Count > 0)

{

foreach (Entity role in roles.Entities)

{

string roleId = “{” + role.GetAttributeValue<Guid>(“roleid”).ToString() + “}”;

XElement Role = new XElement(“Role”);

Role.Add(new XAttribute(“Id”, roleId));

//add Role element into displaycondition

displayConditionAttribute.Add(Role);

}

}

//Update form XML

forms.Entities[0][“formxml”] = formXML.ToString(SaveOptions.DisableFormatting);

//Update Form

_service.Update(forms.Entities[0]);

Publish customization programmatically after updating Form.

You can use the above code in plugin/assembly and any other application.

Conclusion

We can assign one or more security role for more than one CRM Form programmatically.

Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM

Leave a Reply

Your email address will not be published. Required fields are marked *