|
| 1 | +--- |
| 2 | +versionFrom: 9.0.0 |
| 3 | +verified-against: rc001 |
| 4 | +meta.Title: "Media Model" |
| 5 | +meta.Description: "The Media class represents a single item in the media tree." |
| 6 | +--- |
| 7 | + |
| 8 | +# Media |
| 9 | + |
| 10 | +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](https://our.umbraco.com/documentation/Reference/Querying/IPublishedContent/Collections/index-v9). |
| 11 | + |
| 12 | +* **Namespace:** `Umbraco.Cms.Core.Models` |
| 13 | +* **Assembly:** `Umbraco.Core.dll` |
| 14 | + |
| 15 | +All samples in this document will require references to the following dll: |
| 16 | + |
| 17 | +* Umbraco.Core.dll |
| 18 | + |
| 19 | +All samples in this document will require the following using statements: |
| 20 | + |
| 21 | +```csharp |
| 22 | +using Umbraco.Cms.Core.Models; |
| 23 | +using Umbraco.Cms.Core.Services; |
| 24 | +``` |
| 25 | + |
| 26 | +## Constructors |
| 27 | + |
| 28 | +### new Media(string name, IMedia parent, IMediaType mediaType) |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +### new Media(string name, IMedia parent, IMediaType mediaType, IPropertyCollection properties) |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +### new Media(string name, int parentId, IMediaType mediaType) |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +### new Media(string name, int parentId, IMediaType mediaType, IPropertyCollection properties) |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +## Properties |
| 45 | + |
| 46 | +### .CreateDate |
| 47 | + |
| 48 | +Gets or Sets a `DateTime` object, indicating then the given Media was created. |
| 49 | + |
| 50 | +```csharp |
| 51 | +// Given a `MediaService` object get Media by its Id and return CreateDate |
| 52 | +var media = mediaService.GetById(1234); |
| 53 | +return media.CreateDate; |
| 54 | +``` |
| 55 | + |
| 56 | +### .CreatorId |
| 57 | + |
| 58 | +Gets or Sets the Id of the `User` as an `int` who created the Media. |
| 59 | + |
| 60 | +```csharp |
| 61 | +// Given a `MediaService` object get Media by its Id and return the Id of the Creator |
| 62 | +var media = mediaService.GetById(1234); |
| 63 | +return media.CreatorId; |
| 64 | +``` |
| 65 | + |
| 66 | +### .ContentType |
| 67 | + |
| 68 | +Returns a `ISimpleContentType` object representing the ContentType used by the given `Media`. |
| 69 | + |
| 70 | +```csharp |
| 71 | +// Given a `MediaService` object get Media by its Id and return MediaType |
| 72 | +var media = mediaService.GetById(1234); |
| 73 | +return media.ContentType; |
| 74 | +``` |
| 75 | + |
| 76 | +### .ContentTypeId |
| 77 | + |
| 78 | +Returns the id as an `int` of the `MediaType` object representing the ContentType used by the given `Media`. |
| 79 | + |
| 80 | +```csharp |
| 81 | +// Given a `MediaService` object get Media by its Id and return the Id of the MediaType |
| 82 | +var media = mediaService.GetById(1234); |
| 83 | +return media.ContentTypeId; |
| 84 | +``` |
| 85 | + |
| 86 | +### .Id |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +### .Key |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +```csharp |
| 95 | +// Given a `MediaService` object get Media by its Id and return the Key |
| 96 | +var media = mediaService.GetById(1234); |
| 97 | +return media.Key; |
| 98 | +``` |
| 99 | + |
| 100 | +### .Level |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +```csharp |
| 105 | +// Given a `MediaService` object get Media by its Id and return the Level |
| 106 | +var media = mediaService.GetById(1234); |
| 107 | +return media.Level; |
| 108 | +``` |
| 109 | + |
| 110 | +### .Name |
| 111 | + |
| 112 | +Gets or Sets the name of the Media as a `String`. |
| 113 | + |
| 114 | +```csharp |
| 115 | +// Given a `MediaService` object get Media by its Id and return its Name |
| 116 | +var media = mediaService.GetById(1234); |
| 117 | +return media.Name; |
| 118 | +``` |
| 119 | + |
| 120 | +### .ParentId |
| 121 | + |
| 122 | +Gets or Sets the parent `Media` Id as an `Int`. |
| 123 | + |
| 124 | +```csharp |
| 125 | +// Given a `MediaService` object get Media by its Id and return the Id of the Parent Media |
| 126 | +var media = mediaService.GetById(1234); |
| 127 | +return media.ParentId; |
| 128 | +``` |
| 129 | + |
| 130 | +### .Path |
| 131 | + |
| 132 | +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. |
| 133 | + |
| 134 | +```csharp |
| 135 | +// Given a `MediaService` object get Media by its Id and return the Path |
| 136 | +var media = mediaService.GetById(1234); |
| 137 | +return media.Path; |
| 138 | +``` |
| 139 | + |
| 140 | +### .Properties |
| 141 | + |
| 142 | +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`. |
| 143 | + |
| 144 | +```csharp |
| 145 | +// Given a `MediaService` object get Media by its Id and loop through all Properties |
| 146 | +var media = mediaService.GetById(1234); |
| 147 | +foreach(var property in media.Properties){ |
| 148 | + string alias = property.Alias; |
| 149 | + object value = property.GetValue(); |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### .SortOrder |
| 154 | + |
| 155 | +Returns the given `Media` index, compared to sibling media. |
| 156 | + |
| 157 | +```csharp |
| 158 | +// Given a `MediaService` object get Media by its Id and return its SortOrder |
| 159 | +var media = mediaService.GetById(1234); |
| 160 | +return media.SortOrder; |
| 161 | +``` |
| 162 | + |
| 163 | +### .Trashed |
| 164 | + |
| 165 | +Returns a `Bool` indicating whether the given `Media` is currently in the recycle bin. |
| 166 | + |
| 167 | +```csharp |
| 168 | +// Given a `MediaService` object get Media by its Id and return Trashed |
| 169 | +var media = mediaService.GetById(1234); |
| 170 | +return media.Trashed; |
| 171 | +``` |
| 172 | + |
| 173 | +### .UpdateDate |
| 174 | + |
| 175 | +Gets or Sets a `DateTime` object, indicating when the given Media was last updated. |
| 176 | + |
| 177 | +```csharp |
| 178 | +// Given a `MediaService` object get Media by its Id and return UpdateDate |
| 179 | +var media = mediaService.GetById(1234); |
| 180 | +return media.UpdateDate; |
| 181 | +``` |
| 182 | + |
| 183 | +### .Version |
| 184 | + |
| 185 | +Returns the current Version Id as a `Guid`, |
| 186 | +For each change made to a Media item, its values are stored under a new Version. This version is identified by a `Guid`. |
| 187 | + |
| 188 | +## Methods |
| 189 | + |
| 190 | +### .ChangeContentType(IMediaType mediaType) |
| 191 | +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. |
| 192 | + |
| 193 | +```csharp |
| 194 | +// Given a `ContentTypeService` object get the MediaType that we're changing to, |
| 195 | +// get the Media from the `MediaService` for which we want to change MediaType for, |
| 196 | +// and then save the Media through the MediaService. |
| 197 | +var mediaType = contentTypeService.GetMediaType(1122); |
| 198 | +var media = mediaService.GetById(1234); |
| 199 | +media.ChangeContentType(mediaType); |
| 200 | +mediaService.Save(media); |
| 201 | +``` |
| 202 | + |
| 203 | +### .GetCreatorProfile() |
| 204 | +Gets the `IProfile` object for the Creator of this Media, which contains the Id and Name of the User who created this Media item. |
| 205 | + |
| 206 | +```csharp |
| 207 | +// Given a `MediaService` object get Media by its Id and get the `IProfile` of the Creator |
| 208 | +var media = mediaService.GetById(1234); |
| 209 | +var profile = media.GetCreatorProfile(); |
| 210 | +var id = profile.Id; |
| 211 | +string name = profile.Name; |
| 212 | +``` |
| 213 | + |
| 214 | +### .GetValue(string propertyTypeAlias) |
| 215 | +Gets the value of a Property as an `Object`. |
| 216 | + |
| 217 | +```csharp |
| 218 | +// Given a `MediaService` object get Media by its Id and get a value by alias |
| 219 | +var media = mediaService.GetById(1234); |
| 220 | +object value = media.GetValue("height"); |
| 221 | +int text = int.Parse(value.ToString()); |
| 222 | +``` |
| 223 | + |
| 224 | +### .GetValue< TPassType >(string propertyTypeAlias) |
| 225 | +Gets the value of a Property as the defined type 'TPassType'. |
| 226 | + |
| 227 | +```csharp |
| 228 | +// Given a `MediaService` object get Media by its Id and get a value by alias while specifying the return type |
| 229 | +var media = mediaService.GetById(1234); |
| 230 | +int value = media.GetValue<int>("height"); |
| 231 | +``` |
| 232 | + |
| 233 | +### .HasProperty(string propertyTypeAlias) |
| 234 | + |
| 235 | +Returns a `Bool` indicating whether the Media object has a property with the supplied alias. |
| 236 | + |
| 237 | +```csharp |
| 238 | +// Given a `MediaService` object get Media by its Id and check if certain properties exist |
| 239 | +var media = mediaService.GetById(1234); |
| 240 | +bool tagsExists = media.HasProperty("myTagProperty"); |
| 241 | +bool textExists = media.HasProperty("altText"); |
| 242 | +``` |
| 243 | + |
| 244 | +### .SetValue(string propertyTypeAlias, object value) |
| 245 | + |
| 246 | +Sets the value of a property by its alias. |
| 247 | + |
| 248 | +```csharp |
| 249 | +// Given a `MediaService` object get Media by its Id, set a few values |
| 250 | +// and saves the Media through the `MediaService` |
| 251 | +var media = mediaService.GetById(1234); |
| 252 | +media.SetValue("altText", "Alternative text for this media item"); |
| 253 | +media.SetValue("date", DateTime.Now); |
| 254 | +mediaService.Save(media); |
| 255 | +``` |
| 256 | + |
| 257 | +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. |
| 258 | + |
| 259 | +### .ToXml() |
| 260 | + |
| 261 | +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. |
| 262 | + |
| 263 | +```csharp |
| 264 | +// Given a `MediaService` object get Media by its Id and returns the xml |
| 265 | +var media = mediaService.GetById(1234); |
| 266 | +XElement xml = media.ToXml(serializer); |
| 267 | +return xml; |
| 268 | +``` |
0 commit comments