Hi,
I want to create contact through asp.net application.
I added organization service as a service reference
here is my code
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.ServiceModel;
using System.IdentityModel;
using System.Web.SessionState;
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
{
try
{
//Authenticate using credentials of the logged in user;
ClientCredentials Credentials = new ClientCredentials();
Credentials.UserName.UserName = "web.agent";
Credentials.UserName.Password = "pass@word1";
// 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://mozzo:5555/MozzoLab/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");
contact["FirstName"] = txtFirstName.Text.ToString();
contact["LastName"] = txtLastName.Text.ToString();
contact["FullName"] = txtFirstName.Text.ToString() + " " + txtLastName.Text.ToString();
contact["new_UserName"] = txtUsername.Text.ToString();
contact["new_Password"] = txtPassword.Text.ToString();
Guid newContactId = service.Create(contact);
//This code will clear the textboxes after the contact is created.
txtFirstName.Text = "";
txtLastName.Text = "";
txtPassword.Text = "";
txtReEnterPassword.Text = "";
string message = "Data Saved Successfully.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
catch (Exception)
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtPassword.Text = "";
txtReEnterPassword.Text = "";
string message = "Please Try Again.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
// Session["login"] = 1;
}
}
}