Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
List of service references along with instructions on how to use them, as well as some examples for better understanding.
In this article you can learn how to use and work with some of the services provided with Umbraco CMS.
You can find a list of all supported services in the API Documentation.
All services can be accessed with the following using statement:
In some cases, you can use Dependency Injection. For example, if you have registered your class in Umbraco's dependency injection, you can specify the service interface in your constructor.
To use the NotificationService
you can use Dependency Injection via the INotificationService
interface like this:
In Razor views, you can access the Notification Service through the @inject
directive:
Use the above example for other services by replacing the interface and the name of the service.
A service for handling lawful data processing requirements.
A consent is fully identified by a source (whoever is consenting), a context (for example, an application), and an action (whatever is consented). A consent state registers the state of the consent (granted, revoked...).
Consent can be given or revoked or changed via the RegisterConsent
method, which creates a new Consent
entity to track the consent.
Getter methods of this service return the current state of a consent, that is the latest IConsent entity that was created.
Revoking a consent is performed by registering a revoked consent.
A consent cannot be deleted. It can only be revoked by registering a "revoked consent".
Examples on how to create a new folder and a new media item from a stream by using the MediaService.
In this article, you can find some examples on how to create a new media folder and a media item from a stream programmatically.
Samples in this document will require the following using statements:
To create a new folder at the root of the media archive, your code could look like the following:
Alternatively, you can replace the Constants in the above sample with hardcoded values.
For the CreateMedia
method, the first parameter is the name of the folder to be created.
The second parameter is the ID of the parent media item. Constants.System.Root
is a constant defined in Umbraco with the value of -1
, which is used for indicating the root of the media archive. Instead of specifying the numeric ID of the parent, you may instead specify either a Guid
ID or an IMedia
instance representing the parent media.
The third parameter is the alias of the Media Type. As Umbraco comes with a Folder Type by default, we can use the Constants.Conventions.MediaTypes.Folder
constant to specify that the alias of the Media Type is Folder
.
Besides the three mandatory parameters, you can specify a user's numeric ID for media creation attribution. Unspecified cases default to the "Administrator" user with ID -1
.
You can specify a Stream
for the contents of the file that should be created.
As an example, if you have an image on disk named unicorn.jpg
in the images folder of wwwroot
. You can open a new stream for a file on the disk, and then create a new media item for that file in Umbraco:
Please be aware that you will need to inject the following services:
MediaFileManager _mediaFileManager
IShortStringHelper _shortStringHelper
IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider
MediaUrlGeneratorCollection _mediaUrlGeneratorCollection
IMediaService _mediaService
IWebHostEnvironment _webHostEnvironment
Again Umbraco will make sure the necessary properties are updated.
Example on how to create content programmatically using the ContentService.
Learn how to use the Content Service.
In the example below, a new page is programmatically created using the content service. It is assumed that there are two document types, namely Catalogue and Product. In this case, a new Product is added underneath the Catalogue page. Add the below code in the Catalogue template.
In a multilanguage setup, it is necessary to set the name of the content item for a specified culture:
For information on how to retrieve multilingual languages, see the Retrieving languages article.
This will show you how to perform various User management using the Umbraco service layer.
Learn how to use the User Service to manage the users on your Umbraco project.
To assign a User to a User Group, we need both the IUserService
and IUserGroupService
. As with all Umbraco services, these are obtained using dependency injection.
Start by defining an interface for our implementation:
Next we implement the interface. This implementation holds the dependency to the Umbraco services:
Register the implementation in a Composer:
Lastly, we need to put our implementation to use. This could be done in a Management API controller:
Example on how to retrieve languages using the LocalizationService.
Learn how to use the Localization service to retrieve languages.
The localization service contains a number of methods for looking up languages. If you already know the ID of a specific language (eg. the default language has ID 1
), you can use the GetLanguageById
method to get the reference to that language:
As an alternative, you can look up a language by its ISO code via the GetLanguageByIsoCode
method:
The ISO code is a combination of the two-letter ISO 639-1 language code (lowercase) and two-letter ISO-3166 country code (uppercase). Eg. en-US
for English in the United States, en-GB
for English in the United Kingdom and da-DK
for Danish in Denmark.
Both methods will return an instance of the ILanguage interface, which has traditional properties like Id
and Key
, but also properties specific to the language like CultureName
, CultureInfo
and IsoCode
. You can see the API reference for further information on the properties of the interface.
If you need instead need a list of all installed languages, you can use the GetAllLanguages
method. It takes no parameters, and as such a returns a collection of all languages (with no pagination like some of the other services):
As shown in the example above, you can get the System.Globalization.CultureInfo
instance of each language. The CultureInfo determines how numbers, dates and similar should be either parsed or formatted in .NET.
Below you can see a full example of the examples shown above - including the necessary imports: