private bool UpdateAccountState(Guid accid, string astatuscode, string astatecode)
{
bool checkflag = false;
try
{
CrmService Crm4_service = connect();
// Create the request object.
SetStateAccountRequest state = new SetStateAccountRequest();
// Set the properties of the request object.
if (astatecode == "Inactive")
{
state.AccountState = AccountState.Inactive;
}
if (astatecode == "Active")
{
state.AccountState = AccountState.Active;
}
state.AccountStatus = Convert.ToInt32(astatuscode);
// EntityId is the GUID of the account whose state is being changed.
state.EntityId = accid;
// Execute the request.
SetStateAccountResponse stateSet = (SetStateAccountResponse)Crm4_service.Execute(state);
checkflag = true;
}
catch
{
return checkflag;
}
return checkflag;
}
Tuesday, September 22, 2009
Saturday, July 18, 2009
Send Email Activity using template from CRM 3.0 / 4.0 using C#
private void SendEmailFromTemplate()
{
CrmService service = connect();
// Create the 'From:' activity party for the email
activityparty fromParty = new activityparty();
fromParty.partyid = new Lookup();
fromParty.partyid.type = EntityName.systemuser.ToString();
fromParty.partyid.Value = new Guid("4D52A946-AE37-DE11-B372-000C29DF4A76");
// Create the 'To:' activity party for the email
activityparty toParty = new activityparty();
toParty.partyid = new Lookup();
toParty.partyid.type = EntityName.account.ToString();
toParty.partyid.Value = new Guid("B835890F-E23E-DE11-ACC7-000C29DF4A76");
// Create an email message.
email email = new email();
// Set email properties
email.to = new activityparty[] { toParty };
email.from = new activityparty[] { fromParty };
email.subject = "SDK Sample email";
email.description = "SDK Sample for SendEmailFromTemplate Message.";
CrmBoolean direction = new CrmBoolean();
direction.Value = true;
email.directioncode = direction;
TargetSendFromTemplateEmail emailTemplateTarget = new TargetSendFromTemplateEmail();
emailTemplateTarget.Email = email;
SendEmailFromTemplateRequest emailRequest = new SendEmailFromTemplateRequest();
emailRequest.Target = emailTemplateTarget;
// The regarding Id is requried and must be of the same type as the Email Template
emailRequest.RegardingId = new Guid("B835890F-E23E-DE11-ACC7-000C29DF4A76");
emailRequest.RegardingType = EntityName.account.ToString();
// Use a built-in email template of type "contact".
emailRequest.TemplateId = new Guid("B6C38BB9-2423-471D-8B38-E3B99ED80E7A");
SendEmailFromTemplateResponse emailResponse = (SendEmailFromTemplateResponse)service.Execute(emailRequest);
}
{
CrmService service = connect();
// Create the 'From:' activity party for the email
activityparty fromParty = new activityparty();
fromParty.partyid = new Lookup();
fromParty.partyid.type = EntityName.systemuser.ToString();
fromParty.partyid.Value = new Guid("4D52A946-AE37-DE11-B372-000C29DF4A76");
// Create the 'To:' activity party for the email
activityparty toParty = new activityparty();
toParty.partyid = new Lookup();
toParty.partyid.type = EntityName.account.ToString();
toParty.partyid.Value = new Guid("B835890F-E23E-DE11-ACC7-000C29DF4A76");
// Create an email message.
email email = new email();
// Set email properties
email.to = new activityparty[] { toParty };
email.from = new activityparty[] { fromParty };
email.subject = "SDK Sample email";
email.description = "SDK Sample for SendEmailFromTemplate Message.";
CrmBoolean direction = new CrmBoolean();
direction.Value = true;
email.directioncode = direction;
TargetSendFromTemplateEmail emailTemplateTarget = new TargetSendFromTemplateEmail();
emailTemplateTarget.Email = email;
SendEmailFromTemplateRequest emailRequest = new SendEmailFromTemplateRequest();
emailRequest.Target = emailTemplateTarget;
// The regarding Id is requried and must be of the same type as the Email Template
emailRequest.RegardingId = new Guid("B835890F-E23E-DE11-ACC7-000C29DF4A76");
emailRequest.RegardingType = EntityName.account.ToString();
// Use a built-in email template of type "contact".
emailRequest.TemplateId = new Guid("B6C38BB9-2423-471D-8B38-E3B99ED80E7A");
SendEmailFromTemplateResponse emailResponse = (SendEmailFromTemplateResponse)service.Execute(emailRequest);
}
Send saved email activity / Message form crm 4.0 / 3.0
private void SendEmail(Guid emailActId)
{
#region Create and SendEmail request.
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailActId;
req.TrackingToken = "";
req.IssueSend = true;
// Send the email message.
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
#endregion
}
{
#region Create and SendEmail request.
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailActId;
req.TrackingToken = "";
req.IssueSend = true;
// Send the email message.
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
#endregion
}
Friday, July 17, 2009
Add / Upload attachment to Email activity in CRM 4.0/ 3.0
public void AddAttachmentToEmailActivity(string emailActId, string PDFFileNameName)
{
//Add an attachment
string currentPath = txtPath.Text;
Directory.SetCurrentDirectory(currentPath );
// Check to see if File exists
activitymimeattachment attachment = new activitymimeattachment();
if (File.Exists(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf"))
{
// Get mimeType for File extension
string ext = System.IO.Path.GetExtension(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf").ToLower();
string mimeType = string.Empty;
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
//New Filestream Code for MS Word .doc and .docx extensions and PDF files .pdf extension
FileInfo pointer = new FileInfo(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf");
FileStream fileStream = pointer.OpenRead();
byte[] byteData = new byte[(int)fileStream.Length];
fileStream.Read(byteData, 0, (int)fileStream.Length);
string encodedData = System.Convert.ToBase64String(byteData);
fileStream.Flush();
fileStream.Close();
attachment.activityid = new Lookup();
attachment.activityid.Value =new Guid(emailActId);
attachment.activityid.type = EntityName.email.ToString();
attachment.filename = PDFFileNameName.ToString() + ".pdf";
attachment.attachmentnumber = new CrmNumber();
attachment.attachmentnumber.Value = 1;
attachment.body = encodedData;
attachment.mimetype = mimeType;
service = CLM_CRM_Service.GetCrmService();
// Create the Attachment in CRM.
Guid attachmentId = service.Create(attachment);
}
}
{
//Add an attachment
string currentPath = txtPath.Text;
Directory.SetCurrentDirectory(currentPath );
// Check to see if File exists
activitymimeattachment attachment = new activitymimeattachment();
if (File.Exists(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf"))
{
// Get mimeType for File extension
string ext = System.IO.Path.GetExtension(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf").ToLower();
string mimeType = string.Empty;
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
//New Filestream Code for MS Word .doc and .docx extensions and PDF files .pdf extension
FileInfo pointer = new FileInfo(currentPath + "\\" + PDFFileNameName.ToString() + ".pdf");
FileStream fileStream = pointer.OpenRead();
byte[] byteData = new byte[(int)fileStream.Length];
fileStream.Read(byteData, 0, (int)fileStream.Length);
string encodedData = System.Convert.ToBase64String(byteData);
fileStream.Flush();
fileStream.Close();
attachment.activityid = new Lookup();
attachment.activityid.Value =new Guid(emailActId);
attachment.activityid.type = EntityName.email.ToString();
attachment.filename = PDFFileNameName.ToString() + ".pdf";
attachment.attachmentnumber = new CrmNumber();
attachment.attachmentnumber.Value = 1;
attachment.body = encodedData;
attachment.mimetype = mimeType;
service = CLM_CRM_Service.GetCrmService();
// Create the Attachment in CRM.
Guid attachmentId = service.Create(attachment);
}
}
Create Email activity for CRM 4.0 form C#.net
public void CreateEmailActivity(string accountId, string PDFFileNameName, string invoiceId)
{
string[] splitName = PDFFileNameName.Split('-'); //Larry & Susan Craven - Invoice Processed For Month - Jan 2009.pdf
string[] splitExt = splitName[2].Split('.');
service = CLM_CRM_Service.GetCrmService();
email actpointer = new email();
actpointer.subject = "Christian Life Ministry - " + splitName[1].ToString() + " - " + splitExt[0].ToString();
actpointer.description = "body";
Guid ow = new Guid(ownerID.ToString());
actpointer.ownerid = new Owner();
actpointer.ownerid.type = EntityName.systemuser.ToString();
actpointer.ownerid.Value = ow;
activityparty party = new activityparty();
party.partyid = new Lookup();
party.partyid.type = EntityName.systemuser.ToString();
party.partyid.Value = ow;
actpointer.from = new activityparty[] { party };
Guid ow1 = new Guid(accountId.ToString());
activityparty party1 = new activityparty();
party1.partyid = new Lookup();
party1.partyid.type = EntityName.account.ToString();
party1.partyid.Value = ow1;
actpointer.to = new activityparty[] { party1 };
Guid ow2 = new Guid(invoiceId.ToString());
Lookup lookup = new Lookup();
lookup.Value = ow2;
lookup.type = EntityName.invoice.ToString();
actpointer.regardingobjectid = lookup;
Guid emailActId = service.Create(actpointer);
AddAttachmentToEmailActivity(emailActId.ToString(), PDFFileNameName);
}
{
string[] splitName = PDFFileNameName.Split('-'); //Larry & Susan Craven - Invoice Processed For Month - Jan 2009.pdf
string[] splitExt = splitName[2].Split('.');
service = CLM_CRM_Service.GetCrmService();
email actpointer = new email();
actpointer.subject = "Christian Life Ministry - " + splitName[1].ToString() + " - " + splitExt[0].ToString();
actpointer.description = "body";
Guid ow = new Guid(ownerID.ToString());
actpointer.ownerid = new Owner();
actpointer.ownerid.type = EntityName.systemuser.ToString();
actpointer.ownerid.Value = ow;
activityparty party = new activityparty();
party.partyid = new Lookup();
party.partyid.type = EntityName.systemuser.ToString();
party.partyid.Value = ow;
actpointer.from = new activityparty[] { party };
Guid ow1 = new Guid(accountId.ToString());
activityparty party1 = new activityparty();
party1.partyid = new Lookup();
party1.partyid.type = EntityName.account.ToString();
party1.partyid.Value = ow1;
actpointer.to = new activityparty[] { party1 };
Guid ow2 = new Guid(invoiceId.ToString());
Lookup lookup = new Lookup();
lookup.Value = ow2;
lookup.type = EntityName.invoice.ToString();
actpointer.regardingobjectid = lookup;
Guid emailActId = service.Create(actpointer);
AddAttachmentToEmailActivity(emailActId.ToString(), PDFFileNameName);
}
Code to connect to CRM meta service
public static MetadataService.MetadataService GetMetaService()
{
#region for Online
CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", "dev.crm.dynamics.com", "Passport");
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(ConfigurationSettings.AppSettings["CRMUserName"], ConfigurationSettings.AppSettings["CRMPassword"], "crm.dynamics.com", policyResponse.Policy, "Production");
lm.Dispose();
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.OrganizationName = ConfigurationSettings.AppSettings["Organization"];
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
MetadataService.MetadataService mdService =new CLMApplication.MetadataService.MetadataService();
mdService.Url = crmTicketResponse.OrganizationDetail.CrmMetadataServiceUrl;
MetadataService.CrmAuthenticationToken token = new MetadataService.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
mdService.CrmAuthenticationTokenValue = token;
//CrmService myService = objConfig.RetrieveService();
//CreateDynamiceEntity(mdService, myService);
#endregion
return mdService;
}
{
#region for Online
CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", "dev.crm.dynamics.com", "Passport");
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(ConfigurationSettings.AppSettings["CRMUserName"], ConfigurationSettings.AppSettings["CRMPassword"], "crm.dynamics.com", policyResponse.Policy, "Production");
lm.Dispose();
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.OrganizationName = ConfigurationSettings.AppSettings["Organization"];
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
MetadataService.MetadataService mdService =new CLMApplication.MetadataService.MetadataService();
mdService.Url = crmTicketResponse.OrganizationDetail.CrmMetadataServiceUrl;
MetadataService.CrmAuthenticationToken token = new MetadataService.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
mdService.CrmAuthenticationTokenValue = token;
//CrmService myService = objConfig.RetrieveService();
//CreateDynamiceEntity(mdService, myService);
#endregion
return mdService;
}
Code to connect to CRM Online CRMService
you have to down load service and store it on hard disk and add reference to it in application.
public static CrmService.CrmService GetCrmService()
{
if (CLMservice == null)
{
CLMservice = new CrmService.CrmService();
//CrmAuthenticationToken token1 = new CrmAuthenticationToken();
//token1.AuthenticationType = 0;
//token1.OrganizationName = "CLM";
//CLMservice.CrmAuthenticationTokenValue = token1;
//CLMservice.Credentials = new System.Net.NetworkCredential("administrator", "syspro15*TX");
//
//-------------------
CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", "dev.crm.dynamics.com", "Passport");
//"http://clm.crm.dynamics.com/MSCrmServices/2007/Passport/CrmDiscoveryService.asmx?uniquename=clm";//
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
LogonManager lm = new LogonManager();
//string passportTicket = lm.Logon("syspro101@hotmail.com", "dxt31MS", "crm.dynamics.com", policyResponse.Policy, "Production");
// string passportTicket = lm.Logon("crmadmin@sysprotech.com", "sys35adm", "crm.dynamics.com", policyResponse.Policy, "Production");
string passportTicket = lm.Logon(ConfigurationSettings.AppSettings["CRMUserName"], ConfigurationSettings.AppSettings["CRMPassword"], "crm.dynamics.com", policyResponse.Policy, "Production");
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
//crmTicketRequest.OrganizationName = "clm";
crmTicketRequest.OrganizationName = ConfigurationSettings.AppSettings["Organization"];
//crmTicketRequest.OrganizationName = System.Configuration.ConfigurationSettings.AppSettings["Orgname"];
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
CrmService.CrmService crmService = new CrmService.CrmService();
crmService.Url = crmTicketResponse.OrganizationDetail.CrmServiceUrl;
CrmService.CrmAuthenticationToken token = new CrmService.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
crmService.CrmAuthenticationTokenValue = token;
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)crmService.Execute(whoRequest);
lm.Dispose();
CrmService.CrmService service = crmService;
CLMservice = crmService;
return CLMservice;// service;
}
return CLMservice;
}
public static CrmService.CrmService GetCrmService()
{
if (CLMservice == null)
{
CLMservice = new CrmService.CrmService();
//CrmAuthenticationToken token1 = new CrmAuthenticationToken();
//token1.AuthenticationType = 0;
//token1.OrganizationName = "CLM";
//CLMservice.CrmAuthenticationTokenValue = token1;
//CLMservice.Credentials = new System.Net.NetworkCredential("administrator", "syspro15*TX");
//
//-------------------
CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", "dev.crm.dynamics.com", "Passport");
//"http://clm.crm.dynamics.com/MSCrmServices/2007/Passport/CrmDiscoveryService.asmx?uniquename=clm";//
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
LogonManager lm = new LogonManager();
//string passportTicket = lm.Logon("syspro101@hotmail.com", "dxt31MS", "crm.dynamics.com", policyResponse.Policy, "Production");
// string passportTicket = lm.Logon("crmadmin@sysprotech.com", "sys35adm", "crm.dynamics.com", policyResponse.Policy, "Production");
string passportTicket = lm.Logon(ConfigurationSettings.AppSettings["CRMUserName"], ConfigurationSettings.AppSettings["CRMPassword"], "crm.dynamics.com", policyResponse.Policy, "Production");
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
//crmTicketRequest.OrganizationName = "clm";
crmTicketRequest.OrganizationName = ConfigurationSettings.AppSettings["Organization"];
//crmTicketRequest.OrganizationName = System.Configuration.ConfigurationSettings.AppSettings["Orgname"];
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
CrmService.CrmService crmService = new CrmService.CrmService();
crmService.Url = crmTicketResponse.OrganizationDetail.CrmServiceUrl;
CrmService.CrmAuthenticationToken token = new CrmService.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
crmService.CrmAuthenticationTokenValue = token;
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)crmService.Execute(whoRequest);
lm.Dispose();
CrmService.CrmService service = crmService;
CLMservice = crmService;
return CLMservice;// service;
}
return CLMservice;
}
Subscribe to:
Posts (Atom)