Tuesday, 3 April 2012

create a record in dynamic crm 2011 frow web application

first Create a web application.from Framwork 4.0.

In ASPX we can give one label, one textbox and one button

My example:

        this is the trail to create a record in &quot;new_cooldrink&quot; entity at dynamic crm
        <asp:Label ID="Label1" runat="server" Text="name"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="save" />

code in c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using Microsoft.Xrm.Sdk;
using System.Runtime.Serialization;
using System.Data;
using System.Data.Entity;

namespace connectcrm2011
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void Button1_Click(object sender, EventArgs e)
            ClientCredentials Credentials = new ClientCredentials();
            Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //This URL needs to be updated to match the servername and Organization for the environment.

            Uri OrganizationUri = new Uri("http://<servername>/<organization name>/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            //OrganizationServiceProxy serviceProxy;   

            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");
                //Entity new_cooldrink = new Entity("new_cooldrink");
                contact["lastname"] = TextBox1.Text.ToString();//txtFirstName.Text.ToString();
                 //new_cooldrink["new_name"] = Application["oauth_access"].ToString();
                //contact["lastname"] = txtLastName.Text.ToString();
                //contact["emailaddress1"] = txtEmailAddress.Text.ToString();
                //contact["telephone1"] = txtPhoneNumber.Text.ToString();
                Guid newContactId = service.Create(contact);
                 //for account entity
                //Guid newaccountId = service.Create(account);
                //Guid newnew_cooldrinkid = service.Create(new_cooldrink);
                //This code will clear the textboxes after the contact is created.
                TextBox1.Text = "";

Note : must add the reference
1. System.Runtime.Serialization
  in reference area.

may be cause for error:

Thread: Error: Reference required to assembly 'System.Runtime.Serialization, Version=, Culture=neutral,..

then in webconfig:  

<compilation debug="true" targetFramework="4.0">
        <add assembly="System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Abstractions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

ref: http://blogs.msdn.com/b/crminthefield/archive/2011/05/18/how-to-create-a-simple-webpage-leveraging-the-crm-2011-iorganizationservice-web-service.aspx 

for this we need to add the reference :   


in reference area.




No comments:

Post a Comment