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 Problem Issue Error Dymanic Crm Error 401 Unauthorized When Create Or Retrieve Account And Contact

Dymanic Crm Error 401 Unauthorized When Create Or Retrieve Account And Contact

If you are facing with an error message “Exception Details: System.Net.WebException: The request failed with HTTP status 401: Unauthorized.” when use Microsoft Dynamic CRM Web Service to Create a new Account/ Contact or Retrieve information from a particluar Account/ Contact via a GUID.

Dynamic CRM Unauthorized 401

Dynamic CRM Unauthorized 401

Below is C# script that caused the issue, it try to create an account with some information and then display it:

[csharp highlight=”29″] using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using CRM;

public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "your_organization_name";

CrmService service = new CrmService();
service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;

service.Credentials = System.Net.CredentialCache.DefaultCredentials;

account newAccount = new account();
newAccount.name = "Hoan Huynh";
newAccount.accountnumber = "123456";
newAccount.address1_postalcode = "98052";
newAccount.address1_city = "HCM";
newAccount.emailaddress1 = "[email protected]";
newAccount.address1_telephone1 = "0909345541";
newAccount.description = "description";
newAccount.telephone1 = "0909345541";

Guid accountId = service.Create(newAccount);

Response.Write("accountId: " + accountId.ToString() + "<br>");

AllColumns ac = new AllColumns();

account account = (account)service.Retrieve(EntityName.account.ToString(), accountId, ac);

Response.Write("name: " + account.name.ToString() + "<br>");
}
}
[/csharp]

Because you’re using the default network credentials but the user account has not been added to PrivUserGroup so you must explicitly specify the network credentials of an account that is in PrivUserGroup. Substitute the appropriate logon information for the user name, password, and domain.

Replace the default network credentials above on line 29 with the following example contains sample data for the logon name and password.

service.Credentials = new System.Net.NetworkCredential(“username”, “password”, “domainname”);

The C# code above will become:

[csharp highlight=”30″] using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using CRM;

public partial class Test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "your_organization_name";

CrmService service = new CrmService();
service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;

//service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Credentials = new System.Net.NetworkCredential("your_username", "your_password", "");

account newAccount = new account();
newAccount.name = "Test Again";
newAccount.accountnumber = "123456";
newAccount.address1_postalcode = "98052";
newAccount.address1_city = "HCM";
newAccount.emailaddress1 = "[email protected]";
newAccount.address1_telephone1 = "0909345541";
newAccount.description = "description";
newAccount.telephone1 = "0909345541";

Guid accountId = service.Create(newAccount);

Response.Write("accountId: " + accountId.ToString() + "<br>");

AllColumns ac = new AllColumns();

account account = (account)service.Retrieve(EntityName.account.ToString(), accountId, ac);

Response.Write("name: " + account.name.ToString() + "<br>");
}
}
[/csharp]

Note: please replace your_organization_name, your_username, your_password by your logon information.

Apr 1, 2011Hoan Huynh
Delete, Shrink, Eliminate Transaction Log .LDF FileGenerate And Display Related Content On Wordpress
You Might Also Like:
  • Create And Assign Account To A User In Microsoft Dynamic CRM With C#
  • Create Account With Microsoft Dynamics CRM Web Services By C#
  • Retrieve Account With Microsoft Dynamics CRM Web Services By C#
  • Create CrmService with Domain, Username And Password
  • sphinx install error access is denied
  • Application pool ‘abc’ suffered a fatal communication error with the Windows Process Activation Service
  • Asp.net C# Create MD5 Hash
  • Change or reset MySQL Root Account Password
  • Property or indexer ‘Microsoft.Crm.Sdk.Query.ColumnSet.Attributes’ cannot be assigned to – it is read only
  • PHP Login Or Sign In With Google Account By OpenID
Hoan Huynh

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

10 years ago Problem Issue ErrorCredentials, CrmService, CrmService.Retrieve, Microsoft Dynamics CRM, PrivUserGroup458
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
22,210 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,078 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,854 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,335 views
C# Read Json From URL And Parse/Deserialize Json
9,831 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
  • Photo Editor App – How Good Is it?

  • The Best Way To Write An Essay – Creating A Good Essay
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

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