Answered by:
CRM 4.0: Develop Web Services

Question
-
Good night.
I am a student at the University of Minho, Degree in Information Systems andTechnologies.
As part of a course I'm working on the integration of CRM 4.0 with other ERPs.
For this I need to publish and develop Web services to integrate the BizAgi.
Except that I do not know how to publish them or how to develop them.
Will someone help me?
(excuse my English)Thursday, June 2, 2011 10:33 PM
Answers
-
First off, Does your name relate at all to Tristen De Cunha. I have seen images, it's a beautiful (but terribly remote) island.
You need to download the CRM 4.0 SDK. It has examples of plugin development and documentation in the included .CHM file.
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 2:12 PM
- Marked as answer by Jim Glass Jr Friday, June 3, 2011 5:19 PM
Friday, June 3, 2011 2:12 PMModerator -
Hi,
I would suggest you to reffer the following links for step by step guide to develop applicaiton asp.net (In you case it will be ERP that will be consuming CRM web service) using CRM web service.
http://www.codeproject.com/KB/applications/DynamicsCrmPart1.aspx
http://msdn.microsoft.com/en-us/library/dd393301.aspx
I hope this answers your quesiton.
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 5:12 PM
- Marked as answer by Jim Glass Jr Friday, June 3, 2011 5:19 PM
Friday, June 3, 2011 5:12 PM
All replies
-
I've added the reference to Visual Studio. I created an item of type Web Service and putthis code. Am I on track?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using OMeuSite.CrmSdk.Discovery;
namespace OMeuSite
{
[WebService(Namespace = "http://maria.edu/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public void Account(String nome, string cidade, string mail ) {
account account = new account();
account.name = nome; //use data from your form
account.address1_country=cidade;
account.emailaddress1 = mail;
// Create the target object for the request.
TargetCreateAccount target = new TargetCreateAccount();
target.Account = account;
// Create the request object.
CreateRequest createRequest = new CreateRequest();
createRequest.Target = target;
//Execute the request.
CreateResponse createResponse = (CreateResponse)service.Execute(createRequest);
Guid accountID = createResponse.id;
}
}
}
Best regards, Maria Cunha.Friday, June 3, 2011 12:35 AM -
First off, Does your name relate at all to Tristen De Cunha. I have seen images, it's a beautiful (but terribly remote) island.
You need to download the CRM 4.0 SDK. It has examples of plugin development and documentation in the included .CHM file.
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 2:12 PM
- Marked as answer by Jim Glass Jr Friday, June 3, 2011 5:19 PM
Friday, June 3, 2011 2:12 PMModerator -
I'm from Portugal, but not anything I cared to visit Tristen Wedge.
Thanks for the tip of the SDK. I've done the download, and to develop web services can i use the code it is available?My difficulty is to understand what steps do I take to develop web services.
Best regards, Maria Cunha.Friday, June 3, 2011 4:02 PM -
Hi,
I would suggest you to reffer the following links for step by step guide to develop applicaiton asp.net (In you case it will be ERP that will be consuming CRM web service) using CRM web service.
http://www.codeproject.com/KB/applications/DynamicsCrmPart1.aspx
http://msdn.microsoft.com/en-us/library/dd393301.aspx
I hope this answers your quesiton.
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 5:12 PM
- Marked as answer by Jim Glass Jr Friday, June 3, 2011 5:19 PM
Friday, June 3, 2011 5:12 PM -
Thanks again for the links.
I went to seek information from one or the other side and development this :using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk;
using System.Configuration;
using System.Net;
using System.Security;
namespace OMeuSite
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
/// Summary description for CrmServiceManager
public static class CrmServiceManager
{
public static CrmService GetCrmService()
{
String organizationName = "ISI-EquipaB2";
String serverUrl = "http://win-0jgk5ro1cut:5555";
String userName = "admin@isi.local";
String password = "password";
String domain = "domain";
//Authentication for V1.0 is for AD authentication only.
CrmService service = new CrmService();
service.Url = String.Format("{0}/2007/CrmService.asmx", serverUrl);
service.CrmAuthenticationTokenValue = GetCrmAuthenticationToken(organizationName);
service.Credentials = new NetworkCredential(userName, password, domain);
return service;
}
public static MetadataService GetMetadataService()
{
//Read the web.config to get the relevant settings to create an instance
//of the Metadata Web Service.
String organizationName = "ISI-EquipaB2";
String serverUrl = "http://win-0jgk5ro1cut:5555";
String userName = "Administrador";
String password = "pass";
String domain = "domain";
MetadataService service = new MetadataService();
service.Url = String.Format("{0}/2007/MetadataService.asmx", serverUrl);
service.CrmAuthenticationTokenValue = GetCrmAuthenticationToken(organizationName);
service.Credentials = new NetworkCredential(userName, password, domain);
return service;
}
private static CrmAuthenticationToken GetCrmAuthenticationToken(String organizationName)
{
//Authentication for V1.0 is for AD authentication only.
CrmAuthenticationToken authToken = new CrmAuthenticationToken();
authToken.AuthenticationType = AuthenticationType.AD;
authToken.OrganizationName = organizationName;
return authToken;
}
}
public class WebService2 : System.Web.Services.WebService
{
[WebMethod]
public void Teste_Conta(String nome, string cidade, string mail)
{
account account = new account();
account.name = nome; //use data from your form
account.address1_country = cidade;
account.emailaddress1 = mail;
// Create the target object for the request.
TargetCreateAccount target = new TargetCreateAccount();
target.Account = account;
// Create the request object.
CreateRequest createRequest = new CreateRequest();
createRequest.Target = target;
//Execute the request.
CreateResponse createResponse = (CreateResponse)service.Execute(createRequest);
Guid accountID = createResponse.id;
}
}
}
Now it is making a mistake in the last line of code, EXECUTE.
Anyone know why?
Best regards, Maria Cunha.Friday, June 3, 2011 7:12 PM -
Hi,
This is fine CRM service execute method process the request into CRM i.e. Create Record, Retrieve Record, Delete Record, Assosiate Record etc. for creating a record you may also use service.create method as explained in the following ASP.Net code.
TargetCreateAccount represents the abstract base class for messages regarding compound entities and their related detail entities. Therefore you need a source entity to create target. Where as create can be used with out any source entity. It was the way of creating a record in CRM 3.0 and 1.2 http://msdn.microsoft.com/en-us/library/aa684594.aspx
You can also use late bound class DynamicsEntity in Crm 4.0 for performing entity crete, update,retrieve, delete operations.
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;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e)
{
//Retrieve the organization name from the query string.
string orgname = Request.QueryString["orgname"].ToString();
//Wrap the CRM Web Service Code in a using block.
using (new CrmImpersonator())
{
//Create a token with the static method. ExtractCrmAuthenticationToken
//The 'Context' used here is the Page.Context.
CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
CrmService service = new CrmService();
service.CrmAuthenticationTokenValue = token;
service.Credentials = CredentialCache.DefaultCredentials;
//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;
lead.ownerid.Value = token.CallerId;
//Create the lead on Skype.
Guid leadid = service.Create(lead);
}
//Display the GUID.
Response.Write(leadid.ToString());
}
}I hope this answers your questions.
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 7:29 PM
Friday, June 3, 2011 7:21 PM -
Here is a create request example from my blog that is for .NET and the same page has one for jscript also for client side usage. It's 2011 but the syntax should be pretty close for the .NET portion. It uses the create method instead of execute though.
http://mileyja.blogspot.com/2011/04/create-requests-in-net-and-jscript-in.html
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!Friday, June 3, 2011 7:29 PMModerator -
I would also maybe consider opening a new question on this secondary question as a new unanswered question will draw more attention if neither of these answers work for you.
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!Friday, June 3, 2011 7:30 PMModerator -
Thanks, but now i have this error:
Cannot implicitly convert type 'Microsoft.Crm.SdkTypeProxy.EntityName' to 'string' in lead.ownerid.type = EntityName.systemuser;
Best regards, Maria Cunha.Friday, June 3, 2011 7:33 PM -
Hi,
You should use it like this:
EntityName.systemuser.ToString();
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 7:34 PM
- Edited by Jehanzeb.Javeed Friday, June 3, 2011 7:37 PM
Friday, June 3, 2011 7:34 PM -
Capital T on ToString(); In jscript it would be .toString(); with a lowercase t.
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 7:36 PM
Friday, June 3, 2011 7:36 PMModerator -
I put EntityName.systemuser.ToString(); but the error now is
Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?
Best regards, Maria Cunha.Friday, June 3, 2011 7:55 PM -
Hi,
You can use the statement like this:
lead.ownerid.type = "systemuser";
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 8:00 PM
Friday, June 3, 2011 7:59 PM -
thanks, and sorry for so much work.
After thisGuid leadid = service.Create(lead);
we areResponse.Write(leadid.ToString());
but give me like not exists.
Best regards, Maria Cunha.Friday, June 3, 2011 8:03 PM -
Hi,
Write the statement like this:
//Create the lead on Skype.
Guid leadid = service.Create(lead);//Display the GUID.
Response.Write(leadid.ToString());}
The final code will be:
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;
public partial class _Default : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e)
{
//Retrieve the organization name from the query string.
string orgname = Request.QueryString["orgname"].ToString();
//Wrap the CRM Web Service Code in a using block.
using (new CrmImpersonator())
{
//Create a token with the static method. ExtractCrmAuthenticationToken
//The 'Context' used here is the Page.Context.
CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
CrmService service =new CrmService();
service.CrmAuthenticationTokenValue = token;
service.Credentials = CredentialCache.DefaultCredentials;
//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 = "systemuser";
lead.ownerid.Value = token.CallerId;
//Create the lead on Skype.
Guid leadid = service.Create(lead);//Display the GUID.
Response.Write(leadid.ToString());}
}
}
Thank You,
Jehanzeb Javeed,
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex ProfileIf you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
- Proposed as answer by Jehanzeb.Javeed Friday, June 3, 2011 8:07 PM
Friday, June 3, 2011 8:07 PM -
ok.
Now the code doesn't have error, but how can i use this like a web service?
Best regards, Maria Cunha.Friday, June 3, 2011 8:18 PM -
You wrap it up in a web method. In visual studio right click on the project and pick add item, then click web service.
You will get something like this, basically you wrap your code up in a web method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication4
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 8:25 PM
Friday, June 3, 2011 8:25 PMModerator -
SOMETHING LIKE THIS???
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Configuration; using System.Data; 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 System.Text; namespace OMeuSite { /// <summary> /// Summary description for WebService4 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class WebService4 : System.Web.Services.WebService { [WebMethod] public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Retrieve the organization name from the query string. string orgname = Request.QueryString["ISI-EquipaB2"].ToString(); //Wrap the CRM Web Service Code in a using block. using (new CrmImpersonator()) { //Create a token with the static method. ExtractCrmAuthenticationToken //The 'Context' used here is the Page.Context. CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname); CrmService service = new CrmService(); service.CrmAuthenticationTokenValue = token; service.Credentials = CredentialCache.DefaultCredentials; //Create the lead object as usual. lead lead = new lead(); lead.subject = "Lorem"; lead.firstname = "Maria"; lead.lastname = "Cunha"; lead.companyname = "ISI-EquipaB2"; //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 = "systemuser"; lead.ownerid.Value = token.CallerId; //Create the lead on Skype. Guid leadid = service.Create(lead); //Display the GUID. Response.Write(leadid.ToString()); } } } } }
Best regards, Maria Cunha.Friday, June 3, 2011 8:30 PM -
more like this!
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Configuration; using System.Data; 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 System.Text; namespace OMeuSite { /// <summary> /// Summary description for WebService4 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class WebService4 : System.Web.Services.WebService { [WebMethod] public bool runme() { //Retrieve the organization name from the query string. string orgname = Request.QueryString["ISI-EquipaB2"].ToString(); //Wrap the CRM Web Service Code in a using block. using (new CrmImpersonator()) { //Create a token with the static method. ExtractCrmAuthenticationToken //The 'Context' used here is the Page.Context. CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname); CrmService service = new CrmService(); service.CrmAuthenticationTokenValue = token; service.Credentials = CredentialCache.DefaultCredentials; //Create the lead object as usual. lead lead = new lead(); lead.subject = "Lorem"; lead.firstname = "Maria"; lead.lastname = "Cunha"; lead.companyname = "ISI-EquipaB2"; //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 = "systemuser"; lead.ownerid.Value = token.CallerId; //Create the lead on Skype. Guid leadid = service.Create(lead); //Display the GUID. Response.Write(leadid.ToString()); } } } }
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 8:34 PM
Friday, June 3, 2011 8:33 PMModerator -
Now, don't do this:
Response.write(leadid.ToString());
Don't recognizes WRITE..
Best regards, Maria Cunha.Friday, June 3, 2011 8:43 PM -
change the method to return a string instead and use that.
[WebMethod] public stringrunme()
then you can return the lead id like this
change this:
Response.Write(leadid.ToString());
to this:
return leadid.ToString();
Jamie Miley
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Friday, June 3, 2011 8:47 PM
Friday, June 3, 2011 8:47 PMModerator -
Thank You.
I ran this code, he created the web service, but when I invoke the code gives an error
using the (new CRMImpersonator ())
says "Invalid Operation Exception was unhandled by user code"
Best regards, Maria Cunha.Friday, June 3, 2011 9:50 PM