This tip demonstrates how to create, retrieve , edit and update an costume Entity in Online MSD CRM 2011 Using WCF and LINQ Query. To know how to connect to an Online MSD CRM 2011 using WCF Services check out Abhisek’s tip on it.
step 1: Connect to Online MSD CRM 2011 using WCF Services.
step 2: Create a costumize Entity in the crm 2011 online version (e.g new_abc) and add Four fields
Name(Single line of text),Date(date and time),description(Multiline of text),Status(two options).
step 3:Now Add two panel control over the form existed.In first Panel add a DataGridView Control to the show the data & two button controls to show the data in DataGridView and another to add new data.And in 2nd Panel Add Text box controls for Name and Description and for Date add DateTimePicker control and for Status add Combo box control. At last, add two button control to submit & Update.
step 4: Add a using statement for Microsoft.Xrm.Sdk.Query.This namespace contains the classes needed to build the query
expressions.You should also add a using statement for Microsoft.Xrm.Sdk.Messages.This name space contains the classes needed to issue the retrieve request.
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
step 5: Add following code inside show button click event handler in code behind.
//Create a new instance of a QueryExpression and specify new_abc as the target entity for the query QueryExpression qeLocations = new QueryExpression("new_abc"); //Specify the columns that we want to retrieve.The ColumnSet property is simply a string array of //column names. string[] cols = { "new_abhiseksid","new_name", "new_status", "new_date", "new_description" }; qeLocations.ColumnSet = new ColumnSet(cols); //Call the RetrieveMultiple method on the organization service passing the QueryExpression. var locations = this.OrgService.RetrieveMultiple(qeLocations); //LINQ statement to format the results and bind to the DataGridView. var rtvTable = (from location in locations.Entities select new { Name = location["new_name"], Status = location["new_status"], Date = location["new_date"], Description = location["new_description"], Id=location["new_abcid"] }).ToList(); grdDetail.DataSource = rtvTable ; this.grdDetail.Columns["Id"].Visible = false;
step 6: Add following code inside Submit button click event handler in code behind.
{ if (string.IsNullOrEmpty(this.txbName.Text) || string.IsNullOrEmpty(this.Txbdate.Text) || string.IsNullOrEmpty(this.cbmStatus.Text) || string.IsNullOrEmpty(this.txbDescription.Text)) { MessageBox.Show("Please Complete the Form", "Important Note", MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton. Button1); } else { //enter data to the Entity new_abc. Entity entTable = new Entity("new_abc"); entTable["new_name"] = txbName.Text; if (Convert.ToString(cbmStatus.SelectedItem) == "Active") entTable["new_status"] = true; else entTable["new_status"] = false; entTable["new_date"] = Txbdate.Value; entTable["new_description"] = txbDescription.Text; OrgService.Create(entTable); } }
step 7: Add following code inside RowHeader click event handler of DataGridView to Edit in code behind
{ var j = e.ColumnIndex; //Bind data of Selected row of dataGridView with the controls. var name = grdDetail.Rows[e.RowIndex].Cells[j = 0].Value; var description = grdDetail.Rows[e.RowIndex].Cells[j = 3].Value; var date = grdDetail.Rows[e.RowIndex].Cells[j = 2].Value; var status = grdDetail.Rows[e.RowIndex].Cells[j = 1].Value; idEnt = (Guid)grdDetail.Rows[e.RowIndex].Cells[j = 4].Value; txbName.Text = (string)name; Txbdate.Value = (DateTime)date; if (Convert.ToBoolean(status) == true) { cbmStatus.SelectedIndex = 0; } else { cbmStatus.SelectedIndex = 1; } txbDescription.Text = (string)description; }
step 8: Add following code inside Update button click event handler to update in code behind.
{ //get Guid of the selected row to update. OrganizationServiceContext context = new OrganizationServiceContext(OrgService); var act = context.CreateQuery("new_abc") .Where(a => ((Guid)a["new_abcid"]) == idEnt).Single(); //update the selected row. act["new_name"] = txbName.Text; act["new_date"] =Txbdate.Value; act["new_description"] = txbDescription.Text; if (Convert.ToString(cbmStatus.SelectedItem) == "Active") act["new_status"] = true; else act["new_status"] = false; context.UpdateObject(act); context.SaveChanges(); MessageBox.Show("update successfully"); }