Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dataverse/orgsvc/.claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"permissions": {
"allow": [
"Bash(dir:*)",
"Bash(dir /s /b \"C:\\\\GitHub\\\\microsoft\\\\PowerApps-Samples\\\\dataverse\\\\orgsvc\\\\CSharp-NETCore\")",
"Bash(findstr:*)",
"Bash(ls:*)",
"Bash(git checkout:*)",
"Bash(git add:*)",
"Bash(git commit:*)",
"Bash(pwsh:*)",
"Bash(dotnet sln:*)",
"Bash(dotnet new:*)",
"Bash(dotnet build:*)"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>PowerPlatform.Dataverse.CodeSamples</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Include="..\appsettings.json" Link="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.1.14" />
</ItemGroup>

</Project>
164 changes: 164 additions & 0 deletions dataverse/orgsvc/CSharp-NETCore/Activities/BookAppointment/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Extensions.Configuration;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;

namespace PowerPlatform.Dataverse.CodeSamples
{
/// <summary>
/// Demonstrates how to book or schedule an appointment using the BookRequest message.
/// </summary>
/// <remarks>
/// This sample shows how to create and book an appointment using the BookRequest message,
/// which validates that the appointment can be scheduled and creates it in the system.
///
/// Set the appropriate Url and Username values for your test
/// environment in the appsettings.json file before running this program.
/// </remarks>
class Program
{
private static readonly List<EntityReference> entityStore = new();

#region Sample Methods

/// <summary>
/// Sets up sample data required for the demonstration
/// </summary>
private static void Setup(ServiceClient service)
{
// No setup required for this sample
}

/// <summary>
/// Demonstrates how to book an appointment using BookRequest
/// </summary>
private static void Run(ServiceClient service)
{
Console.WriteLine("Booking an appointment...");

// Get the current user information
var userRequest = new WhoAmIRequest();
var userResponse = (WhoAmIResponse)service.Execute(userRequest);

// Create the ActivityParty instance.
var party = new Entity("activityparty")
{
["partyid"] = new EntityReference("systemuser", userResponse.UserId)
};

// Create the appointment instance.
var appointment = new Entity("appointment")
{
["subject"] = "Test Appointment",
["description"] = "Test Appointment created using the BookRequest Message.",
["scheduledstart"] = DateTime.Now.AddHours(1),
["scheduledend"] = DateTime.Now.AddHours(2),
["location"] = "Office",
["requiredattendees"] = new EntityCollection(new List<Entity> { party }),
["organizer"] = new EntityCollection(new List<Entity> { party })
};

// Use the Book request message.
var book = new BookRequest
{
Target = appointment
};
var booked = (BookResponse)service.Execute(book);
Guid appointmentId = booked.ValidationResult.ActivityId;

// Add to entityStore for cleanup
entityStore.Add(new EntityReference("appointment", appointmentId));

// Verify that the appointment has been scheduled.
if (appointmentId != Guid.Empty)
{
Console.WriteLine("Successfully booked appointment: {0}", appointment["subject"]);
Console.WriteLine("Appointment ID: {0}", appointmentId);
}
}

/// <summary>
/// Cleans up sample data created during execution
/// </summary>
private static void Cleanup(ServiceClient service, bool deleteCreatedRecords)
{
if (deleteCreatedRecords && entityStore.Count > 0)
{
Console.WriteLine("\nDeleting {0} created record(s)...", entityStore.Count);
foreach (var entityRef in entityStore)
{
service.Delete(entityRef.LogicalName, entityRef.Id);
}
Console.WriteLine("Records deleted.");
}
}

#endregion

#region Application Setup

/// <summary>
/// Contains the application's configuration settings.
/// </summary>
IConfiguration Configuration { get; }

/// <summary>
/// Constructor. Loads the application configuration settings from a JSON file.
/// </summary>
Program()
{
// Get the path to the appsettings file. If the environment variable is set,
// use that file path. Otherwise, use the runtime folder's settings file.
string? path = Environment.GetEnvironmentVariable("DATAVERSE_APPSETTINGS");
if (path == null) path = "appsettings.json";

// Load the app's configuration settings from the JSON file.
Configuration = new ConfigurationBuilder()
.AddJsonFile(path, optional: false, reloadOnChange: true)
.Build();
}

static void Main(string[] args)
{
Program app = new();

// Create a Dataverse service client using the default connection string.
ServiceClient serviceClient =
new(app.Configuration.GetConnectionString("default"));

if (!serviceClient.IsReady)
{
Console.WriteLine("Failed to connect to Dataverse.");
Console.WriteLine("Error: {0}", serviceClient.LastError);
return;
}

Console.WriteLine("Connected to Dataverse.");
Console.WriteLine();

bool deleteCreatedRecords = true;

try
{
Setup(serviceClient);
Run(serviceClient);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred:");
Console.WriteLine(ex.Message);
}
finally
{
Cleanup(serviceClient, deleteCreatedRecords);

Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
serviceClient.Dispose();
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
languages:
- csharp
products:
- power-platform
- power-apps
page_type: sample
description: "Demonstrates how to book or schedule an appointment using the BookRequest message."
---

# Book an Appointment

Demonstrates how to book or schedule an appointment using the BookRequest message.

## What this sample does

The `BookRequest` message is used to book or schedule an appointment. This validates that the appointment can be scheduled according to the calendars of the required attendees and creates the appointment in the system.

## How this sample works

### Setup

This sample requires no setup. It uses the current user as both organizer and attendee.

### Run

The sample demonstrates the following steps:

1. Gets the current user information using WhoAmIRequest
2. Creates an ActivityParty for the current user
3. Creates an appointment entity with:
- Subject and description
- Scheduled start and end times
- Location
- Required attendees and organizer
4. Uses BookRequest to validate and create the appointment
5. Verifies the appointment was successfully booked

### Cleanup

The sample deletes the created appointment record by default.

## Demonstrates

- Using `WhoAmIRequest` to get current user information
- Creating an `ActivityParty` entity to represent appointment participants
- Using `BookRequest` message to book an appointment
- Working with appointment scheduling attributes

## Sample Output

```
Connected to Dataverse.

Booking an appointment...
Successfully booked appointment: Test Appointment
Appointment ID: 12345678-1234-1234-1234-123456789012

Deleting 1 created record(s)...
Records deleted.

Press any key to exit.
```

## See also

[BookRequest Class](https://learn.microsoft.com/dotnet/api/microsoft.crm.sdk.messages.bookrequest)
[Appointment entities](https://learn.microsoft.com/power-apps/developer/data-platform/appointment-entities)
[Activity entities](https://learn.microsoft.com/power-apps/developer/data-platform/activity-entities)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>PowerPlatform.Dataverse.CodeSamples</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Include="..\appsettings.json" Link="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.1.14" />
</ItemGroup>

</Project>
Loading