After I create a document, save, publish, refresh the content and cache, I can't seem to get the correct value from the NiceUrl API function until after the page reloads:
Imports UmbracoDocument = umbraco.cms.businesslogic.web.Document Dim doc As UmbracoDocument = UmbracoDocument.MakeNew(GetTitle(), GetDocType(), GetUser(), GetParentID()) doc.Save() doc.Publish(GetUser()) umbraco.library.UpdateDocumentCache(doc.Id) umbraco.library.RefreshContent() umbraco.library.UpdateDocumentCache(doc.Id) ' Again, for good measure. ShowUrl(umbraco.library.NiceUrl(doc.Id)) ' "../usercontrols/#"
I put in placeholder functions and took out the unnecessary bits so I don't waste your time by forcing you to read extra code. This code works fine, except the URL shown is "../usercontrols/#" (it should be something like "/parent-folder/year/month/day/title.aspx"). Note that "usercontrols" is the server-side folder of the ASCX control calling the above code (not sure why NiceUrl would return a server-side path).
I'm thinking maybe the XML isn't updated right away or maybe one of the functions I'm calling above is asynchronous, but I'm not really sure. Any ideas of why NiceUrl doesn't return the correct URL right away?
Where are you calling this code from? An event handler on another document?
Check your umbracoLog table to see if you are getting any errors. Is it possible the parent document isn't published yet?
I ran into a similar issue yesterday (the Link To Document property showed "#" when viewing my new document). The issue was I was creating some child documents on an AfterPublish event handler on the parent document, but for whatever reason the parent wasn't fully published during the AfterPublish event. There were errors in the umbracoLog table stating nodes weren't published or something along those lines. The fix was to use the umbraco.content.AfterUpdateDocumentCache event instead of Document_AfterPublish
Thanks for the direction, Tom. The problem may very well be related to the AfterPublish event. Problem is, I'm not really in a position to fix it. We are using this Blog 4 Umbraco package that I'm pretty sure has a buggy implementation in AfterPublish (when a blog post is saved, some Blog 4 Umbraco code is triggered to nest the node in a year/month/day path). I seem to have to publish items about 3 times for them to actually save (I am using a loop and a try/catch to do this). I guess what I'll do as a workaround is create an intermediate page to do the redirect for me (I'm using NiceUrl to perform a redirect). If you are curious, here are the contents of umbracoLog for the timespan where I ran my code:
-1 2011-04-14 13:52:17.930 Debug Xml saved in 00:00:00.0361001 -1 2011-04-14 13:52:17.913 Debug Xml saved in 00:00:00.0302611 -1 2011-04-14 13:52:17.867 Debug Done republishing Xml Index -1 2011-04-14 13:52:17.820 Debug Xml Pages loaded 3094 2011-04-14 13:52:17.757 Publish -1 2011-04-14 13:52:17.617 Debug Republishing starting -1 2011-04-14 13:52:17.587 System Loading content from database... 3094 2011-04-14 13:52:17.570 Debug Start Before Publish Event for Blog Post 3094 -1 2011-04-14 13:52:17.070 Debug Xml saved in 00:00:00.0352854 -1 2011-04-14 13:52:17.040 Debug Xml saved in 00:00:00.0319576 3094 2011-04-14 13:52:17.023 Publish -1 2011-04-14 13:52:17.007 Debug Done republishing Xml Index -1 2011-04-14 13:52:16.960 Debug Xml Pages loaded -1 2011-04-14 13:52:16.773 Debug Republishing starting -1 2011-04-14 13:52:16.727 System Loading content from database... 3094 2011-04-14 13:52:16.697 Debug Start Before Publish Event for Blog Post 3094 -1 2011-04-14 13:52:16.383 Debug Xml saved in 00:00:00.0374229 -1 2011-04-14 13:52:16.383 Debug Xml saved in 00:00:00.0321789 -1 2011-04-14 13:52:16.350 Debug Done republishing Xml Index 3094 2011-04-14 13:52:16.350 Publish -1 2011-04-14 13:52:16.273 Debug Xml Pages loaded -1 2011-04-14 13:52:16.053 Debug Republishing starting -1 2011-04-14 13:52:16.007 System Loading content from database... 3094 2011-04-14 13:52:15.977 Debug Start Before Publish Event for Blog Post 3094 -1 2011-04-14 13:52:15.633 Debug Xml saved in 00:00:00.0396924 3094 2011-04-14 13:52:15.617 Publish -1 2011-04-14 13:52:15.617 Debug Xml saved in 00:00:00.0441044 -1 2011-04-14 13:52:15.553 Debug Done republishing Xml Index -1 2011-04-14 13:52:15.493 Debug Xml Pages loaded -1 2011-04-14 13:52:15.290 Debug Republishing starting -1 2011-04-14 13:52:15.243 System Loading content from database... 3094 2011-04-14 13:52:15.197 Debug Start Before Publish Event for Blog Post 3094 3094 2011-04-14 13:52:14.913 New
Just to provide an update, I went with the intermediate redirect page as a workaround and that seems to be working fine. So a user will go to site.com/create.aspx, they'll enter some info, hit "Submit", some Umbraco content nodes will be created, the user will automatically be redirected to site.com/redirect.aspx?NodeId=1234, and that page will use NiceUrl to redirect them to site.com/some-custom-url-for-node-1234.aspx.
Can't Get NiceUrl After Creating Document/Node
Think I'm on Umbraco 4.5.2.
After I create a document, save, publish, refresh the content and cache, I can't seem to get the correct value from the NiceUrl API function until after the page reloads:
I put in placeholder functions and took out the unnecessary bits so I don't waste your time by forcing you to read extra code. This code works fine, except the URL shown is "../usercontrols/#" (it should be something like "/parent-folder/year/month/day/title.aspx"). Note that "usercontrols" is the server-side folder of the ASCX control calling the above code (not sure why NiceUrl would return a server-side path).
I'm thinking maybe the XML isn't updated right away or maybe one of the functions I'm calling above is asynchronous, but I'm not really sure. Any ideas of why NiceUrl doesn't return the correct URL right away?
Hi,
Where are you calling this code from? An event handler on another document?
Check your umbracoLog table to see if you are getting any errors. Is it possible the parent document isn't published yet?
I ran into a similar issue yesterday (the Link To Document property showed "#" when viewing my new document). The issue was I was creating some child documents on an AfterPublish event handler on the parent document, but for whatever reason the parent wasn't fully published during the AfterPublish event. There were errors in the umbracoLog table stating nodes weren't published or something along those lines. The fix was to use the umbraco.content.AfterUpdateDocumentCache event instead of Document_AfterPublish
Hope this helps,
Tom
Thanks for the direction, Tom. The problem may very well be related to the AfterPublish event. Problem is, I'm not really in a position to fix it. We are using this Blog 4 Umbraco package that I'm pretty sure has a buggy implementation in AfterPublish (when a blog post is saved, some Blog 4 Umbraco code is triggered to nest the node in a year/month/day path). I seem to have to publish items about 3 times for them to actually save (I am using a loop and a try/catch to do this). I guess what I'll do as a workaround is create an intermediate page to do the redirect for me (I'm using NiceUrl to perform a redirect). If you are curious, here are the contents of umbracoLog for the timespan where I ran my code:
Just to provide an update, I went with the intermediate redirect page as a workaround and that seems to be working fine. So a user will go to site.com/create.aspx, they'll enter some info, hit "Submit", some Umbraco content nodes will be created, the user will automatically be redirected to site.com/redirect.aspx?NodeId=1234, and that page will use NiceUrl to redirect them to site.com/some-custom-url-for-node-1234.aspx.
is working on a reply...