Craig100
04/06/2024, 3:28 PMvar pageRef = _contentService.SaveAndPublish(advertiserPage);
GuidUdi adPageUdi = pageRef.Content.GetUdi();
var adPage = _contentService.GetById(adPageUdi.Guid); // <--- Seems to be ok so far!
var logoImageRef = adPage.GetValue("logoImage") as Udi; // <--- I get Null here although the image exists in the saved page
IMedia logoImage = _mediaService.GetById(logoImageRef.AsGuid());
I've been round and round this and can't work it out.
Actually I could also do with the URL for the page itself as well.
Any pointers would be appreciated.
Thanks.Craig100
04/06/2024, 4:20 PMvar pageRef = _contentService.SaveAndPublish(advertiserPage);
int adPageId = pageRef.Content.Id;
AdvertiserPage? pageFromId = null;
if(_queryAccessor.TryGetValue(out var query))
{
pageFromId = query.Content(adPageId) as AdvertiserPage; // <--- Null :(
}
if (pageFromId is not null) {
string logoImageUrl = pageFromId.LogoImage.Url();
}
Head scratching continues........Jemayn
04/07/2024, 8:58 PMcsharp
var logoImageRef = adPage.GetValue("logoImage") as Udi;
Which as far as I can see is never going to work, as the value of a mediaPicker v3 is a json blob which can't immediately be turned into a udi. Fx with a simple imagepicker allowing only one image and with no local crops I had a value like this:
json
[{"key":"810d0061-b59e-4017-a2dc-bdba544b038c","mediaKey":"276b48e4-f241-428c-8411-133454977781"}]
Where mediaKey is the one you want to use.
The interesting thing is I couldn't get it to get the value of the property even as a string, not really sure why..
Your 2nd example worked for me though, I don't have the modelsbuilder models you have obviously but getting the page as IPublishedContent through the publishedQueryAccessor worked just fine and I could get the image as MediaWithCrops as expected.
csharp
var adPage = _contentService.GetById(content.Id);
if (_publishedContentQueryAccessor.TryGetValue(out var query) && adPage is not null)
{
var page = query.Content(adPage.Id);
var image = page?.Value<MediaWithCrops>("imagePicker"); // gets an image reference
}
var logoImageRef = adPage?.GetValue("imagePicker"); // always null
NOTE: Getting it through the IPublishedContentQueryAccessor will only get the latest Published version, so may not be the right workaround anyways for your needsCraig100
04/08/2024, 9:48 AMvar adPage = _contentService.GetById(pageRef.Content.Id);
if(_queryAccessor.TryGetValue(out var query) && adPage is not null)
{
var pageFromId = query.Content(adPage.Id);
var theLogoImage = pageFromId?.Value<MediaWithCrops>("logoImage");
var logoImageUrl = theLogoImage.Url();
}
Jemayn
04/08/2024, 10:03 AMCraig100
04/08/2024, 10:07 AMvar pageRef = _contentService.SaveAndPublish(advertiserPage);
at the top of my second example. And the ContentService returns a page. So is there any other way of getting the URL's of the images on the page that's just been published if not using the QueryAccessor?Craig100
04/08/2024, 4:07 PMIUmbracoContextFactory context
😦 :-
var pageRef = _contentService.SaveAndPublish(advertiserPage)
var adPage = _contentService.GetById(pageRef.Content.Id);
using (var ctxRef = _umbracoContextFactory.EnsureUmbracoContext())
{
var publishedAdPage = ctxRef.UmbracoContext.Content.GetById(adPage.Id);
if (publishedAdPage != null) <-- Is Null!!
{
var content = _contentService.GetById(adPage.Id);
content = _contentService.GetVersion(content.PublishedVersionId);
if (publishedAdPage.UpdateDate.Equals(content.PublishDate))
{
parameters.Add("advertiserPageUrl", publishedAdPage.Url());
var publishedLogoImage = publishedAdPage.Value<MediaWithCrops>("logoImage");
parameters.Add("advertiserPageUrl", publishedLogoImage.Url());
}
}
}
Craig100
04/10/2024, 2:29 PMJason
04/10/2024, 4:54 PMCraig100
04/11/2024, 10:07 AM