MartinSant.net

Software Engineering | MicroMachines

  • Airplane
  • Electronics
  • Model Trains
  • Chickens
  • 3D Design
  • German Shepherds
  • Garden
  • Protothrottle

Microsoft CRM and C#

So, after much surfing and experimenting, here is the final code snip for the CRM ‘leads’ table insert.  This basic method should work with any of the tables and since I googled forums and blogs for about three days trying to get it to work, I’ll post up my final effort.  Note this is for a very simple ‘get more information’ page with a “contact info form” and a ‘submit’ button.

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.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // This executes on the load of the page
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ClientCredentials Credentials = new ClientCredentials();
        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://skydom06:5555/XrmServices/2011/Organization.svc");
        Uri HomeRealmUri = null;

        //OrganizationServiceProxy serviceProxy;       
        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
        {
            string strUserName = string.Empty;
            string strUserPassword = string.Empty;
            string strUserOrganization = string.Empty;
            string strUserHost = string.Empty;
            string strUserPort = string.Empty;
            string strUserDomain = string.Empty;

            strUserName = "!user!";
            strUserPassword = "!password!";
            strUserDomain = "!domain!";
            strUserOrganization = "!organization!";

            try
            {
                // Set up the CRM service
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = 0;
                token.OrganizationName = strUserOrganization;

                // get Authorization using above info
                CrmService AuthService = new CrmService();
                AuthService.Url = "http://skydom06:5555/mscrmservices/2007/crmservice.asmx";
                AuthService.CrmAuthenticationTokenValue = token;
                AuthService.Credentials = new System.Net.NetworkCredential(strUserName, strUserPassword, strUserDomain);

                DynamicEntity NewLead = new DynamicEntity(EntityName.lead.ToString());

                NewLead.Properties.Add(new StringProperty("firstname", txtFirstName.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("lastname", txtLastName.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_line1", txtStreet1.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_line2", txtStreet2.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_city", txtCity.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_stateorprovince", txtState.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_postalcode", txtZip.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("address1_country", txtCountry.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("telephone2", txtPhoneNumber.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("mobilephone", txtCellPhone.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("ses_yeargraduated", txtGradYear.Text.ToString()));
                //NewLead.Properties.Add(new StringProperty("subject", txtProgram.Text.ToString()));
                NewLead.Properties.Add(new StringProperty("subject", "Prospective Student"));
                NewLead.Properties.Add(new StringProperty("emailaddress1", txtEmailAddress.Text.ToString()));

                AuthService.Create(NewLead);
            }
            catch (System.ServiceModel.Security.SecurityAccessDeniedException ex)
            {
                throw new Exception("An error occurred while attempting to authenticate.", ex);
            }

            Response.Redirect("thanks.htm");
        }
    }
}

  • Recent Posts

    • All finished, well almost
    • New Paint
    • Train Layout Rebuild
    • New Computer
    • Airplane Refurb
  • Pages

    • 2D vector drawings into 3D models
    • 3D Printed Figures
    • Android – Reels Game App
    • AVR Attiny 1634 s/w UART
    • Bluetooth Diagram and Descriptions
    • Building a G Scale Turnout
    • Burnley Country Store
    • Flying to York PA
    • New Puppy – Sophie
    • Protothrottle
    • Protothrottle to Airwire Beta
    • Rail Layout Editor
    • Raspberry Pi Zero W Wifi Access Point
    • RPi Android HTML5 Servo Control
    • Steam Engine and Dynamo
    • Tin Can Steam Boiler
    • Wixel Breakout Board
    • CRM C# Autonumber Plugin
    • Microsoft CRM and C#
    • EightWay Game
    • Dice In A Stone Box
    • About Martin
Proudly powered by WordPress Theme: Parament by Automattic.