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:

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

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
2.System.Data.Entity
  in reference area.

may be cause for error:

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

then in webconfig:  

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

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 :   

System.Runtime.Serialization 

in reference area.


 

 

 


No comments:

Post a Comment