Hi,
I have CRM 2011 IFD but need to use old MSCRMService temporarily.
I have learnt that we need to get a token as if we try to use the endpoint /2007/...asmx we get a 401 error.
Can someone please show me some code that works? I have spent all day trying to find this code.
I am using... but still get 401 error?
using System;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 System.Net;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Win32;
using MSCRMService;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = "xxxx";
string Password = "xxxxxx";
string Domain = "HANEN";
string orgName = "crm-test";
string crmServerURL = "https://crm-test.<server>:4444";
CrmService service = null;
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Credentials = new NetworkCredential(UserName, Password, Domain);
disco.Url = String.Concat(crmServerURL, "/MSCrmServices/2007/CrmService.asmx");
RetrieveOrganizationsRequest OrgRequest = new RetrieveOrganizationsRequest();
OrgRequest.Password = Password;
OrgRequest.UserId = Domain + "\\" + UserName;
RetrieveOrganizationsResponse OrgResponse = (RetrieveOrganizationsResponse)disco.Execute(OrgRequest);
foreach (OrganizationDetail OrgDetail in OrgResponse.OrganizationDetails)
{
if (OrgDetail.OrganizationName.ToLower().Equals(orgName))
{
//Retrieve the ticket.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = OrgDetail.OrganizationName;
ticketRequest.UserId = Domain + "\\" + UserName;
ticketRequest.Password = Password;
RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
// Setup the Authentication Token
CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(HttpContext.Current,OrgDetail.OrganizationName);
token.OrganizationName = OrgDetail.OrganizationName;
token.AuthenticationType = AuthenticationType.Spla; //2
token.CrmTicket = ticketResponse.CrmTicket;
//Setup Service
service = new CrmService();
service.Url = OrgDetail.CrmServiceUrl;
service.UseDefaultCredentials = true;
service.CrmAuthenticationTokenValue = token;
//Create the lead object as usual.
lead lead = new lead();
lead.subject = "Lorem";
lead.firstname = "John";
lead.lastname = "Smith";
lead.companyname = "Ipsum";
//Assign the owner as the caller ID from the token.
//If you don't do this, the owner will be SYSTEM.
lead.ownerid = new Owner();
lead.ownerid.type = EntityName.systemuser.ToString();
lead.ownerid.Value = token.CallerId;
//Create the lead on Skype.
Guid leadid = service.Create(lead);
//Display the GUID.
Label1.Text = leadid.ToString();
}
}
}
}