Freigeben über


Create IntelliSense Code Snippets for CRM

Visual Studio 2005 IntelliSense Code Snippets feature enables pre-authored pieces of code to be created that can later be shared and inserted into your application. Code snippets increase productivity by reducing the amount of time spent typing repetitive code or searching for samples.

As far as CRM programmability is concerned, Snippets add a lot of value because often you need to perform repetitive tasks when working with our web services (book an appointment, upload and attach notes to entities, etc). To showcase how code snippets can help CRM developers I created a simple one below.

This code snippet Books a CRM Appointment by taking advantage of CRM V3.0 powerful Service Scheduling feature. It takes some basic parameters such as the location, subject, time of the appointment etc as input and uses the BookRequest message in CRM Web services to book the appointment. BookRequest honors availability of the user and business closures, etc. If user has other appointments or not available due to being out of the office or on vacation, the method simply refrains from booking the appointment informing the caller that user is not available at that time. This is one of the differences between Booking an appointment vs just Creating it.

In order to create a code snippets you need to create an XML (example shown below) and add it to Visual Studio using Code Snippet Manager. It is pretty easy and you are will br done with a few clicks.

Once you add your code snippets to VS, there are multiple ways to insert them into your code using IntelliSense or keyboard shortcuts. My favorite method is CTRL+K followed by CTRL+X. This will show you this code snippet dialog that you can choose from.

I want to talk with our User Education team to see if we can deliver some of our code samples using Code snippets.

CRM Code Snippet XML: Create a text file named CRMSnippets.snippet (or whatever name you fancy, the file extension should be "snippet" though) and copy this content into the file. Save the file on your dev machin and using the snippet manager point to the file to get it registered with VS.

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<

CodeSnippet Format="1.0.0">

<

Header>

<

Title>Book a CRM Appointment</Title>
<Shortcut>BookCRMAppointment</Shortcut>

</Header>

<

Snippet>

<

Code Language="CSharp">

<![CDATA[

//Books a CRM appointment that takes into account the user availability

//and business rules

public Guid BookMyAppointment (Guid myId, string subject, string description, string location, string start, string end)

{

// Create the activityparty

activityparty myParty = new activityparty();

myParty.partyid = CrmTypes.CreateLookup(EntityName.systemuser.ToString(), myId);

// Create the appointment Object

appointment myAppointment = new appointment();

// Set the appointment Object's properties

myAppointment.description = description;

myAppointment.ownerid = CrmTypes.CreateOwner(EntityName.systemuser.ToString(), myId);

myAppointment.scheduledstart = CrmTypes.CreateCrmDateTime(start);

myAppointment.scheduledend = CrmTypes.CreateCrmDateTime(end);

myAppointment.location = location;

myAppointment.subject = subject;

myAppointment.requiredattendees = new activityparty[] { myParty };

//Create the Target

TargetScheduleAppointment myAppointmentTarget = new TargetScheduleAppointment();

//Set the Target's Properties

myAppointmentTarget.Appointment = myAppointment;

// Create the Request Object

BookRequest myBookRequest = new BookRequest();

// Set the Request Object's Properties

myBookRequest.Target = myAppointmentTarget;

// Execute the Request

BookResponse booked = (BookResponse)myService.Execute(myBookRequest);

if (booked.ValidationResult.ValidationSuccess)

{

return booked.ValidationResult.ActivityId;

}

else return new Guid();

}

]]>

</

Code>

</Snippet>

</

CodeSnippet>

</

CodeSnippets>

CrnCodeSnippets.bmp

Comments

  • Anonymous
    July 17, 2006
    Arash,

    That is an excellent idea.  I would love to see all of the SDK examples as a code snippet library.

    Actually, what I would like more would be a public repository where any of us in the CRM community could contribute samples to.

    Thanks, Mitch
  • Anonymous
    October 10, 2006
    The following blog posts demonstrate techniques and tools designed to improve your efficiency and ease