Pages

Tuesday 11 March 2014

Connecting CRM to Dot NET Application

Here I am Creating  a WindowsForm Application which is conneced to CRM.
So I'll Create a new Solution in Visual Studio of Windows Application Type. Now Right click on that solution and Click on the properties. Change the Version of .Net Framework as 4.0

now In my Scenario I am Creating a Record using this Solution. Sor in my form I've added one Text Field and One Button. On clicking of button the record must be created using the textfield value from textbox.

so double click on button in form Design and paste the following code

---------------->
 try
            {
                ClientCredentials cre = new ClientCredentials();
                cre.UserName.UserName = "User Name";
                cre.UserName.Password = "Password";
               var a = "https://<org name>.crm5.dynamics.com";  //for online crm
                Uri serviceUri = new Uri(a+"/xrmservices/2011/Organization.svc");

                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                IOrganizationService service = (IOrganizationService)proxy;

                Entity ent = new Entity("entity name");
                ent.Attributes["Field attribute name"] = textBox1.Text;
                service.Create(ent);
            }
            catch (SoapException ex)
            {

            }
            catch (Exception ex)
            {

            }
---------------->

Monday 10 March 2014

Delete a Record in CRM using OData Query

Hi Folks,

To Delete a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library 
--------------------------->
 function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();  //Getting id throug lookup
    var id = lookupObject[0].id;
    var set="new_entity5Set";
    updateRecord(id, set);
}
    function updateRecord(id, odataSetName) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Deleted successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    alert("Error while deletion " + errorThrown);
  
    }
    });
    }

--------------------------->

Update a Record in CRM using OData Query

Hi Folks,

To Update a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library
------------------>
function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();        //getting the id through lookup
    var id = lookupObject[0].id;
    var entity = new Object();
    entity.new_name="Hello";
    var set="your entity schema nameSet";              //entity name with Set
    updateRecord(id, entity, set);
}
    function updateRecord(id, entityObject, odataSetName) {
    var jsonEntity = window.JSON.stringify(entityObject);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Updated successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    if (XmlHttpRequest && XmlHttpRequest.responseText) {
    alert("Error while updating " + odataSetName+ " ; Error – " + XmlHttpRequest.responseText);
    }
    }
    });
    }

----------------------->

Wednesday 5 March 2014

Connecting CRM to Custom Web Page

Here a Custom Web Page is connected to CRM. Now for doing so we require SDK and Visual Studio.

the following Steps can be used to do so

a)  Open Visual Studio create a new Web Site of type ASP.NET Empty Web Site give a desired name and click on Ok. I have my form name as "crmsample".


 
b) now In Solution Explorer, right click on Project(crmsample), go to Add and click on New Item. this new Item is the actual web page in which the fields are displayed. Now select the Web Form type of template.

 

c) Within the Solution Explorer, right Click on References and Click on Add Reference, now a pop-up will be displayed select the following Assemblies or browse it to the SDK/Bin folder there you can get these Assemblies.
  • System.ServiceModel.Description
  • Microsoft.Xrm.Sdk.Client
  • System.Net
  • Microsoft.Xrm.Sdk

d) Now go to Solution Explorer, right click on the form name(I had given name as Default) and click on View Designer.
 
e)  Now add corresponding labels and text boxes and button with the help of toolbox{here I am creating a contact record on clicking of a button}. The below screen shot explains it.

 
f) I've changed the Id's of text boxes,  by right clicking on text boxes go to properties, In properties toolbox you can change its ID.
for First Name - txtFirstName
for Last Name - txtLastName
for Email Address - txtEmailAddress
for Phone Number - txtPhoneNumber

g)  Now on click of Submit button the record must be created, so for doing thse we must give an action to the button so double click on button, then a snippet for the action of button on clicking will be displayed. add the following gode within the on click event of Submit button.
------------------>
protected void Button1_Click(object sender, EventArgs e)
        {
            ClientCredentials Credentials = new ClientCredentials();
            Credentials.UserName.UserName = "your User Name";
            Credentials.UserName.Password = "your Password";
            //Uncomment th below lines to use the default username and password for the browser in which CRM is currently opened
            //Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //This URL needs to be updated to match the servername and Organization for the environment.
            Uri OrganizationUri = new Uri("Organization svc URL");
            Uri HomeRealmUri = null;
      
            using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
            {
                IOrganizationService service = (IOrganizationService)serviceProxy;

                //Instantiate the contact object and populate the attributes.
                Entity contact = new Entity("contact");
                contact["firstname"] = txtFirstName.Text.ToString();
                contact["lastname"] = txtLastName.Text.ToString();
                contact["emailaddress1"] = txtEmailAddress.Text.ToString();
                contact["telephone1"] = txtPhoneNumber.Text.ToString();
                Guid newContactId = service.Create(contact);

                //This code will clear the textboxes after the contact is created.
                txtFirstName.Text = "";
                txtLastName.Text = "";
                txtEmailAddress.Text = "";
                txtPhoneNumber.Text = "";
            }
        }
----------------->
also add the included references to this file at top of the code.

h) Now click Complie and Run the code. The webpage will be opened in default web browser. the following screen shot shows it
 
i) Now fill the fields and click on Submit Button. Now go to your CRM and see the contact entity, a record will be created.
Note:- The Mandatory Fields in CRM should be filled or else the record will not be created and instead it will throw an error.