Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How to get SEO-friendly image url? View modes: 
User avatar
Member
Member
lancetek - 7/6/2011 9:28:45 AM
   
How to get SEO-friendly image url?
I have a document field that uses the 'Image Selector' field type. This stores a NodeGuid identifier into the appropriate column for my document.

What functions are available to retrieve a SEO-friendly Url for the image using this Guid? I'm mostly concerned about use in a transformation.

I've seen various other posts about using functions like:

GetFileUrl("ArticleImage")
- which gives a Url like: /getattachment/829b3830-4802-45db-8a97-c591f3d7ba41/invest-in-spanish-real-estate-minister-urges-brits.aspx

or even using: ResolveUrl("~/CMSPages/GetFile.aspx?guid=" + Eval("ArticleImage").ToString())
- which gives an equally horrid: /CMSPages/GetFile.aspx?guid=829b3830-4802-45db-8a97-c591f3d7ba41



Since these image files are in the hierarchy and are documents on their own, they have the nice Kentico Url properties like 'Document Url Path' and various 'Document Aliases'. So I'd like to easily get the 'Document Url Path' for the image, along with the file extension - something like: /Images/big-blue-image.jpg for a url.

I've gone and made my own custom Function, but I think that this has already got to be in the framework, but I just don't know where to find it.


public static string GetImagePathFromGuid(object guid)
{
if (null != guid && !String.IsNullOrEmpty(guid.ToString()))
{
//lets use this to get a 'cleaner' SEO-optimal Url
Guid g = Guid.Parse(guid.ToString());

CMSContext.Init();
UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
var tree = new CMS.TreeEngine.TreeProvider(ui);

var node = tree.SelectSingleNode(g, CMS.CMSHelper.CMSContext.CurrentDocument.DocumentCulture, CMS.CMSHelper.CMSContext.CurrentSiteName);
return node.DocumentUrlPath;
}
//default
return null;
}



Any ideas?

Cheers,
Lance

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 7/7/2011 2:43:46 AM
   
RE:How to get SEO-friendly image url?
Hi,

Regrettably there is nothing better. But, it is already being planned.

Your approach is also a good way how to achieve it. There is also a KB article, for older version of Kentico CMS but the idea is still the same, please see http://devnet.kentico.com/Knowledge-Base/API-and-Internals/How-to-retrieve-an-image-from-the-KenticoCMS-datab.aspx. You can use the code in your custom transformation function.

Best regards,
Juraj Ondrus