Skip to content

Quick start

Scott Durow edited this page May 4, 2022 · 9 revisions

From your terminal of choice:


mkdir quick-start
cd quick-start
npm init
npm install -g typescript
tsc --init
mkdir src
npm install --save-dev @types/xrm
npm install --save dataverse-ify

Now you need to auth against Dataverse using:

npx dataverse-auth [environment]

E.g.

npx dataverse-auth contosoorg.crm.dynamics.com

Now you can generate your types

npx dataverse-gen init

Follow the instructions - you can select the entities/actions/functions to include in the config

Now open VSCode:

Code .

You'll want to update your tsconfig to be similar to:

{
	"compilerOptions": {
		"target": "es6",
		"module": "commonjs",
		"moduleResolution": "node",
		"lib": [
			"es2015",
			"dom"
		],
		"rootDir": "src",
		"outDir": "lib",
		"strict": true,
		"alwaysStrict": true,
		"strictFunctionTypes": true,
		"strictNullChecks": true,
		"strictPropertyInitialization": true,
		"forceConsistentCasingInFileNames": true,
		"noImplicitAny": true,
		"noImplicitReturns": true,
		"noImplicitThis": true,
		"noFallthroughCasesInSwitch": true,
		"noUnusedLocals": true,
		"noUnusedParameters": true,
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true,
		"downlevelIteration": true,
		"declaration": true,
		"sourceMap": true,
		"pretty": true
	}
}

You can now start to write code using the XrmContextCdsServiceClient like:

setMetadataCache(metadataCache);
const serviceClient = new XrmContextCdsServiceClient(Xrm.WebApi);

const contact1 = {
logicalName: contactMetadata.logicalName,
firstname: "Jazzy",
lastname: "Jeff",
} as Contact;

// Create contact
contact1.id = await serviceClient.create(contact1);

// Create Account - notice primarycontactid is a simple EntityReference
const account1 = {
logicalName: accountMetadata.logicalName,
name: "Contoso",
primarycontactid: Entity.toEntityReference(contact1),
} as Account;

account1.id = await serviceClient.create(account1);

// Retreive Account (notice enum AccountAttributes)
const accountRetrieved = await serviceClient.retrieve<Account>(accountMetadata.logicalName, account1.id, [
AccountAttributes.PrimaryContactId,
]);

console.log(accountRetrieved);

You should get auto resolved references apart from 'Account' since there is a name collision. The imports will look something like this:

import { Entity, setMetadataCache, XrmContextCdsServiceClient } from "dataverse-ify";
import { AccountAttributes, accountMetadata } from "../dataverse-gen/entities/Account";
import { Contact, contactMetadata } from "../dataverse-gen/entities/Contact";
import { Account } from "../dataverse-gen/entities/Account";
import { metadataCache } from "../dataverse-gen/metadata";

For an example of a webpack configuration, check out a sample template repo cdsify-jswebresource-template

If you don't want to use the XrmContextCdsServiceClient - that's fine you can just use the cdsify methods to transform the raw WebApi request/responses.