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 isstring stringValue &&string.IsNullOrWhiteSpace(stringValue) isfalse, _ =>thrownewNotSupportedException($"Invalid level: {level}.") }; }publicTypeGetPropertyValueType(IPublishedPropertyType propertyType)=>typeof(IPublishedContent);publicPropertyCacheLevelGetPropertyCacheLevel(IPublishedPropertyType propertyType)=>PropertyCacheLevel.Elements;publicobject?ConvertSourceToIntermediate(IPublishedElement owner,IPublishedPropertyType propertyType,object? source,bool preview) // parse the source string to a GuidUdi intermediate value=> source isstring stringValue &&UdiParser.TryParse(stringValue,outGuidUdi? guidUdi)? guidUdi:null;publicobject?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.