usingUmbraco.Cms.Core;usingUmbraco.Cms.Core.Models.PublishedContent;usingUmbraco.Cms.Core.PropertyEditors;usingUmbraco.Cms.Core.PublishedCache;namespaceUmbracoDocs.Samples;publicclassContentPickerPropertyConverter:IPropertyValueConverter{privatereadonlyIPublishedSnapshotAccessor _publishedSnapshotAccessor; // Injecting the PublishedSnapshotAccessor for fetching contentpublicContentPickerPropertyConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor)=> _publishedSnapshotAccessor = publishedSnapshotAccessor;publicboolIsConverter(IPublishedPropertyType propertyType)=>propertyType.EditorAlias.Equals("Umbraco.ContentPicker");publicbool?IsValue(object? value,PropertyValueLevel level) {return level switch { PropertyValueLevel.Source => value is string stringValue && string.IsNullOrWhiteSpace(stringValue) is false,
_ =>thrownewNotSupportedException($"Invalid level: {level}.") }; }publicTypeGetPropertyValueType(IPublishedPropertyType propertyType)=>typeof(IPublishedContent);publicPropertyCacheLevelGetPropertyCacheLevel(IPublishedPropertyType propertyType)=>PropertyCacheLevel.Elements; public object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview)
// parse the source string to a GuidUdi intermediate value => source is string stringValue && UdiParser.TryParse(stringValue,outGuidUdi? guidUdi) ? guidUdi: null; public object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview)
// inter is expected to be a GuidUdi at this point (see ConvertSourceToIntermediate)=> inter isGuidUdi guidUdi?_publishedSnapshotAccessor.GetRequiredPublishedSnapshot().Content?.GetById(guidUdi.Guid):null;}
The following example uses Umbraco.Cms.Core.PublishedCache and IPublishedSnapshotAccessor which are obsolete in Umbraco 15 and will be removed in a future version. For more information, see the Version specific upgrades article.