1
votes

I'm trying to retrieve a list of contacts from CRM 4.0 but I'm having problems authenticating. From what I've read, the method of listing all clinets is to parse the XML returned from an ExportAllXmlRequest

The problem is, when I browse to http://crmserver/MSCRMServices/2007/spla/CRMDiscoveryService.asmx, I get a 401.2 unauthorized and no prompt to authenticate. If I add the web reference via visual studio, I get a password prompt which doesn't accept my credentials. I've also tried authenticating in code with the following, but neither work. Windows Authentication and Anonymous Authentication are both enabled on the server.

CrmService svc = new CrmService();
// this doesn't work
svc.UseDefaultCredentials = true;

// this doesn't work either
svc.Credentials = new NetworkCredential("myuser", "password", "mydomain");
svc.Credentials= System.Net.CredentialCache.DefaultCredentials;
ExportAllXmlRequest request = new ExportAllXmlRequest();

ExportAllXmlResponse response = (ExportAllXmlResponse)svc.Execute(request);
string resp=string.Empty;
using (StreamReader reader = new StreamReader(response.ExportXml)) {
    resp = reader.ReadToEnd();
}
return resp;

Is there 1) An easier method of listing contacts from CRM and 2) something I can do to fix this authentication issue.

3

3 Answers

2
votes

Your code is not correct. You are mixing the Metadata Service and the CRM Service.

For interacting with the CRM data, you should use the CRM Service. The Discovery Service is something like the yellow pages for the CRM deployment.

// Set up the CRM Service.
var token = new CrmAuthenticationToken();
token.AuthenticationType = 0; 
token.OrganizationName = "{yourorgname}";

var service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

This creates the connection to the CRM Service. To retrieve data you could use the RetrieveMultiple method of the CrmService.

 // Create the QueryExpression object.
 var query = new QueryExpression();

 // Set the properties of the QueryExpression object.
 query.EntityName = EntityName.contact.ToString();
 query.ColumnSet = new AllColumns();

 // Retrieve the contacts.
 BusinessEntityCollection contacts = service.RetrieveMultiple(query);
1
votes

This is definitely not the way you need to retrieve entity records. An ExportAllXmlRequest will pull the customizations information for your CRM environment, but will have no actual records. You'll want the RetrieveMultipleRequest (sdk link: http://msdn.microsoft.com/en-us/library/bb929303.aspx).

As to the authentication issue, is your CRM site in your list of trusted or intranet sites in IE?

0
votes

ckeller's answer above is good if you are working within a plugin or in the CRM Online version where you don't have access to the underlying SQL database. If this is in an on-premise installation and you have SQL server access to the CRM tables, it is often faster and more straightforward to query the SQL tables or views directly to read entity information. You would just need to execute the following SQL query and read out the results with a SQL data reader or datatable -

SELECT * FROM OrgName_MSCRM.dbo.FilteredContact