Friday 17 March 2017

Send a Custom E-mail for a Campaign Activity

The following sample shows how to send an e-mail for a campaign activity.
 
[C#]
public void SendEmail(Guid campaignActivityID)
{
   CrmService service = new CrmService();
   service.Credentials = 
      System.Net.CredentialCache.DefaultCredentials;

   service.CallerIdValue = new CallerId();
   // Replace the GUID with the GUID of your Microsoft Dynamics CRM
   // Administrator.
   service.CallerIdValue.CallerGuid =
      new Guid("FD80F8E8-C852-DA11-B1FB-0007E94D105B");

   SendEmailRequest req = new SendEmailRequest();
   req.EmailId = campaignActivityID;
   req.TrackingToken = "";
   req.IssueSend = true;

   try 
   {
      SendEmailResponse res =
         (SendEmailResponse)service.Execute(req);
   }
   catch (System.Web.Services.Protocols.SoapException er)
   {
      //Process any error messages here.
   }
}
 


CRM 4.0 Form JS Library

Here are a set of functions to work with CRM 4.0 forms.
 
/* Jscript */

                    document = new Object();

// Field Object
document.getFieldObj = function(fname)
{
    var ret = document.getElementById(fname);
    return ret;
};
// FieldText
document.getDataValue = function(fname)
{
    var str = document.getFieldObj(fname);
    str = str.DataValue;
    return tmp;
};
document.getDefaultValue = function(fname)
{
    var obj = document.getFieldObj(fname);
    obj = obj.DefaultValue;
    return tmp;
};
document.lookupItem = function(fname)
{
    var obj = document.getFieldObj(fname);
    return (obj[0].name);
};
document.lookupGuid = function(fname)
{
    var obj = document.getFieldObj(fname);
    return (obj[0].id);
};
document.lookupTypename = function(fname)
{
    var obj = document.getFieldObj(fname);
    return (obj[0].typename);
};
document.setFocus = function(fname)
{
    var obj = document.getFieldObj(fname);
    obj.SetFocus();
};
document.onChange = function(fname)
{
    var obj = document.getFieldObj(fname);
    obj.FireOnChange();
};
document.getRequiredLevel = function(fname)
{
    var tmp = document.getFieldObj(fname);
    return tmp.RequiredLevel;
};
document.idDirty = function(fname)
{
    var tmp = document.getFieldObj(fname);
    if(tmp.IsDirty)
        return true;
    else
        return false;
};
document.disableField = function(fname)
{
    var str = document.getFieldObj(fname);
    str.disabled = true;
    return true;
}
document.forceSubmit = function(fname)
{
    var obj = document.getFieldObj(fname);
    obj.ForceSubmit;
}
document.getSelectedText = function(fname)
{
    var obj = document.getFieldObj(fname);
    return(obj.SelectedText);
}
document.getSelectedOption = function(fname)
{
    var obj = document.getFieldObj(fname);
    return(obj.GetSelectedOption);
}
document.getOptions = function(fname)
{
    var obj = document.getFieldObj(fname);
    return(obj.Options);
}
document.addOption = function(fname,text,datavalue)
{
    var obj = document.getFieldObj(fname);
    obj.AddOption(text,datavalue);
    return true;
}
document.delOption = function(fname,value)
{
    var obj = document.getFieldObj(fname);
    obj.DeleteOption(value);
    return true;
}
document.genSoap = function(fxml)
{
    var soap2 = "";
    soap2 += GenerateAuthenticationHeader();
    soap2 += "";
    soap2 += fxml;
    soap2 += "";
    return soap2;
}
document.ajaxRequest = function(genUrl)
{
    var xhr = new ActiveXObject("Msxml2.XMLHTTP");
    xhr.open("GET", genUrl, false);
    xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xhr.send(null);
    var resultSet = xhr.responsetext;
    return resultSet;
}

document.ajaxSoap = function (soap_msg) {
    // COUNTRY ISO CODE
    var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    XmlHttp.open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
    XmlHttp.setRequestHeader("Content-Length", soap_msg.length);
    XmlHttp.send(soap_msg);
    var resultSet = XmlHttp.responseXML.text;
}             

Create CRM Organization

public class CreateCrmOrg
{
       public CreateCRM_Org()
       {
        static void Main()
        {
               DeploymentServiceClient service = Microsoft.Xrm.Sdk.Deployment.Proxy
                      .ProxyClientHelper.CreateClient(new Uri("http://srv-crm04/XRMDeployment/2011/Deployment.svc"));
               Console.WriteLine(CreateOrganization(service
                      ,new Organization
                             {
                                   UniqueName = "testOrgProv1",
                                   FriendlyName = "testOrgProv1",
                                   SqlServerName = "SQL1-CRM04",
                                   SrsUrl = "http://SQL1-CRM04/ReportServer",
                                   BaseCurrencyCode = RegionInfo.CurrentRegion.ISOCurrencySymbol,
                                   BaseCurrencyName = RegionInfo.CurrentRegion.CurrencyNativeName,
                                   BaseCurrencySymbol = RegionInfo.CurrentRegion.CurrencySymbol,
                                   State = Microsoft.Xrm.Sdk.Deployment.OrganizationState.Enabled
                             }));
        }           

        Guid? CreateOrganization(IDeploymentService deploymentService,Organization org)
        {
               BeginCreateOrganizationRequest req = new BeginCreateOrganizationRequest
               {
                      Organization = org
               };

               BeginCreateOrganizationResponse resp = deploymentService.Execute(req) as BeginCreateOrganizationResponse;
               return resp != null ? (Guid?)resp.OperationId : null;
        }
              
}




CRM 4.0 JScript Guide

// CRM 4.0 Form Scripts Example
//Set Field Value
crmForm.all.crmfieldscemaname.DataValue= "value";

//Get Field Value
var value = crmForm.all.crmfieldscemaname.DataValue;

//Set Requirement Field
crmForm.all.crmfieldscemaname.setAttribute( "req",0);
//or
crmForm.all.crmfieldscemaname_c.classname= "n";

// Set Recommended
crmForm.all.crmfieldscemaname.setAttribute( "req",1);
//or
crmForm.all.crmfieldscemaname_c.className= "rec";

//Set Required
crmForm.all.crmfieldscemaname.setAttribute( "req",2);
//or
crmForm.all.crmfieldscemaname_c.className= "req";

//Disable a field
crmForm.all.crmfieldscemaname.Disabled= true;

//Enable a Field
crmForm.all.crmfieldscemaname.Disabled= false;

//hide a field
crmForm.all.crmfieldscemaname.style.Display= "none";
//or
crmForm.all.crmfieldscemaname_c.style.Display= "none";

//Show a field
crmForm.all.crmfieldscemaname.style.Display= "block";
//or
crmForm.all.crmfieldscemaname_c.style.Display= "inline";

//Set PickList Value
crmForm.all.crmfieldscemaname.DataValue= "none";

//Get PickList Value
var value=crmForm.all.crmfieldscemaname.DataValue;

RetrieveMultiple method using Javascript

This sample shows how to use the CrmService.RetrieveMultiple method using the same example provided in the following topic: CrmService.RetrieveMultiple method.


// Prepare variables to retrieve the contacts.
var searchCity = "Sammamish";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = ""+ 
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
authenticationHeader+ 
""+ 
""+ 
"" xsi:type='q1:QueryExpression'>"+ 
"contact"+ 
""+ 
""+ 
"fullname"+ 
"contactid"+ 
""+ 
""+ 
"false"+ 
""+ 
"And"+ 
""+ 
""+ 
"address1_city"+ 
"Like"+ 
""+ 
""+searchCity+""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
""+ 
"";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
 var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
 alert(msg);
}
// Parse and display the results.
else
{
 var results = resultXml.getElementsByTagName('BusinessEntity');
 var msg = "";
 if (results.length == 0)
 {
  msg = "No contacts were found in "+searchCity+".";
  alert(msg);
  return;
 }
 else
 {
  msg = "Contacts found in "+searchCity+":\r";
  msg += "\Contact Id\t\t\t\tContact Full Name\r";
  msg +="--------------------------------------------------------------------------------\r";
    for (i=0;i < results.length;i++)
    {
     var idValue = results[i].selectSingleNode('./q1:contactid').nodeTypedValue;
     var name = results[i].selectSingleNode('./q1:fullname').nodeTypedValue;
     msg += idValue +"\t"+ name+"\r";
    }
    alert(msg);
 }
}

Update Method Using JScript

This sample shows how to use the CrmService.Update method using the same example provided in the following topic: CrmService.Update method.
// Prepare variables for updating a contact.
var contactId = "56914948-991C-DD11-AD3A-0003FF9EE217";
var newAddressLine1 = "34 Market St.";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = ""+ 
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
authenticationHeader+
""+ 
""+ 
""+ 
""+newAddressLine1+""+ 
""+contactId+""+ 
""+ 
""+ 
""+ 
"";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
 var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
 alert(msg);
}
// Display a confirmation message and open the updated contact.
else
{
alert("Contact with id = "+contactId+" successfully updated.");
window.open("/sfa/conts/edit.aspx?id={"+contactId+"}");
}

Tuesday 11 February 2014

CrmService.Create Method Using JScript

This sample shows how to use the CrmService.Create method using the same example provided in the Server Programming Guide



// Prepare values for the new contact.
var firstname = "Jesper";
var lastname = "Aaberg";
var donotbulkemail = "true";
var address1_stateorprovince = "MT";
var address1_postalcode = "99999";
var address1_line1 = "23 Market St.";
var address1_city = "Sammamish";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "" +
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
authenticationHeader+
""+
""+
""+
""+address1_city+""+
""+address1_line1+""+
""+address1_postalcode+""+
""+address1_stateorprovince+""+
""+donotbulkemail+""+
""+firstname+""+
""+lastname+""+
"
"+
"
"+
"
"+
"
";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Create");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
// Open new contact record if no errors.
else
{
var contactid = resultXml.selectSingleNode("//CreateResult");
window.open("/sfa/conts/edit.aspx?id={"+contactid.nodeTypedValue+"}");
}

A successful response includes XML with a CreateResponse element that returns the ID for the record created. The following is an example of a successful response:





368c8b1b-851c-dd11-ad3a-0003ff9ee217