Tuesday 14 July 2015

Sample code for creating a record in visual studio and updating it in crm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel.Description;
using System.Web.Services.Protocols;

namespace sample_code2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ClientCredentials cre = new ClientCredentials();
                cre.UserName.UserName = "username";// enter your user name
                cre.UserName.Password = "password";// enter your password
                Uri serviceUri = new Uri("login address");//give the url of your crm organization.You will                                                                                                                     find it in developer resource.

            OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                IOrganizationService service = (IOrganizationService)proxy;


                Entity ent = new Entity("contact");// creation of entity
                ent["firstname"] = textBox1.Text;
                ent["lastname"] = textBox2.Text;
                service.Create(ent);


            }
            catch (SoapException ex)
            {
                throw new InvalidOperationException();
            }
         

        }

    }
}

No comments:

Post a Comment