Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp Create And Assign Account To A User In Microsoft Dynamic CRM With C#

Create And Assign Account To A User In Microsoft Dynamic CRM With C#

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;

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", "");

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);

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);

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);
    }
}

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.

May 13, 2011Hoan Huynh
Validate Email Address Format Using PHP Regular Expression preg_matchManage Or Organize WordPress Media Library Structure In Categories
You Might Also Like:
  • Create Account With Microsoft Dynamics CRM Web Services By C#
  • Dymanic Crm Error 401 Unauthorized When Create Or Retrieve Account And Contact
  • Retrieve Account With Microsoft Dynamics CRM Web Services By C#
  • Create CrmService with Domain, Username And Password
  • Asp.net C# Create MD5 Hash
  • Property or indexer ‘Microsoft.Crm.Sdk.Query.FilterExpression.Conditions’ cannot be assigned to – it is read only
  • Property or indexer ‘Microsoft.Crm.Sdk.Query.ColumnSet.Attributes’ cannot be assigned to – it is read only
  • Resolve Could not load file or assembly Microsoft.VisualStudio.Web.Runtime
  • Create Free SSL Certificate For Testing On Windows Server 2003 IIS 6
  • Change or reset MySQL Root Account Password
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago CSharpAssignRequest, AssignResponse, CrmService, Microsoft Dynamic CRM, NetworkCredential, SecurityPrincipal, TargetOwnedAccount250
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,459 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,656 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,998 views
C# Read Json From URL And Parse/Deserialize Json
11,723 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development