versionFrom | verified-against | meta.Title | meta.Description |
---|---|---|---|
9.0.0 |
rc001 |
Media Model |
The Media class represents a single item in the media tree. |
The Media
class represents a single item in the media tree, its values are fetched directly from the database, not from the cache. Notice the Media class should strictly be used for CRUD operations. Media is already stored in cache, so for querying Media you'd want to use the IUmbracoContext.IPublishedMediaCache
to get the media. Then one would use LINQ to query and filter the collection.
- Namespace:
Umbraco.Cms.Core.Models
- Assembly:
Umbraco.Core.dll
All samples in this document will require references to the following dll:
- Umbraco.Core.dll
All samples in this document will require the following using statements:
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
Constructor for creating a new Media object where the necessary parameters are the name of the Media, the parent of the Media as an IMedia
object and the MediaType as an IMediaType
object for the Media being created.
Constructor for creating a new Media object where the necessary parameters are the name of the Media, the parent of the Media as an IMedia
object, the MediaType as an IMediaType
object and a IPropertyCollection
for the Media being created.
Constructor for creating a new Media object where the necessary parameters are the name of the Media, the id of the parent as int
and the MediaType as an IMediaType
object for the Media being created.
Constructor for creating a new Media object where the necessary parameters are the name of the Media, the id of the parent as int
, the MediaType as an IMediaType
object and a IPropertyCollection
for the Media being created.
Gets or Sets a DateTime
object, indicating then the given Media was created.
// Given a `MediaService` object get Media by its Id and return CreateDate
var media = mediaService.GetById(1234);
return media.CreateDate;
Gets or Sets the Id of the User
as an int
who created the Media.
// Given a `MediaService` object get Media by its Id and return the Id of the Creator
var media = mediaService.GetById(1234);
return media.CreatorId;
Returns a ISimpleContentType
object representing the ContentType used by the given Media
.
// Given a `MediaService` object get Media by its Id and return MediaType
var media = mediaService.GetById(1234);
return media.ContentType;
Returns the id as an int
of the MediaType
object representing the ContentType used by the given Media
.
// Given a `MediaService` object get Media by its Id and return the Id of the MediaType
var media = mediaService.GetById(1234);
return media.ContentTypeId;
Returns the unique Media
Id as a Int
, this ID is based on a Database identity field, and is therefore not safe to reference in code which are moved between different instances, use Key instead.
Returns the Guid
assigned to the Media during creation. This value is unique, and should never change, even if the Media is moved between instances.
// Given a `MediaService` object get Media by its Id and return the Key
var media = mediaService.GetById(1234);
return media.Key;
Gets or Sets the given Media
level in the site hierarchy as an Int
. Media placed at the root of the tree, will return 1, Media right underneath will return 2, and so on.
// Given a `MediaService` object get Media by its Id and return the Level
var media = mediaService.GetById(1234);
return media.Level;
Gets or Sets the name of the Media as a String
.
// Given a `MediaService` object get Media by its Id and return its Name
var media = mediaService.GetById(1234);
return media.Name;
Gets or Sets the parent Media
Id as an Int
.
// Given a `MediaService` object get Media by its Id and return the Id of the Parent Media
var media = mediaService.GetById(1234);
return media.ParentId;
Gets or Sets the path of the Media as a String
. This string contains a comma separated list of the ancestors Ids including the current medias own id at the end of the string.
// Given a `MediaService` object get Media by its Id and return the Path
var media = mediaService.GetById(1234);
return media.Path;
Gets or Sets the PropertyCollection
object, which is a collection of Property
objects. Each property corresponds to a PropertyType
, which is defined on the MediaType
.
// Given a `MediaService` object get Media by its Id and loop through all Properties
var media = mediaService.GetById(1234);
foreach(var property in media.Properties){
string alias = property.Alias;
object value = property.GetValue();
}
Returns the given Media
index, compared to sibling media.
// Given a `MediaService` object get Media by its Id and return its SortOrder
var media = mediaService.GetById(1234);
return media.SortOrder;
Returns a Bool
indicating whether the given Media
is currently in the recycle bin.
// Given a `MediaService` object get Media by its Id and return Trashed
var media = mediaService.GetById(1234);
return media.Trashed;
Gets or Sets a DateTime
object, indicating when the given Media was last updated.
// Given a `MediaService` object get Media by its Id and return UpdateDate
var media = mediaService.GetById(1234);
return media.UpdateDate;
Returns the current Version Id as a Guid
,
For each change made to a Media item, its values are stored under a new Version. This version is identified by a Guid
.
Changes the IMediaType
for the current Media object and removes PropertyTypes and Properties, which are not part of the new MediaType
. Please use with caution as this remove differences between the new and old MediaType.
// Given a `ContentTypeService` object get the MediaType that we're changing to,
// get the Media from the `MediaService` for which we want to change MediaType for,
// and then save the Media through the MediaService.
var mediaType = contentTypeService.GetMediaType(1122);
var media = mediaService.GetById(1234);
media.ChangeContentType(mediaType);
mediaService.Save(media);
Gets the IProfile
object for the Creator of this Media, which contains the Id and Name of the User who created this Media item.
// Given a `MediaService` object get Media by its Id and get the `IProfile` of the Creator
var media = mediaService.GetById(1234);
var profile = media.GetCreatorProfile();
var id = profile.Id;
string name = profile.Name;
Gets the value of a Property as an Object
.
// Given a `MediaService` object get Media by its Id and get a value by alias
var media = mediaService.GetById(1234);
object value = media.GetValue("height");
int text = int.Parse(value.ToString());
Gets the value of a Property as the defined type 'TPassType'.
// Given a `MediaService` object get Media by its Id and get a value by alias while specifying the return type
var media = mediaService.GetById(1234);
int value = media.GetValue<int>("height");
Returns a Bool
indicating whether the Media object has a property with the supplied alias.
// Given a `MediaService` object get Media by its Id and check if certain properties exist
var media = mediaService.GetById(1234);
bool tagsExists = media.HasProperty("myTagProperty");
bool textExists = media.HasProperty("altText");
Sets the value of a property by its alias.
// Given a `MediaService` object get Media by its Id, set a few values
// and saves the Media through the `MediaService`
var media = mediaService.GetById(1234);
media.SetValue("altText", "Alternative text for this media item");
media.SetValue("date", DateTime.Now);
mediaService.Save(media);
It is worth noting that it is also possible to pass a HttpPostedFile, HttpPostedFileBase or HttpPostedFileWrapper to the SetValue method, so it can be used for uploads.
Returns an XElement
containing the Media data, based off the latest changes. When the Media item is saved the xml is stored in the database.
// Given a `MediaService` object get Media by its Id and returns the xml
var media = mediaService.GetById(1234);
XElement xml = media.ToXml(serializer);
return xml;