Currently if you press the preview button only that node will be in preview mode. All other content is just the published version. We've got a huge website and we do many customizations in it. You might have heard of it ;-). For our preview we need all the content to be in preview mode. We know this might be slow, but we still need this.
Anyone got some suggestions for this? I already have the code to convert an IContent to an IPublishedContent. I could check if the website is in preview mode and than use that, but maybe there are better ways to achieve this :-).
Not only the current node will be in preview, but also all child nodes. So by clicking preview on the highest node in the tree it seems that you can run your entire site in preview.
Preview works in the frontend, but I would also like preview to work in the backend. So if I'm in preview mode it also shows those URLs in the backoffice. For now I checked the Umbraco source and it looks like I can check if the preview cookie is available. Would that be enough?
The UmbracoHelper is only used in the frontend where InPreviewMode is already true. For the backoffice URLs replacing the ContentFinderByNiceUrl might actually work. But it's not the content finder that I need to replace but the UrlProvider.
Well could you create a custom property editor which just displays what the URL is? You it would just get the UrlName of all ancestors, add it's own based on string.ToUrlSegment() (in Umbraco.Core), and join with a '/'. It's not the same as the Url but if it is just for information?
I got it working by having a custom property editor which get's the URLs from the front of the website. If the website is in preview it will return all the URLs. Even of unpublished ones.
Since preview for all content only works if you click preview on the highest node I wrote some code for that which can be triggered from every node.
You can call this from a custom property editor. It will get the highest node and use that for preview.
/// <summary>
/// The url preview api controller.
/// </summary>
public class UrlPreviewApiController : UmbracoAuthorizedApiController
{
/// <summary>
/// Update preview for the entire website.
/// </summary>
/// <returns>
/// The <see cref="HttpResponseMessage"/>.
/// </returns>
[System.Web.Http.HttpGet]
public HttpResponseMessage UpdatePreview()
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
// Get the highest node in the tree.
// If we use this id for preview it will preview the entire website.
var websiteId = this.GetWebsiteId();
// Update the preview so we can see the saved changes.
this.UpdatePreviewContext(websiteId);
return result;
}
/// <summary>
/// Checks if the user is currently in preview mode and if so will update the preview content for this item.
/// If it's not in preview a new one will be created.
/// </summary>
/// <param name="contentId">
/// The content id.
/// </param>
/// <remarks>
/// This code uses some obsolete methods, but parts are copied from the Umbraco source and we keep it the same.
/// </remarks>
private void UpdatePreviewContext(int contentId)
{
// Get the preview id from the cookie.
var previewId = this.Request.GetPreviewCookieValue();
// If we have a GUID from the cookie use that.
Guid id;
var existingPreview = Guid.TryParse(previewId, out id);
if (!existingPreview)
{
// If we don't have GUID from the cookie yet create a new one.
id = Guid.NewGuid();
}
// Create the preview.
var d = new Document(contentId);
var pc = new PreviewContent(this.UmbracoUser, id, false);
pc.PrepareDocument(this.UmbracoUser, d, true);
pc.SavePreviewSet();
if (!existingPreview)
{
// Create a new preview cookie if we don't have one.
pc.ActivatePreviewCookie();
}
}
/// <summary>
/// Get the id of the website node. Don't use the ContentRepository because that only works with published content.
/// </summary>
/// <returns>
/// The id.
/// </returns>
private int GetWebsiteId()
{
var websiteContentType = this.Services.ContentTypeService.GetContentType(Website.ModelTypeAlias);
var website = this.Services.ContentService.GetContentOfContentType(websiteContentType.Id).FirstOrDefault();
if (website != null)
{
return website.Id;
}
return -1;
}
}
Has anyone ever tested if preview works when multiple users try it at the same time? It uses cookies so it should work. When I try to preview in 2 different browers the preview doesn't work anymore in the first browser when I open preview in the second browser.
Both browsers had the preview cookie set. The problem was that we were testing with the same user and that didn't work. So when testing in multiple browsers you should also login with different Umbraco users.
Also pretty sure there's a setting somewhere to turn on full site preview... Or at least there was something along those lines at some point in time ;) i can see if i can find anything tomorrow if you don't beat me to it.
@shannon any chance you found how to turn on full-site preview at site level via settings?
We are in the same situation but need to preview unpublished pages as anonymous users
Preview all content
Hello,
Currently if you press the preview button only that node will be in preview mode. All other content is just the published version. We've got a huge website and we do many customizations in it. You might have heard of it ;-). For our preview we need all the content to be in preview mode. We know this might be slow, but we still need this.
Anyone got some suggestions for this? I already have the code to convert an IContent to an IPublishedContent. I could check if the website is in preview mode and than use that, but maybe there are better ways to achieve this :-).
Jeroen
Not only the current node will be in preview, but also all child nodes. So by clicking preview on the highest node in the tree it seems that you can run your entire site in preview.
Will do some more experiments :-).
Jeroen
Also make sure to upgrade to Umbraco 7.4.2 if you want to preview protected pages: http://issues.umbraco.org/issue/U4-4219#comment=67-27355
Jeroen
Preview works in the frontend, but I would also like preview to work in the backend. So if I'm in preview mode it also shows those URLs in the backoffice. For now I checked the Umbraco source and it looks like I can check if the preview cookie is available. Would that be enough?
Jeroen
Could you replace the
ContentFinderByNiceUrl
with your own version that hits the ContentService rather than the cache? https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/IContentFinderNa. That won't work. Sorry - you've need to replace the UmbracoHelper as well I'd guess. You've got me.
The UmbracoHelper is only used in the frontend where InPreviewMode is already true. For the backoffice URLs replacing the ContentFinderByNiceUrl might actually work. But it's not the content finder that I need to replace but the UrlProvider.
Jeroen
OK, I've not properly understood where exactly in the back office you're referring to but I hope you make some progress with that approach.
I mean the URLs that you see in the backoffice. An unpublished node has no URL, but in preview mode it does, but still isn't displayed.
Jeroen
Well could you create a custom property editor which just displays what the URL is? You it would just get the UrlName of all ancestors, add it's own based on string.ToUrlSegment() (in Umbraco.Core), and join with a '/'. It's not the same as the Url but if it is just for information?
Already got something like that, but it's a complex website and I need to get the URLs from the UrlProviders.
Jeroen
I got it working by having a custom property editor which get's the URLs from the front of the website. If the website is in preview it will return all the URLs. Even of unpublished ones.
Jeroen
This looks cool!
Can you share the code for this?
The UrlPreview code can be found in the 1-1 multilingual example: https://our.umbraco.org/projects/developer-tools/1-1-multilingual-example/
Jeroen
Since preview for all content only works if you click preview on the highest node I wrote some code for that which can be triggered from every node.
You can call this from a custom property editor. It will get the highest node and use that for preview.
Jeroen
Has anyone ever tested if preview works when multiple users try it at the same time? It uses cookies so it should work. When I try to preview in 2 different browers the preview doesn't work anymore in the first browser when I open preview in the second browser.
Jeroen
Is it simply because your 2nd browser that it doesn't work in doesn't have the preview cookie set?
Both browsers had the preview cookie set. The problem was that we were testing with the same user and that didn't work. So when testing in multiple browsers you should also login with different Umbraco users.
Jeroen
Also pretty sure there's a setting somewhere to turn on full site preview... Or at least there was something along those lines at some point in time ;) i can see if i can find anything tomorrow if you don't beat me to it.
Preview for the full site would be cool, but my solution at https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/78849-preview-all-content#comment-252857 also works fine :-).
Jeroen
@shannon any chance you found how to turn on full-site preview at site level via settings? We are in the same situation but need to preview unpublished pages as anonymous users
I blogged about how you can preview all the content. It also has a working example: http://24days.in/umbraco-cms/2016/umbraco-edge-case-stories/#preview
Jeroen
is working on a reply...