Does anyone know if its possible to get the URL (NiceUrl) of an unpublished page with the current API (Umbraco 4.5.2)?
I know that NiceUrl uses the XML cache (umbraco.config) to construct the URL - and that only applies to published pages!
However for the Bit.ly Url Shortener package that I'm working on, I'd like to auto-generated the short URLs on page publish. The problem is that when you first publish a page, it has no accessible URL to use - or at least anything I can access during the data-type's save life-cycle.
I've tried to also hook into the "umbraco.content.AfterUpdateDocumentCache" event ... but this doesn't help any other event trying to use the shortened url (for example; Tweet on Publish)
Could you get the nice url for the parent and then apply the same logic that umbraco uses on the current node name to replace spaces, non alphanumerics etc?
Thanks Darren, it's a good approach - as you can only publish a child page if its parent is already published! Good thinking!
I've come up with the following method to "guess" the NiceUrl - *cough*cough* an educated guess...
public static string GuessNiceUrl(int nodeId)
{
const string HTTP = "http://";
string domain = string.Empty;
// get the document from the node id.
umbraco.cms.businesslogic.web.Document doc = new umbraco.cms.businesslogic.web.Document(nodeId);
// check if the node has a domain associated.
if (umbraco.UmbracoSettings.UseDomainPrefixes)
{
// get the path
List<string> path = new List<string>(doc.Path.Split(','));
// remove the first entry (-1)
path.RemoveAt(0);
// reverse the path order - we want to work backwards
path.Reverse();
// loop through each path entry
foreach (string part in path)
{
int partId;
if (int.TryParse(part, out partId))
{
umbraco.cms.businesslogic.web.Domain[] domains = umbraco.cms.businesslogic.web.Domain.GetDomainsById(partId);
if (domains != null && domains.Length > 0)
{
domain = domains[0].Name;
break;
}
}
}
}
// try getting the NiceUrl
string niceUrl = umbraco.library.NiceUrl(nodeId);
// if it equals a hash, then the node is unpublished!
if (niceUrl == "#")
{
// get the published node for the parent node.
var node = new umbraco.presentation.nodeFactory.Node(doc.ParentId);
if (node.NiceUrl != "#")
{
const string DOTASPX = ".aspx";
string parentUrl = node.NiceUrl;
string urlName = string.Concat("/", umbraco.cms.helpers.url.FormatUrl(doc.Text.ToLower()));
string fileExtension = umbraco.GlobalSettings.UseDirectoryUrls ? string.Empty : DOTASPX;
// if the URL contains the '.aspx' extension...
if (parentUrl.Contains(DOTASPX))
{
// ... remove it!
parentUrl = parentUrl.Replace(DOTASPX, string.Empty);
}
// construct the NiceUrl for the unpublished node.
niceUrl = string.Concat(parentUrl, urlName, fileExtension);
}
}
// if the NiceUrl doesn't start with 'http://' (and isn't '#') then prepend it.
if (!niceUrl.StartsWith(HTTP) && niceUrl != "#")
{
niceUrl = string.Concat(HTTP, domain, niceUrl);
}
// return the NiceUrl
return niceUrl;
}
It takes into account any hostnames associated with the node (and/or parent nodes) ... and sorts out prefixing the protocol and domain.
The "umbraco.cms.helpers.url.FormatUrl" method takes care of stripping out the "bad chars" ... all good!
Could probably refactor it to make it more streamline? ;-)
NiceUrl for unpublished page
Does anyone know if its possible to get the URL (NiceUrl) of an unpublished page with the current API (Umbraco 4.5.2)?
I know that NiceUrl uses the XML cache (umbraco.config) to construct the URL - and that only applies to published pages!
However for the Bit.ly Url Shortener package that I'm working on, I'd like to auto-generated the short URLs on page publish. The problem is that when you first publish a page, it has no accessible URL to use - or at least anything I can access during the data-type's save life-cycle.
I've tried to also hook into the "umbraco.content.AfterUpdateDocumentCache" event ... but this doesn't help any other event trying to use the shortened url (for example; Tweet on Publish)
Any ideas or suggestions are much appreciated.
Thanks, Lee.
Could you get the nice url for the parent and then apply the same logic that umbraco uses on the current node name to replace spaces, non alphanumerics etc?
Thanks Darren, it's a good approach - as you can only publish a child page if its parent is already published! Good thinking!
I've come up with the following method to "guess" the NiceUrl - *cough*cough* an educated guess...
It takes into account any hostnames associated with the node (and/or parent nodes) ... and sorts out prefixing the protocol and domain.
The "umbraco.cms.helpers.url.FormatUrl" method takes care of stripping out the "bad chars" ... all good!
Could probably refactor it to make it more streamline? ;-)
Cheers, Lee.
is working on a reply...