An ASP.NET page below is written in C# will show you step by step on creating a CrmService web service instance with a NetworkCredential includes username/password and create an Account with some basic information such as name, email, phone then assign the new account to a new security principal (user).
First, please make sure you’ve downloaded the Microsoft Dynamic CRM SDK and added all CrmDiscoveryService Web Reference DLL.
1. Using namespace for using Microsoft Dynamic CRM DLL
using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Query; using Microsoft.Crm.SdkTypeProxy; using CRM; |
2. Create a CrmService instance with Username and Password
CrmAuthenticationToken token = new CrmAuthenticationToken(); token.AuthenticationType = 0; token.OrganizationName = "_Your_OrganizationName_"; CrmService service = new CrmService(); service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx"; service.CrmAuthenticationTokenValue = token; service.Credentials = new System.Net.NetworkCredential("username", "password", ""); |
3. Create an account entity and assign data to some attributes.
account newAccount = new account(); newAccount.name = "Hoan Huynh"; newAccount.emailaddress1 = "[email protected]"; newAccount.description = "Enter some messages here ..."; newAccount.telephone1 = "0909345541"; Guid accountId = service.Create(newAccount); |
4. Assign the new account to a new security principal (user).
You have to know the GUID belonging to the user that will own the new account. In this example, I assume it’s 2AF213B6-D222-E011-B10C-E61F13C072B3.
SecurityPrincipal assignee = new SecurityPrincipal(); assignee.Type = SecurityPrincipalType.User; assignee.PrincipalId = new Guid("2AF213B6-D222-E011-B10C-E61F13C072B3"); TargetOwnedAccount target = new TargetOwnedAccount(); target.EntityId = accountId; AssignRequest assign = new AssignRequest(); assign.Assignee = assignee; assign.Target = target; AssignResponse assignResponse = (AssignResponse)service.Execute(assign); |
5. That’s all. Combine together
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Query; using Microsoft.Crm.SdkTypeProxy; using CRM; public partial class CreateAccountAndAssignUser : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CrmAuthenticationToken token = new CrmAuthenticationToken(); token.AuthenticationType = 0; token.OrganizationName = "_Your_OrganizationName_"; CrmService service = new CrmService(); service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx"; service.CrmAuthenticationTokenValue = token; service.Credentials = new System.Net.NetworkCredential("username", "password", ""); // Create an account entity and assign data to some attributes. account newAccount = new account(); newAccount.name = "Hoan Huynh"; newAccount.emailaddress1 = "[email protected]"; newAccount.description = "Enter some messages here ..."; newAccount.telephone1 = "0909345541"; // Call the Create method to create an account. Guid accountId = service.Create(newAccount); Response.Write("accountId: " + accountId.ToString() + "<br>"); SecurityPrincipal assignee = new SecurityPrincipal(); assignee.Type = SecurityPrincipalType.User; assignee.PrincipalId = new Guid("2AF213B6-D222-E011-B10C-E61F13C072B3"); TargetOwnedAccount target = new TargetOwnedAccount(); target.EntityId = accountId; AssignRequest assign = new AssignRequest(); assign.Assignee = assignee; assign.Target = target; AssignResponse assignResponse = (AssignResponse)service.Execute(assign); } } |
Note: please replace the _Your_OrganizationName_, username, password and URL with your information.