Sometimes, when we work with Episerver, we may need to store a reference to an Episerver content item in a third-party service or database. On a recent project, I came across a situation where a developer had chosen to just store the ID part of a content reference and he didn't understand the subtle bug that he had created. In today's guide, I'm going to cover why just storing the database Id is 'sub-optimal' and cover some alternative approaches.
// Get the ID from third-party database private void Get(int id) { var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); var startpage = contentLoader.Get<IContent>(new ContentReference(id)); }
var reference = new ContentReference(); var result = JsonConvert.SerializeObject(reference);
// get result from db JsonConvert.DeserializeObject<ContentReference>(result );
var myContent = new PageData(); var guidToString = "websitedomain.com"; var customIdentity = new Uri(guidToString); var identityMappingService = ServiceLocator.Current.GetInstance<IdentityMappingService>(); var mappedIdentity = identityMappingService.MapContent(customIdentity, myContent); var referenceToStoreAsString= mappedIdentity.ContentGuid.ToString();
var referneceToStore = new Guid(referenceToStoreAsString); var content = identityMappingService.Get(referneceToStore); var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); var startpage = contentLoader.Get<IContent>(content.ContentLink);
Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge