WebpageUrlRetriver Throwing Error
Hello Community,
I have created one content type in which i have one filed of type page. Author can select the pages for that fields from all the available pages. Now I have following logic responsible to get the url of that selected page:
var targetGuid = item.LinkPage.FirstOrDefault().WebPageGuid;
item.LinkURL = _webPageUrlRetriever.Retrieve(targetGuid, _preferredLanguageRetriever.Get()).GetAwaiter().GetResult().RelativePath;
Now i have assigned one page called Article to above mentioned field and it is generating the link as expected. No when i delete that article page and does not update the link. Then i am getting this error.
Now i want to know how to handle this error in the code. because author might not update and link after deleting any page. So in that case as developer i want to handle it in the code.
Environment
Xperience by Kentico version: [30.5.2]
.NET version: [8]
Execution environment: [SaaS]
Answers
- Retrieve the page using a content query or use the new content retriever API.
- Make sure there result is not null or empty.
Also, why are you using GetAwaiter().GetResult()
inside a View Component? GetAwaiter().GetResult()
is only an escape hatch meant to be used when you have to write async code inside a synchronous method. The View Component's InvokeAsync()
is not synchronous, which means you should be using async
and await
.
I would look up the "Maybe/Result Monad" pattern, because you should be VERY careful assuming something exists. If there's even a remote possibility it won't, you should do a check first, otherwise you expose errors. Yes it's more coding, but ultimately it's a more error-proof code.
var retrieverQuery = new ContentItemQueryBuilder().ForContentType(Interactive.CONTENT_TYPE_NAME, query => query
.WithLinkedItems(8)
.Where(where => where.WhereEquals(nameof(Interactive.InteractiveCodeName), interactiveCodeName))
).InLanguage(currentLanguage, useLanguageFallbacks: true);
var resultItems = (await _contentQueryExecutor.GetMappedResult<Interactive>(retrieverQuery, new ContentQueryExecutionOptions() { ForPreview = false, IncludeSecuredItems = true }));
if (!resultItems.FirstOrMaybe().TryGetValue(out var resultItem)) {
return Result.Failure<InteractiveWithParent>("Could not locate");
}
// It's found now, so continue
var parentPage = Maybe<InteractiveParentSummaryItem>.None;
I want to retrieve the page link. but that page can any type. Currently i have 10 different types of page template.
Also i have removed GetAwaiter and Get Result and used async await. Thanks for pointing out
I want to retrieve the page link. but that page can any type.
This won't prevent you from using either the Content Retriever or Content Item Query Builder from constructing a query that does not know content type of the items being queried. You'll need to look at the definition and use of IncludeUrlPath()
in your queries to make sure you've retrieved values for all the fields required to generate URLs correctly (and not throw exceptions).
You can then take the results and retrieve the URL for them, independent on content type.
To answer this question, you have to login first.