Wednesday 29 July 2015

Sample code for creating contact record through web service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using System.ServiceModel.Description;

namespace crmtovswebservice
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public void run(string a, string b)
        {

            try
            {
           
                ClientCredentials cre = new ClientCredentials();
                cre.UserName.UserName = "username";
                cre.UserName.Password = "password";


                Uri serviceUri = new Uri("your server name");

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


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

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


        }
    }
}

No comments:

Post a Comment