---
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:

## Consents

### Creating a consent declaration

```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("en-us", shortText, fullText);

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

```

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

### Updating a consent declaration

```csharp

// Gets the consent
ConsentInfo existingConsent = ConsentInfo.Provider.Get("NewConsent");

// Defines and updates content of the consent
string shortText = existingConsent.GetConsentText("en-us").ShortText.Replace("consent", "consent declaration");
string fullText = existingConsent.GetConsentText("en-us").FullText;
existingConsent.UpsertConsentText("en-us", 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)

### Creating a consent language version

```csharp

// Gets the consent
ConsentInfo existingConsent = ConsentInfo.Provider.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("es-es", shortText, fullText);

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

```

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

### Deleting a consent declaration

```csharp

// Gets the consent
ConsentInfo deleteConsent = ConsentInfo.Provider.Get("NewConsent");

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

```

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

## Consent agreements

### Creating a consent agreement

```csharp

// Gets the consent
ConsentInfo consent = ConsentInfo.Provider.Get("NewConsent");

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

// Initializes an instance of the consent agreement service
IConsentAgreementService consentAgreementService = Service.Resolve<IConsentAgreementService>();

// 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)

### Checking a consent agreement

```csharp

// Gets the consent
ConsentInfo consent = ConsentInfo.Provider.Get("NewConsent");

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

// Initializes an instance of the consent agreement service
IConsentAgreementService consentAgreementService = Service.Resolve<IConsentAgreementService>();

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

```

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

### Revoking a consent agreement

```csharp

// Gets the consent
ConsentInfo consent = ConsentInfo.Provider.Get("NewConsent");

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

// Initializes an instance of the consent agreement service
IConsentAgreementService consentAgreementService = Service.Resolve<IConsentAgreementService>();

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

```

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

### Getting the consent agreements of a contact

```csharp

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

// Initializes an instance of the consent agreement service
IConsentAgreementService consentAgreementService = Service.Resolve<IConsentAgreementService>();

// 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("en-us").ShortText;
    string fullText = consent.GetConsentText("en-us").FullText;
}

```

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