Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Display Image programmatically with Image selector View modes: 
User avatar
Member
Member
Rene 7 - 8/5/2012 9:07:12 PM
   
Display Image programmatically with Image selector
Hi Kentico, this is the situation:
I created a document type that has a "Image selection" field called "Image." I a webpart I want to list items of this document type programmatically with a data table like:

DataClassInfo MyItemDCI = DataClassInfoProvider.GetDataClass("custom.MyItem");

DataSet MyItemDSAll = conn.ExecuteQuery(MyItemDCI.ClassName + ".selectdocuments", null, "SiteName = '" + CMS.CMSHelper.CMSContext.CurrentSiteName + "' AND Published = 1", "NodeOrder");

MyItemDataTable = MyItemDSAll.Tables[0];


I would have thought displaying the image with a literal would be easy by doing:

output = "<img alt=\"" + dRow["Title"].ToString() + "\" src=\"" + ResolveUrl("~/CMSPages/GetFile.aspx?guid=" + dRow["Image"].ToString()) + "\">";

But no luck. Any help?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/6/2012 5:53:41 PM
   
RE:Display Image programmatically with Image selector
You can use this:

TransformationHelper.HelperObject.GetFileUrl(dRow["Image"],"WhateverFileNameYouWant");

but I would do this if I were you:

//create an instance of a TreeProvider, optionally set it to work as though the administrator were accessing it
TreeProvider tree = new TreeProvider(UserInfoProvider.GetUserInfo("administrator"));
//Query the tree
var myDocs = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", TreeProvider.ALL_CULTURES, true, "custom.MyItem", null, "NodeOrder", -1, true);
//make sure the result is not null or empty
if (!DataHelper.DataSourceIsEmpty(myDocs))
{
//access the results as TreeNode types
foreach (var n in myDocs.Items)
{
// create a new macro resolver for the item
MacroResolver res = new MacroResolver();
// add the item as sourcedata
res.SourceData = new object[] { n };
//use K# macros to output your markup
output += res.ResolveMacros(@"<img alt=""{%HTMLEncode(Title)%}"" src=""{%GetFileUrlByGuid(Image,NodeAlias)%}"">");
}
}


User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/6/2012 5:55:49 PM
   
RE:Display Image programmatically with Image selector
I do things like this quite often so I've actually created some extension and helper classes to make the whole process a bit smoother, but that above is the bare bones code to get the job done.

User avatar
Member
Member
Rene 7 - 8/6/2012 6:47:12 PM
   
RE:Display Image programmatically with Image selector
Great, thank you for your help!
Unfortunately I couldn't get both running. I could just see the "alt" attribute and not the image. What I found in another forum post and what did work was following:


Guid iguid = new Guid(dRow["Image"].ToString());

CMS.TreeEngine.TreeNode node = null;

UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider(ui);

node = tree.SelectSingleNode(iguid, "en-AU", CurrentSiteName);

string myurl = node.NodeAliasPath + ".aspx";

Should be fine now.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/7/2012 12:49:10 PM
   
RE:Display Image programmatically with Image selector
I apologize. I misread how you were storing the image. My method would work if you were using a "File upload" form field in your document type. Which I find to be the preferable way to associate an image with a document.

Try using GetDocumentUrl(Image) instead of GetFileUrl(Image)

The way you're doing it isn't the safest way to do it as far as compatibility goes.

Here I have updated my code snippet for you.


//create an instance of a TreeProvider, optionally set it to work as though the administrator were accessing it
TreeProvider tree = new TreeProvider(UserInfoProvider.GetUserInfo("administrator"));
//Query the tree
var myDocs = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", TreeProvider.ALL_CULTURES, true, "custom.MyItem", null, "NodeOrder", -1, true);
//make sure the result is not null or empty
if (!DataHelper.DataSourceIsEmpty(myDocs))
{
//access the results as TreeNode types
foreach (var n in myDocs.Items)
{
// create a new macro resolver for the item
MacroResolver res = new MacroResolver();
// add the item as sourcedata
res.SourceData = new object[] { n };
//use K# macros to output your markup
output += res.ResolveMacros(@"<img alt=""{%HTMLEncode(Title)%}"" src=""{%GetDocumentUrl(Image)%}"">");
}
}