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.
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.