Friday, October 2, 2009

Connect to CRM 1.2

#region "Connect to CRM 1.2"
///
/// This fucntion will connect to CRM 1.2
///

/// After sucessfully connection it will return CRM 1.2 user authentication
private Microsoft.CRM.Proxy.CUserAuth connectToCRM()
{
// BizUser proxy object
Microsoft.CRM.Proxy.BizUser bizUser = new Microsoft.CRM.Proxy.BizUser();
bizUser.Credentials = new System.Net.NetworkCredential(userName, password, domain);
bizUser.Url = strDir + "BizUser.srf";

try
{
userAuth = bizUser.WhoAmI();
//MessageBox.Show("Sucessfully connected to CRM 1.2");
}
catch (System.Web.Services.Protocols.SoapException ex)
{
MessageBox.Show("Unable to connect to CRM 1.2, Please check CRM 1.2 details.", ex.Message.ToString());
return null;
}

return userAuth;
}

#endregion

Download file form web / IE without Save/Open dialog box

string url = "http://"; // Download File path
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Credentials = new System.Net.NetworkCredential("User Name", "Password", "Domain");

string sFile ="C:\\"; //Path to save file

myWebClient.DownloadFile(url, sFile);

Update / Set CRM Opportunity State/ Status

public bool UpdateOpportunityState(Guid activityid, string astatuscode, string astatecode)
{

CrmService Crm4_service = connect();
try
{
if (astatecode == "Open")
{
SetStateOpportunityRequest state = new SetStateOpportunityRequest();

if (astatuscode == "1")
{
return true;
}
else if (astatuscode == "2")
{
state.OpportunityState = OpportunityState.Open;
state.OpportunityStatus = 2;/// Convert.ToInt32();
}

state.EntityId = activityid;
SetStateOpportunityResponse stateSet = (SetStateOpportunityResponse)Crm4_service.Execute(state);

return true;
}



opportunity src_opp = (opportunity)Crm4_service.Retrieve(EntityName.opportunity.ToString(), activityid, new AllColumns()); // = OpportunityState.Lost;


opportunityclose opp_close = new opportunityclose();
opp_close.actualrevenue = src_opp.actualvalue;
opp_close.opportunityid = new Lookup();
opp_close.opportunityid.Value = src_opp.opportunityid.Value;
opp_close.actualend = src_opp.actualclosedate;
opp_close.statuscode = src_opp.statuscode;
if (astatuscode == "3")
{
// this Opportunity was won
WinOpportunityRequest opp_won = new WinOpportunityRequest();
opp_won.OpportunityClose = opp_close;
opp_won.Status = 3;// src_opp.statuscode.Value;
Crm4_service.Execute(opp_won);
return true;
}
else
{
// this Opportunity was lost
LoseOpportunityRequest opp_lost = new LoseOpportunityRequest();
opp_lost.OpportunityClose = opp_close;
if (astatuscode == "4")
{
opp_lost.Status = 4;// src_opp.statuscode.Value;
}
else
{
opp_lost.Status = 5;// src_opp.statuscode.Value;

}
Crm4_service.Execute(opp_lost);
return true;
}

}
catch (System.Web.Services.Protocols.SoapException ex)
{
return false;
}

return true;


}

Update / Set CRM Invoice Status/ state

internal bool updateInvioceState(Guid InvioceId, string statusCode, string stateCode)
{
try
{
CrmService Crm4_service = connect();
SetStateInvoiceRequest state = new SetStateInvoiceRequest();

if (stateCode == "Active")
{
state.InvoiceState = InvoiceState.Active;
}
else if (stateCode == "Canceled")
{
state.InvoiceState = InvoiceState.Canceled;
}
else if (stateCode == "Closed")
{
state.InvoiceState = InvoiceState.Closed;
}
else if (stateCode == "Paid")
{
state.InvoiceState = InvoiceState.Paid;
}

state.InvoiceStatus = Convert.ToInt32(statusCode);
state.EntityId = InvioceId;
SetStateInvoiceResponse stateSet = (SetStateInvoiceResponse)Crm4_service.Execute(state);

}
catch (System.Web.Services.Protocols.SoapException ex)
{
return false;
}
return true;
}