Webhooks, snapshots and roles with .NET
The Content Management API (CMA) is a restful API for managing content in your Contentful spaces. You can use it to create, update, delete and retrieve content using well-known HTTP verbs.
To make development easier for our users, we publish client libraries for various languages which make the task easier. This article details how to use the .NET client library to create, update and delete webhooks, snapshots and roles.
Pre-requisites
This tutorial assumes you understand the basic Contentful data model as described in the developer center and that you have already read and understand the getting started tutorial for the .NET client library and the using the management API with Contentful and .NET client library article.
Contentful.net is built on .NET Core and targets .NET Standard 2.0. The client library is cross-platform and runs on Linux, macOS, and Windows.
Working with webhooks
To learn more about what webhooks refer to the webhooks concepts article.
To create a webhook for a space use the CreateWebhook method.
The topics available are described in more detail in the webhooks concepts article, but the summary is that you specify a Type and an Action. Type can be any of Entry, Asset or ContentType and Action can be any of create, save, auto_save, archive, unarchive, publish, unpublish or delete. _ is a wildcard and _.\* is valid
and would mean to call this webhook for all actions across all types.
Once you have created a webhook you can fetch them from the client library using the GetWebhooksCollection and GetWebhook methods.
To retrieve more information about a specific webhook call use the GetWebhookCallDetailsCollection to retrieve a list of calls made to the webhook or the GetWebhookCallDetails to get details of a specific call.
A method is available that gives a more general overview of the total number of webhook calls for a specific webhook and whether they returned a success code.
To delete a webhook you are no longer using.
Roles and memberships
The .NET client library allows for the creation of custom roles. It’s complex to understand the permissions and policies system, but the examples below should give you a good overview. For more detailed information, refer to the management API documentation.
Creating a role
Start by creating a new Role and adding the permissions and policies you need.
This example above would give the role permissions to read entries, assets, and content types, but not edit, create or delete them. It would also give permissions to manage settings for the space. The policies give this role-specific access to read, create and update entries.
Policies explained
The policies can look daunting and the framework behind them is complex. However, they do make it possible to create granular and flexible authorization rules.
The first property is the Effect which is how this policy is to be treated, will it allow or deny access.
Actions represents what actions this policy allows or denies. This is a list of strings but only a certain set of values are acceptable. These are read, create, update, delete, publish, unpublish, archive, unarchive or all.
Constraint is an IConstraint, which is explained in more detail below.
Constraints explained
Constraints represent which resources a specific policy applies to. There are five different kinds of constraints.
AndConstraint and OrConstraint are lists of other IConstraint. The OrConstraint ensures that at least one of the contained constraints is true and the AndConstraint requires all contained constraints to be true.
The NotConstraint contains another IConstraint and inverts the value of that constraint.
The EqualsConstraint contains a Property which is a field on the content type or asset and a ValueToEqual which contains the value that this field must equal for this constraint to evaluate to be fulfilled.
The PathConstraint contains a Fields property that gives a path that must exist on the content type. An example is "fields.heading" which would only match content types that have the heading field present.
Putting them all together looks something like the following.
Once you’ve created a Role and added the appropriate permissions and policies, you can call the CreateRole method.
You can update, delete and fetch roles with the appropriate methods.
Working with snapshots
A snapshot of an entry is automatically created each time an entry is published and is the state of every field of the entry at that given time. You can use the snapshots of an entry to keep a full version history of the entry content. For more information read the snapshots documentation.
To get all snapshots for an entry use the GetAllSnapshotsForEntry. To get a specific snapshot use the SingleSnapshot method.
Similarly, a snapshot of a content type is created each time a content type is published and can be fetched using the GetAllSnapshotsForContentType and GetSnapshotForContentType methods.
Working with space memberships
A space membership represents the membership type a user has in a space and you can assign additional roles to a user, flag a user as admin or remove a user from a space.
To get all memberships of a space call the GetSpaceMemberships method. To get a single membership call GetSpaceMembership method.
To update an existing membership use the UpdateSpaceMembership method.
To create a new membership use the CreateSpaceMembership method.
To delete a membership use the DeleteSpaceMembership method.
Working with API keys
The Contentful .NET client library allows you to get all API keys for the Content Delivery API ((not the management API) for a space. It also allows you to create new API keys.
Working with environments
To create an environment call the CreateOrUpdateEnvironment method.
This environment will be an identical copy to your current master environment. Note that it can take a while for the environment to be fully available depending on the size of the master environment.
To get a list of available environments use the GetEnvironments method.
To get a specific environment use the GetEnvironment method.
To delete an environment use the DeleteEnvironment method.