Class PublishedElementExtensions
Provides extension methods for IPublishedElement.
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class PublishedElementExtensions
Methods
View SourceCreatorName(IPublishedElement, IUserService)
Gets the name of the content item creator.
Declaration
public static string? CreatorName(this IPublishedElement content, IUserService userService)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IUserService | userService | The user service. |
Returns
| Type | Description |
|---|---|
| string | The name of the creator, or null if not found. |
CultureDate(IPublishedElement, IVariationContextAccessor, string?)
Gets the culture date of the content item.
Declaration
public static DateTime CultureDate(this IPublishedElement content, IVariationContextAccessor variationContextAccessor, string? culture = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IVariationContextAccessor | variationContextAccessor | The variation context accessor. |
| string | culture | The specific culture to get the name for. If null is used the current culture is used (Default is null). |
Returns
| Type | Description |
|---|---|
| DateTime |
GetCreatorName(IPublishedElement, IUserService)
Gets the name of the user who created the content item.
Declaration
public static string GetCreatorName(this IPublishedElement content, IUserService userService)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IUserService | userService | The user service. |
Returns
| Type | Description |
|---|---|
| string | The name of the creator, or an empty string if not found. |
GetWriterName(IPublishedElement, IUserService)
Gets the name of the user who last updated the content item.
Declaration
public static string GetWriterName(this IPublishedElement content, IUserService userService)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IUserService | userService | The user service. |
Returns
| Type | Description |
|---|---|
| string | The name of the writer, or an empty string if not found. |
HasCulture(IPublishedElement, string?)
Determines whether the content has a culture.
Declaration
public static bool HasCulture(this IPublishedElement content, string? culture)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | |
| string | culture |
Returns
| Type | Description |
|---|---|
| bool |
Remarks
Culture is case-insensitive.
HasProperty(IPublishedElement, string)
Gets a value indicating whether the content has a property identified by its alias.
Declaration
public static bool HasProperty(this IPublishedElement content, string alias)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| string | alias | The property alias. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether the content has the property identified by the alias. |
Remarks
The content may have a property, and that property may not have a value.
HasValue(IPublishedElement, string, string?, string?)
Gets a value indicating whether the content has a value for a property identified by its alias.
Declaration
public static bool HasValue(this IPublishedElement content, string alias, string? culture = null, string? segment = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | |
| string | alias | |
| string | culture | |
| string | segment |
Returns
| Type | Description |
|---|---|
| bool |
Remarks
Returns true if GetProperty(alias) is not null and GetProperty(alias).HasValue is
true.
HasValue(IPublishedElement, IPublishedValueFallback, string, string?, string?, Fallback)
Gets a value indicating whether the content has a value for a property identified by its alias.
Declaration
public static bool HasValue(this IPublishedElement content, IPublishedValueFallback publishedValueFallback, string alias, string? culture = null, string? segment = null, Fallback fallback = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| IPublishedValueFallback | publishedValueFallback | The published value fallback implementation. |
| string | alias | The property alias. |
| string | culture | The variation language. |
| string | segment | The variation segment. |
| Fallback | fallback | Optional fallback strategy. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether the content has a value for the property identified by the alias. |
Remarks
Returns true if HasValue is true, or a fallback strategy can provide a value. ToAncestors is not supported at the element level and will throw a System.NotSupportedException.
IsComposedOf(IPublishedElement, string)
Gets a value indicating whether the content is of a content type composed of the given alias
Declaration
public static bool IsComposedOf(this IPublishedElement content, string alias)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| string | alias | The content type alias. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether the content is of a content type composed of a content type identified by the alias. |
IsDocumentType(IPublishedElement, string)
Determines whether the specified content is a specified content type.
Declaration
public static bool IsDocumentType(this IPublishedElement content, string docTypeAlias)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content to determine content type of. |
| string | docTypeAlias | The alias of the content type to test against. |
Returns
| Type | Description |
|---|---|
| bool | True if the content is of the specified content type; otherwise false. |
IsDocumentType(IPublishedElement, string, bool)
Determines whether the specified content is a specified content type or it's derived types.
Declaration
public static bool IsDocumentType(this IPublishedElement content, string docTypeAlias, bool recursive)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content to determine content type of. |
| string | docTypeAlias | The alias of the content type to test against. |
| bool | recursive | When true, recurses up the content type tree to check inheritance; when false just calls IsDocumentType(this IPublishedElement content, string docTypeAlias). |
Returns
| Type | Description |
|---|---|
| bool | True if the content is of the specified content type or a derived content type; otherwise false. |
IsEqual(IPublishedElement, IPublishedElement)
Determines whether this content item is equal to another content item by comparing their IDs.
Declaration
public static bool IsEqual(this IPublishedElement content, IPublishedElement other)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IPublishedElement | other | The other content item to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsInvariantOrHasCulture(IPublishedElement, string)
Determines whether the content is invariant, or has a culture.
Declaration
public static bool IsInvariantOrHasCulture(this IPublishedElement content, string culture)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | |
| string | culture |
Returns
| Type | Description |
|---|---|
| bool |
Remarks
Culture is case-insensitive.
IsNotEqual(IPublishedElement, IPublishedElement)
Determines whether this content item is not equal to another content item by comparing their IDs.
Declaration
public static bool IsNotEqual(this IPublishedElement content, IPublishedElement other)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IPublishedElement | other | The other content item to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
IsVisible(IPublishedElement, IPublishedValueFallback)
Gets a value indicating whether the content is visible.
Declaration
public static bool IsVisible(this IPublishedElement content, IPublishedValueFallback publishedValueFallback)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| IPublishedValueFallback | publishedValueFallback | The published value fallback implementation. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether the content is visible. |
Remarks
A content is not visible if it has an umbracoNaviHide property with a value of "1". Otherwise, the content is visible.
MediaUrl(IPublishedContent, IPublishedUrlProvider, string?, UrlMode, string)
Gets the url for a media.
Declaration
public static string MediaUrl(this IPublishedContent content, IPublishedUrlProvider publishedUrlProvider, string? culture = null, UrlMode mode = UrlMode.Default, string propertyAlias = "umbracoFile")
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedContent | content | The content item. |
| IPublishedUrlProvider | publishedUrlProvider | The published url provider. |
| string | culture | The culture (use current culture by default). |
| UrlMode | mode | The url mode (use site configuration by default). |
| string | propertyAlias | The alias of the property (use 'umbracoFile' by default). |
Returns
| Type | Description |
|---|---|
| string | The url for the media. |
Remarks
The value of this property is contextual. It depends on the 'current' request uri, if any. In addition, when the content type is multi-lingual, this is the url for the specified culture. Otherwise, it is the invariant url.
OfTypes<T>(IEnumerable<T>, params string[])
Filters published elements by their content type alias.
Declaration
public static IEnumerable<T> OfTypes<T>(this IEnumerable<T> contents, params string[] types) where T : IPublishedElement
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | contents | The elements to filter. |
| string[] | types | The content type aliases to include. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | Elements whose content type alias matches any of the specified types. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of published elements. |
Remarks
The .OfType<T>() filter is nice when there's only one type. This method supports filtering with multiple types.
ToIndexedArray<TContent>(IEnumerable<TContent>)
Converts an enumerable of published elements to an indexed array with positional information.
Declaration
public static IndexedArrayItem<TContent>[] ToIndexedArray<TContent>(this IEnumerable<TContent> source) where TContent : class, IPublishedElement
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TContent> | source | The source enumerable. |
Returns
| Type | Description |
|---|---|
| IndexedArrayItem<TContent>[] | An array of indexed items containing positional information. |
Type Parameters
| Name | Description |
|---|---|
| TContent | The type of published elements. |
Value(IPublishedElement, IPublishedValueFallback, string, string?, string?, Fallback, object?)
Gets the value of a content's property identified by its alias.
Declaration
public static object? Value(this IPublishedElement content, IPublishedValueFallback publishedValueFallback, string alias, string? culture = null, string? segment = null, Fallback fallback = default, object? defaultValue = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| IPublishedValueFallback | publishedValueFallback | The published value fallback implementation. |
| string | alias | The property alias. |
| string | culture | The variation language. |
| string | segment | The variation segment. |
| Fallback | fallback | Optional fallback strategy. |
| object | defaultValue | The default value. |
Returns
| Type | Description |
|---|---|
| object | The value of the content's property identified by the alias, if it exists, otherwise a default value. |
Remarks
The value comes from IPublishedProperty field Value ie it is suitable for use when rendering
content.
If no property with the specified alias exists, or if the property has no value, returns
defaultValue.
If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.
The alias is case-insensitive.
ValueFor<TModel, TValue>(TModel, IPublishedValueFallback, Expression<Func<TModel, TValue>>, string?, string?, Fallback, TValue?)
Gets the value of a specified property from the model.
Declaration
public static TValue? ValueFor<TModel, TValue>(this TModel model, IPublishedValueFallback publishedValueFallback, Expression<Func<TModel, TValue>> property, string? culture = null, string? segment = null, Fallback fallback = default, TValue? defaultValue = default) where TModel : IPublishedElement
Parameters
| Type | Name | Description |
|---|---|---|
| TModel | model | The published element model from which to retrieve the property value. |
| IPublishedValueFallback | publishedValueFallback | The fallback strategy to use when resolving the property value. |
| Expression<Func<TModel, TValue>> | property | An expression that identifies the property to retrieve. |
| string | culture | The culture to use when retrieving the value, or |
| string | segment | The segment to use when retrieving the value, or |
| Fallback | fallback | The fallback behavior to apply if the value is not found. |
| TValue | defaultValue | The value to return if the property value is not found. |
Returns
| Type | Description |
|---|---|
| TValue | The value of the specified property, or |
Type Parameters
| Name | Description |
|---|---|
| TModel | |
| TValue |
Value<T>(IPublishedElement, IPublishedValueFallback, string, string?, string?, Fallback, T?)
Gets the value of a content's property identified by its alias, converted to a specified type.
Declaration
public static T? Value<T>(this IPublishedElement content, IPublishedValueFallback publishedValueFallback, string alias, string? culture = null, string? segment = null, Fallback fallback = default, T? defaultValue = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content. |
| IPublishedValueFallback | publishedValueFallback | The published value fallback implementation. |
| string | alias | The property alias. |
| string | culture | The variation language. |
| string | segment | The variation segment. |
| Fallback | fallback | Optional fallback strategy. |
| T | defaultValue | The default value. |
Returns
| Type | Description |
|---|---|
| T | The value of the content's property identified by the alias, converted to the specified type. |
Type Parameters
| Name | Description |
|---|---|
| T | The target property type. |
Remarks
The value comes from IPublishedProperty field Value ie it is suitable for use when rendering
content.
If no property with the specified alias exists, or if the property has no value, or if it could not be
converted, returns default(T).
If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.
The alias is case-insensitive.
WriterName(IPublishedElement, IUserService)
Gets the name of the content item writer.
Declaration
public static string? WriterName(this IPublishedElement content, IUserService userService)
Parameters
| Type | Name | Description |
|---|---|---|
| IPublishedElement | content | The content item. |
| IUserService | userService | The user service. |
Returns
| Type | Description |
|---|---|
| string | The name of the writer, or null if not found. |