Had a quick look and I believe its unable to retrieve the url because it doesnt exist until the item is saved post publish somewhere?
The node ID is created though.
Unless you have a url write on it, maybe use this to generate what the url will be with :
var urlName = umbracoHelper.NiceUrlWithDomain(publishedItem.ParentId) + publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
Probably very hacky until a real solution is found. This may cause an error with new items created at root so retrieving the root url and appending in that case might be best.
Ill have a further look into it just now and see if I can see a more elegant solution.
Created a new class in the app code, this seems to work on the small tests I've done. Hope it might help you out until you find a more elegant (correct) solution.
public class ContentNewService : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentServicePublished;
}
private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
foreach (var publishedItem in e.PublishedEntities)
{
string contentUrl = "";
string root = HttpContext.Current.Request.Url.ToString();
//check if published before
if (umbracoHelper.UrlAbsolute(publishedItem.Id) != "#")
{
contentUrl = umbracoHelper.UrlAbsolute(publishedItem.Id);
}
else
{
//Check if published at root
if (publishedItem.ParentId == -1)
{
contentUrl = root + publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
}
else
{
contentUrl = umbracoHelper.UrlAbsolute(publishedItem.ParentId) +
publishedItem.Name.Replace(' ', '-').ToLowerInvariant();
}
}
//Success message
e.Messages.Add(new EventMessage("Success", "Url:'" + contentUrl +"'", EventMessageType.Success));
}
}
}
Umbraco Published Event
Hi
I seem to have a few difficulties with using "Published" event;
It seems, that if the entity is new, I'm unable to get the URL of the node. Shouldn't the data be in the cache once Published gets triggered?
I'm doing it like this:
but contentUrl is empty, if the entity is new.
Any help is appreciated.
Had a quick look and I believe its unable to retrieve the url because it doesnt exist until the item is saved post publish somewhere?
The node ID is created though.
Unless you have a url write on it, maybe use this to generate what the url will be with :
Probably very hacky until a real solution is found. This may cause an error with new items created at root so retrieving the root url and appending in that case might be best.
Ill have a further look into it just now and see if I can see a more elegant solution.
Created a new class in the app code, this seems to work on the small tests I've done. Hope it might help you out until you find a more elegant (correct) solution.
Thanks man! It might be a hacky solution, but it seems to be how I need to do it right now.
No worries man, hopefully someone who has a little more experience could chime in with a better solution.
Atb,
is working on a reply...