Refer the blog, how to create CRM Entites and Service Data Context
http://makdns.blogspot.com/2012/11/crmsvcutilexe-crm-dynamics-2011-code.html
Add the CRM.Entities.cs file to your project.
Insert, Update, Delete, View and Select Entities list using c#.
Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Reason - The reason for this error could be the machine does not have “Windows Identify Foundation Pack” installed, Download and install the Windows Identity Foundation Pack on the machine where you are trying to generate the wrappers.
Add the CRM.Entities.cs file to your project.
Insert, Update, Delete, View and Select Entities list using 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 Microsoft.Xrm.Sdk.Query;
using CRM.Entities;
namespace ContactDataEntry
{
public partial class default1 : System.Web.UI.Page
{
Uri OrganizationUri = new Uri("http://<Server_Name>/<Organization_Name>/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
String eid = "";
protected void Page_Load(object sender, EventArgs e)
{
ViewAll();
panDetails.Visible = false;
if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 1)
{
eid = Request.QueryString["id"];
}
if (!IsPostBack)
{
BindList(ddlSList, StateList);
BindList(ddlJList, JuList);
if (eid.Length > 0)
{
panDetails.Visible = true;
CreateNew.Visible = false;
View();
}
}
}
private void BindList(DropDownList ddl, Object obj)
{
ddl.DataSource = obj;
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.DataBind();
}
Assign List to Select
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 Microsoft.Xrm.Sdk.Query;
using CRM.Entities;
namespace ContactDataEntry
{
public partial class default1 : System.Web.UI.Page
{
Uri OrganizationUri = new Uri("http://<Server_Name>/<Organization_Name>/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
String eid = "";
protected void Page_Load(object sender, EventArgs e)
{
ViewAll();
panDetails.Visible = false;
if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 1)
{
eid = Request.QueryString["id"];
}
if (!IsPostBack)
{
BindList(ddlSList, StateList);
BindList(ddlJList, JuList);
if (eid.Length > 0)
{
panDetails.Visible = true;
CreateNew.Visible = false;
View();
}
}
}
private void BindList(DropDownList ddl, Object obj)
{
ddl.DataSource = obj;
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.DataBind();
}
internal IEnumerable<SelectList> StateList
{
get
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
IEnumerable<SelectList> bps = service.
g_stateSet.
Select(st => new SelectList
{
Text = st.g_name,
Value = st.g_stateId.Value.ToString()
}).ToList();
return bps;
}
}
}
internal IEnumerable<SelectList> JuList
{
get
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
IEnumerable<SelectList> bps = service.
g_jurisdictionSet.
Where(js => js.g_jType == 2).
Select(st => new SelectList
{
Text = st.g_name,
Value = st.g_jId.Value.ToString()
}).ToList();
return bps;
}
}
}
protected void Insert_Click(object sender, EventArgs e)
{
panDetails.Visible = true;
CreateNew.Visible = true;
Delete.Visible = false;
Update.Visible = false;
ClearAll();
//Response.Redirect("default.aspx?id=");
}
protected void CreateNew_Click(object sender, EventArgs e)
{
CreateUpdate("C");
}
Insert or Update Record
{
get
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
IEnumerable<SelectList> bps = service.
g_stateSet.
Select(st => new SelectList
{
Text = st.g_name,
Value = st.g_stateId.Value.ToString()
}).ToList();
return bps;
}
}
}
internal IEnumerable<SelectList> JuList
{
get
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
IEnumerable<SelectList> bps = service.
g_jurisdictionSet.
Where(js => js.g_jType == 2).
Select(st => new SelectList
{
Text = st.g_name,
Value = st.g_jId.Value.ToString()
}).ToList();
return bps;
}
}
}
protected void Insert_Click(object sender, EventArgs e)
{
panDetails.Visible = true;
CreateNew.Visible = true;
Delete.Visible = false;
Update.Visible = false;
ClearAll();
//Response.Redirect("default.aspx?id=");
}
protected void CreateNew_Click(object sender, EventArgs e)
{
CreateUpdate("C");
}
private void CreateUpdate(String type)
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
g_bpres bp = new g_bpres();
bp.g_fName = txtFName.Text.ToString();
bp.g_lname = txtLName.Text.ToString();
bp.g_Address1 = txtAddress.Text.ToString();
bp.g_City = txtCity.Text.ToString();
bp.g_sname = (ddlSList.SelectedValue != "") ? new EntityReference("g_state", new Guid(ddlSList.SelectedValue)) : null;
bp.g_MName = (ddlJList.SelectedValue != "") ? new EntityReference("g_jurisdiction", new Guid(ddlJList.SelectedValue)) : null;
bp.g_ZipCode = txtZip.Text.ToString();
if (type == "C")
{
Guid newContactId = service.Create(bp);
}
else
{
bp.g_bpId = new Guid(eid);
service.Update(bp);
}
ClearAll();
}
ViewAll();
}
private void ClearAll()
{
//This code will clear the textboxes after the contact is created.
txtFName.Text = "";
txtLName.Text = "";
txtAddress.Text = "";
txtCity.Text = "";
ddlSList.SelectedValue = "";
ddlJList.SelectedValue = "";
txtZip.Text = "";
}
View Record
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
g_bpres bp = new g_bpres();
bp.g_fName = txtFName.Text.ToString();
bp.g_lname = txtLName.Text.ToString();
bp.g_Address1 = txtAddress.Text.ToString();
bp.g_City = txtCity.Text.ToString();
bp.g_sname = (ddlSList.SelectedValue != "") ? new EntityReference("g_state", new Guid(ddlSList.SelectedValue)) : null;
bp.g_MName = (ddlJList.SelectedValue != "") ? new EntityReference("g_jurisdiction", new Guid(ddlJList.SelectedValue)) : null;
bp.g_ZipCode = txtZip.Text.ToString();
if (type == "C")
{
Guid newContactId = service.Create(bp);
}
else
{
bp.g_bpId = new Guid(eid);
service.Update(bp);
}
ClearAll();
}
ViewAll();
}
private void ClearAll()
{
//This code will clear the textboxes after the contact is created.
txtFName.Text = "";
txtLName.Text = "";
txtAddress.Text = "";
txtCity.Text = "";
ddlSList.SelectedValue = "";
ddlJList.SelectedValue = "";
txtZip.Text = "";
}
private void View()
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
g_bpres bp = new g_bpres();
bp = service.g_bpresSet.Where(cbp => cbp.g_bpId.Value == new Guid(eid)).SingleOrDefault();
txtFName.Text = bp.g_fName;
txtLName.Text = bp.g_lname;
txtZip.Text = bp.g_ZipCode;
txtAddress.Text = bp.g_Address1;
txtCity.Text = bp.g_City;
ddlSList.SelectedValue = (bp.g_sname != null)?bp.g_sname.Id.ToString():"";
ddlJList.SelectedValue = (bp.g_MName != null) ? bp.g_MName.Id.ToString() : "";
}
}
protected void Delete_Click(object sender, EventArgs e)
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
service.Delete(g_bpres.EntityLogicalName, new Guid(eid));
}
panDetails.Visible = false;
ViewAll();
}
protected void Update_Click(object sender, EventArgs e)
{
CreateUpdate("U");
}
View All Records
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
g_bpres bp = new g_bpres();
bp = service.g_bpresSet.Where(cbp => cbp.g_bpId.Value == new Guid(eid)).SingleOrDefault();
txtFName.Text = bp.g_fName;
txtLName.Text = bp.g_lname;
txtZip.Text = bp.g_ZipCode;
txtAddress.Text = bp.g_Address1;
txtCity.Text = bp.g_City;
ddlSList.SelectedValue = (bp.g_sname != null)?bp.g_sname.Id.ToString():"";
ddlJList.SelectedValue = (bp.g_MName != null) ? bp.g_MName.Id.ToString() : "";
}
}
protected void Delete_Click(object sender, EventArgs e)
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
service.Delete(g_bpres.EntityLogicalName, new Guid(eid));
}
panDetails.Visible = false;
ViewAll();
}
protected void Update_Click(object sender, EventArgs e)
{
CreateUpdate("U");
}
private void ViewAll()
{
View_All();
}
//retrieving all the attributes for an entity will affect the performance of your code.
private void View_All()
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
List<g_bpres> bps = service.g_bpresSet.ToList();
DisplayResults(bps);
}
}
private void View_All_Condition()
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
List<g_bpres> bps = service.
g_bpresSet.
Where(bp => bp.g_ZipCode == "53590").
Select(bp => new g_bpres
{
g_bpId = bp.g_bpId.Value,
g_fName = bp.g_fName,
g_lname = bp.g_lname
}).
ToList();
DisplayResults(bps);
}
}
Display Results
{
View_All();
}
//retrieving all the attributes for an entity will affect the performance of your code.
private void View_All()
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
List<g_bpres> bps = service.g_bpresSet.ToList();
DisplayResults(bps);
}
}
private void View_All_Condition()
{
using (XrmServiceContext service = new XrmServiceContext(CrmService))
{
List<g_bpres> bps = service.
g_bpresSet.
Where(bp => bp.g_ZipCode == "53590").
Select(bp => new g_bpres
{
g_bpId = bp.g_bpId.Value,
g_fName = bp.g_fName,
g_lname = bp.g_lname
}).
ToList();
DisplayResults(bps);
}
}
private void DisplayResults(List<g_bpres> bps)
{
String strHtml = "<table rules='all' border='1'><tr><td></td><td>Name</td><td>State</td><td>M Name</td></tr>";
foreach (var c in bps)
{
strHtml += "<tr><td><a href=default1.aspx?id=" + c.g_bpId + ">Edit</a></td><td>";
strHtml += (c.g_fName != null && c.g_lname != null) ? (c.g_fName + "," + c.g_lname) : "";
strHtml += "</td><td>";
strHtml += c.g_sname!=null?c.g_sname.Name:"";
strHtml += "</td><td>";
strHtml += c.Attributes.Contains("g_mname")?(((EntityReference)c.Attributes["g_mname"]).Name.ToString()):"";
strHtml += "</td></tr>";
}
litViewAll.Text = strHtml + "</table>";
}
internal class SelectList
{
public String Text { get; set; }
public String Value { get; set; }
}
internal ClientCredentials Credentials
{
get
{
//Authenticate using credentials of the logged in user;
Credentials
{
String strHtml = "<table rules='all' border='1'><tr><td></td><td>Name</td><td>State</td><td>M Name</td></tr>";
foreach (var c in bps)
{
strHtml += "<tr><td><a href=default1.aspx?id=" + c.g_bpId + ">Edit</a></td><td>";
strHtml += (c.g_fName != null && c.g_lname != null) ? (c.g_fName + "," + c.g_lname) : "";
strHtml += "</td><td>";
strHtml += c.g_sname!=null?c.g_sname.Name:"";
strHtml += "</td><td>";
strHtml += c.Attributes.Contains("g_mname")?(((EntityReference)c.Attributes["g_mname"]).Name.ToString()):"";
strHtml += "</td></tr>";
}
litViewAll.Text = strHtml + "</table>";
}
internal class SelectList
{
public String Text { get; set; }
public String Value { get; set; }
}
internal ClientCredentials Credentials
{
get
{
//Authenticate using credentials of the logged in user;
ClientCredentials cntCredentials = new ClientCredentials();
cntCredentials.UserName.UserName = "uid";
cntCredentials.UserName.Password = "pwd";
//cntCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
return cntCredentials;
}
}
internal OrganizationServiceProxy CrmService
{
get
{
OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);
serviceProxy.EnableProxyTypes();
return serviceProxy;
}
}
}
}
Error cntCredentials.UserName.UserName = "uid";
cntCredentials.UserName.Password = "pwd";
//cntCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
return cntCredentials;
}
}
internal OrganizationServiceProxy CrmService
{
get
{
OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);
serviceProxy.EnableProxyTypes();
return serviceProxy;
}
}
}
}
Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Reason - The reason for this error could be the machine does not have “Windows Identify Foundation Pack” installed, Download and install the Windows Identity Foundation Pack on the machine where you are trying to generate the wrappers.
No comments:
Post a Comment