---
title: Consents
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Dependency injection

### Initialize required services

```csharp
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IInfoProvider<ConsentInfo> consentInfoProvider;
private readonly IConsentAgreementService consentAgreementService;
private readonly IInfoProvider<ContactInfo> contactInfoProvider;

public ConsentsServices(IInfoProvider<ConsentInfo> consentInfoProvider,
                         IConsentAgreementService consentAgreementService,
                         IInfoProvider<ContactInfo> contactInfoProvider)
{
    this.consentInfoProvider = consentInfoProvider;
    this.consentAgreementService = consentAgreementService;
    this.contactInfoProvider = contactInfoProvider;
}
```

[> Back to list of examples](#toc)

## Consents

### Create consents

```csharp
// Creates a new consent object
ConsentInfo newConsent = new ConsentInfo()
{
    ConsentName = "NewConsent",
    ConsentDisplayName = "New Consent",
};

// Defines and sets content of the consent
string shortText = "This is a sample consent.";
string fullText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
newConsent.UpsertConsentText("english", shortText, fullText);

// Saves the created consent to the database
newConsent.Update();
```

[> Back to list of examples](#toc)

### Update consents

```csharp
// Gets the consent
ConsentInfo existingConsent = consentInfoProvider.Get("NewConsent");

// Defines and updates content of the consent
ConsentText consentText = await existingConsent.GetConsentTextAsync("english");
string shortText = consentText.ShortText.Replace("consent", "consent declaration");
string fullText = consentText.FullText;
existingConsent.UpsertConsentText("english", shortText, fullText);

// Saves the created consent object to the database.
// If at least one contact gave an agreement with this consent,
// the content of the consent is archived before updating.
existingConsent.Update();
```

[> Back to list of examples](#toc)

### Create consent language versions

```csharp
// Gets the consent
ConsentInfo existingConsent = consentInfoProvider.Get("NewConsent");

// Defines and updates content of the Spanish language variant
string shortText = "Esta es una muestra de consentimiento.";
string fullText = "ESP Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
existingConsent.UpsertConsentText("spanish", shortText, fullText);

// Saves the created consent object to the database
existingConsent.Update();
```

[> Back to list of examples](#toc)

### Delete consents

```csharp
// Gets the consent
ConsentInfo deleteConsent = consentInfoProvider.Get("NewConsent");

if (deleteConsent != null)
{
    // Deletes the consent
    consentInfoProvider.Delete(deleteConsent);
}
```

[> Back to list of examples](#toc)

## Consent agreements

### Create a consent agreement

```csharp
// Gets the consent
ConsentInfo consent = consentInfoProvider.Get("NewConsent");

// Gets the first contact whose name is John Doe
ContactInfo contact = contactInfoProvider.Get()
                                         .WhereEquals("ContactLastname", "Doe")
                                         .WhereEquals("ContactFirstName", "John")
                                         .TopN(1)
                                         .FirstOrDefault();

// Creates a consent agreement signifying that the contact has given an agreement with the consent
consentAgreementService.Agree(contact, consent);
```

[> Back to list of examples](#toc)

### Check a consent agreement

```csharp
// Gets the consent
ConsentInfo consent = consentInfoProvider.Get("NewConsent");

// Gets the first contact whose name is John Doe
ContactInfo contact = contactInfoProvider.Get()
                                         .WhereEquals("ContactLastname", "Doe")
                                         .WhereEquals("ContactFirstName", "John")
                                         .TopN(1)
                                         .FirstOrDefault();

// Checks if the contact has given an agreement with the consent
bool agreed = consentAgreementService.IsAgreed(contact, consent);
```

[> Back to list of examples](#toc)

### Revoke a consent agreement

```csharp
// Gets the consent
ConsentInfo consent = consentInfoProvider.Get("NewConsent");

// Gets the first contact whose name is John Doe
ContactInfo contact = contactInfoProvider.Get()
                                         .WhereEquals("ContactLastname", "Doe")
                                         .WhereEquals("ContactFirstName", "John")
                                         .TopN(1)
                                         .FirstOrDefault();

// Revokes the previously created consent agreement
consentAgreementService.Revoke(contact, consent);
```

[> Back to list of examples](#toc)

### Get the consent agreements of a contact

```csharp
// Gets the first contact whose name is John Doe
ContactInfo contact = contactInfoProvider.Get()
                                         .WhereEquals("ContactLastname", "Doe")
                                         .WhereEquals("ContactFirstName", "John")
                                         .TopN(1)
                                         .FirstOrDefault();

// Gets the list of consents with which the contact gave a consent agreement.
// The 'Consent' object contains information about a consent declaration. The 'GetConsentText'
// method gets the content of the consent from either the current consent declaration or from
// the consent archive, depending on the version of the consent with which the contact gave
// the agreement.
IEnumerable<Consent> consents = consentAgreementService.GetAgreedConsents(contact);

// Iterates through the retrieved consents
foreach (Consent consent in consents)
{
    // Gets the properties of the individual consents
    string displayName = consent.DisplayName;
    int id = consent.Id;

    // Gets the content of the consent
    string shortText = consent.GetConsentText("english").ShortText;
    string fullText = consent.GetConsentText("english").FullText;
}
```

[> Back to list of examples](#toc)
