InstantiateTemplate Message (CrmService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Works for all deployment types Works online and offline

Creates an e-mail message from a template.

The relevant classes are specified in the following table.

Type Class
Request InstantiateTemplateRequest
Response InstantiateTemplateResponse
Entity template

Remarks

To use this message, pass an instance of the InstantiateTemplateRequest class as the request parameter in the Execute method.

To perform this action, the caller must access rights on the template entity instance. For a list of required privileges, see InstantiateTemplate Privileges.

Example

The following code shows how to use the InstantiateTemplate message.

[C#]
// Set up the CRM service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0; 
token.OrganizationName = "AdventureWorksCycle";
 
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create a template instantiation request.
InstantiateTemplateRequest instTemplate = new InstantiateTemplateRequest();

// The email template to use.
instTemplate.TemplateId = new Guid("9AF74EF9-A04C-DA11-A0C5-000D9DD8CDAC");

// The object that this email is regarding.
instTemplate.ObjectId = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
instTemplate.ObjectType = EntityName.account.ToString();

//Uncomment this next line to return dynamic entities.
//instTemplate.ReturnDynamicEntities = true;

// Execute the request to create an email message from the template.
InstantiateTemplateResponse instTemplateResponse = 
   (InstantiateTemplateResponse)service.Execute(instTemplate);

// Serialize the email message to XML and save to a file.
XmlSerializer serializer = new XmlSerializer(typeof(InstantiateTemplateResponse));
string filename = "email-message.xml";
StreamWriter writer = new StreamWriter(filename);
serializer.Serialize(writer, instTemplateResponse);
writer.Close();

[Visual Basic .NET]
' Set up the CRM service.
Dim token As New CrmAuthenticationToken()
' You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";
 
Dim service As New CrmService()
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials

' Create a template instantiation request.
Dim instTemplate As InstantiateTemplateRequest = New InstantiateTemplateRequest

' The email template to use.
instTemplate.TemplateId = New Guid("9AF74EF9-A04C-DA11-A0C5-000D9DD8CDAC");

' The object that this email is regarding.
instTemplate.ObjectId = New Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
instTemplate.ObjectType = EntityName.account.ToString

'Uncomment this next line to return dynamic entities.
'instTemplate.ReturnDynamicEntities = true;

' Execute the request to create an email message from the template.
Dim instTemplateResponse As InstantiateTemplateResponse = CType(service.Execute(instTemplate), InstantiateTemplateResponse)

' Serialize the email message to XML and save to a file.
Dim serializer As XmlSerializer = New XmlSerializer(GetType(InstantiateTemplateResponse))
Dim filename As String = "email-message.xml"
Dim writer As StreamWriter = New StreamWriter(filename)
serializer.Serialize(writer, instTemplateResponse)
writer.Close()

See Also

Concepts

Reference

© 2010 Microsoft Corporation. All rights reserved.