using System;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Net;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //Retrieve Organization Name from the Query String

        string orgname = Request.QueryString["orgname"].ToString();

 

        //Wrap all your CRM Web Service Code in a using block

        using (new CrmImpersonator())

        {

            //Create your token with the static method ExtractCrmAuthenticationToken

            //The 'Context' used here is the Page.Context

            CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);

 

            CrmService service = new CrmService();

            service.CrmAuthenticationTokenValue = token;

            service.Credentials = CredentialCache.DefaultCredentials;

 

            //Create the lead object as normal

            lead lead = new lead();

            lead.subject = "Lorem";

            lead.firstname = "foo";

            lead.lastname = "bar";

            lead.companyname = "Ipsum";

 

            //Assign the owner as the caller id from the token

            //If you don't do this the owner will be SYSTEM

            lead.ownerid = new Owner();

            lead.ownerid.type = EntityName.systemuser;

            lead.ownerid.Value = token.CallerId;

 

            //Create the lead on Skype

            Guid leadid = service.Create(lead);

        }

 

        //Write the GUID out

        Response.Write(leadid.ToString());

    }

}