{"id":12230,"date":"2018-07-06T15:42:37","date_gmt":"2018-07-06T10:12:37","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=12230"},"modified":"2022-07-01T14:30:51","modified_gmt":"2022-07-01T09:00:51","slug":"is-it-possible-to-createupdate-application-user-in-dynamics-365-programmatically","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2018\/07\/is-it-possible-to-createupdate-application-user-in-dynamics-365-programmatically\/","title":{"rendered":"Is it possible to Create\/Update Application User in Dynamics 365 Programmatically?"},"content":{"rendered":"<p style=\"text-align: justify;\">You may have heard about the Application user in Dynamics 365. The Application user is a non-licensed and non-interactive user in the Dynamics 365 that we can use to connect to Dynamics 365 services to perform operations.<\/p>\n<p style=\"text-align: justify;\">Have you come across a requirement where you need to create\/update Application User programmatically and do you think it is possible?<\/p>\n<p style=\"text-align: justify;\">Recently, we had a requirement where we had to create an Application User in Dynamics 365 programmatically. And, after researching it, we came to know that it is possible!<\/p>\n<p style=\"text-align: justify;\">In this blog, we will illustrate how we can create\/update the Application user programmatically.<\/p>\n<h2 style=\"text-align: justify;\"><strong>Programmatically Creating or Updating Application User in Dynamics 365<\/strong><\/h2>\n<p style=\"text-align: justify;\">While creating an Application User from Application Form manually, we have to pass the below mandatory fields.<\/p>\n<ol style=\"text-align: justify;\">\n<li>First Name<\/li>\n<li>Last Name<\/li>\n<li>Primary Email<\/li>\n<li>Application ID (that we get from Azure AD)<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">After you click on save button, the Application ID URI, Azure AD Object ID fields get auto-populated.<\/p>\n<p style=\"text-align: justify;\">If you try to do the same thing using either JavaScript (Web API) or C# code (bypassing above mentioned four fields to entity object to create), you will get the following error,<\/p>\n<blockquote>\n<p style=\"text-align: justify;\"><strong><em>\u201cAn unexpected error occurred\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<p style=\"text-align: justify;\">While creating an Application User, we must pass the Business Unit ID field along with the fields mentioned above.<\/p>\n<p style=\"text-align: justify;\">So to create an Application User, we must pass following fields to an entity object,<\/p>\n<ol style=\"text-align: justify;\">\n<li>First Name<\/li>\n<li>Last Name<\/li>\n<li>Primary Email<\/li>\n<li>Application ID (that we get from Azure AD)<\/li>\n<li>Business Unit ID<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Below is the sample code that shows how to create Application User using Web API at the client side.<\/p>\n<pre class=\"lang:default decode:true \">function createApplicationUser() {\r\n    try {\r\n\r\nvar apiconfig = { APIUrl: Xrm.Utility.getGlobalContext().getClientUrl() + '\/api\/data\/v9.0\/' };\r\n        var crmAPI = new CRMWebAPI(apiconfig);\r\n\r\n        \/\/retrieve business unit\r\n        var buQueryOpions = {\r\n            Select: [\"businessunitid\", \"name\"],\r\n            Filter: \"_parentbusinessunitid_value eq null\"\r\n        };\r\n\r\n        \/\/retrive Business Units\r\n        crmAPI.GetList(\"businessunits\", buQueryOpions).then(function (response) {\r\n            if (response != null &amp;&amp; response != undefined &amp;&amp; response.List != null &amp;&amp; response.List != undefined &amp;&amp; response.List.length &gt; 0) {\r\n\r\n                \/\/set Business Unit ID\r\n                var businessUnitId = response.List[0].businessunitid;\r\n\r\n                \/\/create Application User\r\n                createApplicationuser(businessUnitId, crmAPI);\r\n            }\r\n            else {\r\n                alert(\"Root Business Unit not found\");\r\n            }\r\n        }, function (error) {\r\n            alert(err.responseText);\r\n        });\r\n    } catch (e) {\r\n        throwError(e, functionName);\r\n    }\r\n}\r\n\r\n\r\n\/\/ This function create or update the Application User\r\nfunction createApplicationuser(businessUnitId, crmAPI) {\r\n    try {\r\n        \/\/check if application user is already exist\r\n        var queryOptions = {\r\n            Select: ['systemuserid', 'applicationid'],\r\n            Filter: 'applicationid eq XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'\r\n        };\r\n        var applicationUser = null;\r\n\r\n        \/\/retrive Applicationuser with Application ID\r\n        crmAPI.GetList(\"systemusers\", queryOptions).then(function (response) {\r\n\r\n            \/\/if Application User record is present then Update\r\n            if (response != null &amp;&amp; response != undefined &amp;&amp; response.List != null &amp;&amp; response.List != undefined &amp;&amp; response.List.length &gt; 0) {\r\n\r\n                \/\/set the Application User\r\n                applicationUser = response.List[0];\r\n\r\n                \/\/crete user data object\r\n                var user = {\r\n                    \"applicationid\": \"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX\",\r\n                    \"firstname\": \"MultiTenant\",\r\n                    \"lastname\": \"Portal3\",\r\n                    \"internalemailaddress\": \"multitenantportal@test.com\"\r\n                };\r\n\r\n                \/\/update application user\r\n                crmAPI.Update(\"systemusers\", applicationUser.systemuserid, user, true).then(function (result) {\r\n                    alert(\"Application user updated with new details.\");\r\n                }, function (err) { alert(err.responseText);});\r\n            }\r\n                \/\/check if Application User record is not present then Crete \r\n            else {\r\n                \/\/create user data object \r\n                var user = {\r\n                    \"applicationid\": \"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX\",\r\n                    \"firstname\": \"MultiTenant\",\r\n                    \"lastname\": \"Portal3\",\r\n                    \"internalemailaddress\": \"multitenantportal@test.com\",\r\n                    \"businessunitid@odata.bind\": \"\/businessunits(\" + businessUnitId + \")\"\r\n                };\r\n                \/\/create application user\r\n                crmAPI.Create(\"systemusers\", user).then(function (result) {\r\n                    alert(\"Application user created.\");\r\n                }, function (err) { alert(err.responseText);});\r\n            }\r\n\r\n        }, function (error) {\r\n            alert(error);\r\n        });\r\n    } catch (error) {\r\n        alert(error.message || error.description);\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Here, we first retrieved the Business Unit ID and then we passed this Business Unit ID to create the Application User along with other fields.<\/p>\n<p style=\"text-align: justify;\">You can see that we are checking whether an Application User already exists with matching Application ID in the System. If matching Application User is found, then we are updating existing Application User, else we create a new Application user.<\/p>\n<h2 style=\"text-align: justify;\"><strong>Conclusion:<\/strong><\/h2>\n<p style=\"text-align: justify;\">It is possible to create Application user either at client side (using JavaScript) or server side (using plugin, workflow assembly or external tools). We need to make sure that we are passing all the required fields, i.e. First name, Last Name, Email Address, Application ID and Business Unit ID.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Free 70% of storage space in CRM with Attachment Management Apps!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/attach-2-dynamics-365-crm-upload-multiple-files-sharepoint-cloud-storage\" target=\"_blank\" rel=\"noopener noreferrer\">Attach2Dynamics<\/a> &#8211; Store and manage documents\/attachments in cloud storage of your choice &#8211; SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.<\/strong><\/em><br \/>\n<em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\">SharePoint Security Sync<\/a><\/strong> \u2013 Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may have heard about the Application user in Dynamics 365. The Application user is a non-licensed and non-interactive user in the Dynamics 365 that we can use to connect to Dynamics 365 services to perform operations. Have you come across a requirement where you need to create\/update Application User programmatically and do you think\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2018\/07\/is-it-possible-to-createupdate-application-user-in-dynamics-365-programmatically\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":12232,"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],"tags":[140],"class_list":["post-12230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizations","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm","tag-application-user-in-dynamics-365"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/12230","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=12230"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/12230\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/12232"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=12230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=12230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=12230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}