Share via


Use XRM tooling to create data

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

There are seven methods available in the CrmServiceClient class for creating new data and associations. A create action using the XRM tooling API requires a data payload. The data payload takes the form of a Dictionary<string, CrmDataTypeWrapper> object. CrmDataTypeWrapper is used to inform the interface what sort of handling needs to be applied to the data point you are referencing. Some of the methods for creating data are listed in this topic.

CreateNewRecord

This method is used to create any type of entity data in Microsoft Dynamics 365. To use it, you need to know the schema name of the entity you want to create a record in, and must construct a data payload to pass to it. This example creates an account record.

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>",“<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you’re connected to.
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Create an account record
    Dictionary<string, CrmDataTypeWrapper> inData = new Dictionary<string, CrmDataTypeWrapper>();
    inData.Add("name", new CrmDataTypeWrapper("Sample Account Name", CrmFieldType.String));
    inData.Add("address1_city", new CrmDataTypeWrapper("Redmond", CrmFieldType.String));
    inData.Add("telephone1", new CrmDataTypeWrapper("555-0160", CrmFieldType.String));
    accountId = ctrl.CrmConnectionMgr.CrmSvc.CreateNewRecord("account", inData);

    // Verify if the account is created.
    if (accountId != Guid.Empty)
    {
        Console.WriteLine(“Account created.”);
    }
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

In this example, we created a data payload object called indata. Next, we populated it using the general syntax: crmFieldName , new CrmDataTypeWrapper(data,CrmFieldType). After setting up the indata object to get the values to create, we called CreateNewRecord method providing the entity logical name for the account and the data payload (indata).

Note

You can also create an entity record using XRM tooling by executing the CreateRequest message with the ExecuteCrmOrganizationRequest method. More information: Use messages (request and response classes) with the ExecuteCrmOrganizationRequest method

CreateAnnotation

This method is used to create and attach a note object to any entity record. While you can populate all the variables for the note in the first pass, you only need to provide subject and note text fields. In practice, this is generally used to attach system-generated notes to an entity, or to attach files that are stored in Dynamics 365 to an entity. Additionally, if you provide your own UI for creating notes for your user, this is how you would attach that note to the owner entity in Dynamics 365. This example continues from the prior example to create a note on the newly created account.

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", “<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected.
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you are connected to.
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Create and attach a note.
    inData.Clear(); 
    inData.Add("subject", new CrmDataTypeWrapper("This is a NOTE from the API" , CrmFieldType.String)); 
    inData.Add("notetext", new CrmDataTypeWrapper("This is text that will go in the body of the note" , CrmFieldType.String));
    Guid noteID = crmSvc.CreateAnnotation("account", accountId, inData);
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

See Also

Sample: Quick start for XRM Tooling API
Use XRM tooling to execute actions in Dynamics 365

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright