{"id":19269,"date":"2019-06-24T10:50:17","date_gmt":"2019-06-24T10:50:17","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=19269"},"modified":"2020-05-28T07:27:20","modified_gmt":"2020-05-28T07:27:20","slug":"how-to-assign-security-role-to-a-form-programmatically-in-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/06\/how-to-assign-security-role-to-a-form-programmatically-in-dynamics-365-crm\/","title":{"rendered":"How to assign security role to a Form programmatically in Dynamics 365 CRM"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>In Dynamics 365 we can update security role of Form through customization. You have to just follow the given steps:<\/p>\n<p>Go to <strong>Setting \u2192<\/strong><strong> Customization \u2192<\/strong><strong> Customize the System \u2192<\/strong><strong> Components \u2192<\/strong><strong> Entities \u2192<\/strong><strong>\u00a0Forms <\/strong><\/p>\n<p>Open Form and click on \u201c<strong>Enable Security Roles<\/strong>\u201d in Home tab to Assign Security Role to selected Form.<\/p>\n<p>However, in few cases you can also assign security role to a Form programmatically.<\/p>\n<p>In this blog, we will discuss about how to assign security role to a Form programmatically.<\/p>\n<p>The steps to assign security role programmatically are as follows:-<\/p>\n<ul>\n<li>Retrieve Forms from CRM<\/li>\n<\/ul>\n<p>\/\/object of form Collection<\/p>\n<p>EntityCollection forms = new EntityCollection();<\/p>\n<p>\/\/Query Expression Object<\/p>\n<p>QueryExpression queryExp = null;<\/p>\n<p>queryExp = new QueryExpression<\/p>\n<p>{<\/p>\n<p>EntityName = &#8220;systemform&#8221;,<\/p>\n<p>ColumnSet = new ColumnSet(&#8220;formid&#8221;, &#8220;type&#8221;, &#8220;formxml&#8221;, &#8220;name&#8221;)<\/p>\n<p>};<\/p>\n<p>FilterExpression conditions = new FilterExpression(LogicalOperator.And);<\/p>\n<p>conditions.Conditions.Add(new ConditionExpression(&#8220;name&#8221;, ConditionOperator.Equal, &#8220;Account&#8221;));<\/p>\n<p>queryExp.Criteria.AddFilter(conditions);<\/p>\n<p>\/\/Retrieve form collection<\/p>\n<p>forms = _service.RetrieveMultiple(queryExp);<\/p>\n<ul>\n<li>Create formXML (XDocument) and DisplayCondition(XElement) objects<\/li>\n<\/ul>\n<p>\/\/parse formXML into XDocument<\/p>\n<p>XDocument formXML = XDocument.Parse(forms.Entities[0].Attributes[&#8220;formxml&#8221;].ToString());<\/p>\n<blockquote><p><strong>Note<\/strong>: In this code we have only used one Form, but you can also perform same operation for multiple Forms using <strong>for loop<\/strong> or <strong>for each<\/strong> statement.<\/p><\/blockquote>\n<p>\/\/Get displayCondtion element from form XML if Exist<\/p>\n<p>XElement displayCondition = formXML.Descendants(&#8220;DisplayConditions&#8221;).FirstOrDefault();<\/p>\n<p>\/\/Create new displayCondition Object<\/p>\n<p>XElement displayConditionAttribute;<\/p>\n<ul>\n<li>Retrieve role details from CRM<\/li>\n<\/ul>\n<p>\/\/Create role collections<\/p>\n<p>string[] roleNames = { &#8220;Activity Feeds&#8221;, &#8220;Sales Manager&#8221; };<\/p>\n<blockquote><p><strong>Note<\/strong><strong>:<\/strong> You can pass collection of Roles dramatically through Input Parameter in Plugin (using SecureConfig and UnsecureConfig)\/ Assembly (Input parameters)<\/p><\/blockquote>\n<p>string condition = string.Empty;<\/p>\n<p>\/\/Create role condition<\/p>\n<p>if (roleNames!= null)<\/p>\n<p>{<\/p>\n<p>foreach (string role in roleNames)<\/p>\n<p>{<\/p>\n<p>condition = condition + $&#8221;&lt;condition attribute = &#8216;name&#8217; operator= &#8216;eq&#8217; value='{role}&#8217;\/&gt;&#8221;;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>string fetchXML = string.Empty;<\/p>\n<p>fetchXML = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml-platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;entity name=&#8217;role&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;roleid&#8217;\/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;filter type=&#8217;or&#8217;&gt;&#8221; + condition + &#8220;&lt;\/filter&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/entity&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/fetch&gt;&#8221;;<\/p>\n<p>var query = new FetchExpression(fetchXML);<\/p>\n<p>EntityCollection roles = new EntityCollection();<\/p>\n<p>\/\/Collection of Role<\/p>\n<p>roles = _service.RetrieveMultiple(query);<\/p>\n<ul>\n<li>Check weather display condition exists or not. <strong>If not<\/strong> then create new display condition and update it otherwise, just update the existing display condition.<\/li>\n<\/ul>\n<p>\/\/Check DispalyConditon Exist or not<\/p>\n<p>if (displayCondition != null)<\/p>\n<p>{<\/p>\n<p>displayConditionAttribute = formXML.Descendants(&#8220;DisplayConditions&#8221;).FirstOrDefault();<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>\/\/create display condition<\/p>\n<p>displayConditionAttribute = new XElement(&#8220;DisplayConditions&#8221;);<\/p>\n<p>\/\/to set a form as follbackform<\/p>\n<p>displayConditionAttribute.Add(new XAttribute(&#8220;FallbackForm&#8221;, true));<\/p>\n<p>\/\/set form order<\/p>\n<p>displayConditionAttribute.Add(new XAttribute(&#8220;Order&#8221;, &#8220;1&#8221;));<\/p>\n<p>XElement form = formXML.Descendants(&#8220;form&#8221;).FirstOrDefault();<\/p>\n<p>\/\/add display condition on form element<\/p>\n<p>form.LastNode.AddAfterSelf(displayConditionAttribute);<\/p>\n<p>\/\/get display condition on form<\/p>\n<p>displayConditionAttribute = formXML.Descendants(&#8220;DisplayConditions&#8221;).FirstOrDefault();<\/p>\n<p>}<\/p>\n<p>\/\/specify that the form is visible to everyone<\/p>\n<p>XElement Everyone = new XElement(&#8220;Everyone&#8221;);<\/p>\n<p>\/\/add Everyone element into displaycondition<\/p>\n<p>displayConditionAttribute.Add(Everyone);<\/p>\n<blockquote><p><strong>Note: <\/strong>If you want to display Form to everyone then use the above code. <strong>If not<\/strong> then use the below code.<\/p><\/blockquote>\n<p>\/\/specify that the security role to view a form<\/p>\n<p>if (roles != null &amp;&amp; roles.Entities.Count &gt; 0)<\/p>\n<p>{<\/p>\n<p>foreach (Entity role in roles.Entities)<\/p>\n<p>{<\/p>\n<p>string roleId = &#8220;{&#8221; + role.GetAttributeValue&lt;Guid&gt;(&#8220;roleid&#8221;).ToString() + &#8220;}&#8221;;<\/p>\n<p>XElement Role = new XElement(&#8220;Role&#8221;);<\/p>\n<p>Role.Add(new XAttribute(&#8220;Id&#8221;, roleId));<\/p>\n<p>\/\/add Role element into displaycondition<\/p>\n<p>displayConditionAttribute.Add(Role);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/Update form XML<\/p>\n<p>forms.Entities[0][&#8220;formxml&#8221;] = formXML.ToString(SaveOptions.DisableFormatting);<\/p>\n<p>\/\/Update Form<\/p>\n<p>_service.Update(forms.Entities[0]);<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/2016\/10\/programmatically-publish-customizations-in-dynamics-crm\/\" target=\"_blank\" rel=\"noopener noreferrer\">Publish customization<\/a> programmatically after updating Form.<\/p>\n<p>You can use the above code in plugin\/assembly and any other application.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We can assign one or more security role for more than one CRM Form programmatically.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-pack\/user-adoption-monitor-in-dynamics-crm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-19145\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/06\/Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM.png\" alt=\"Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/06\/Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/06\/Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/06\/Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/06\/Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM-660x165.png 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In Dynamics 365 we can update security role of Form through customization. You have to just follow the given steps: Go to Setting \u2192 Customization \u2192 Customize the System \u2192 Components \u2192 Entities \u2192\u00a0Forms Open Form and click on \u201cEnable Security Roles\u201d in Home tab to Assign Security Role to selected Form. However, in\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/06\/how-to-assign-security-role-to-a-form-programmatically-in-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":23750,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[13,16,18,19,51],"tags":[],"class_list":["post-19269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizations","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm","category-security"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/19269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=19269"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/19269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/23750"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=19269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=19269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=19269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}