How to display an image\picture column via BDC Web Part?
- Hello,
I have an SQL Server with a table that has an image column (that contains a binary array) and I want to display it with a BDC selection.
Am I asking too much, I know that images require a server source location, but I was hoping it could be displayed as a resource (like js files that appear as resources links in the dll files).
Thank you.
Antworten
- I don't know if this is the best way to do it, but this will produce an image from a file on the file system. Easily adapted to meet your requirements for pulling in from database.
Create this file in a subdirectory of TEMPLATE\LAYOUTS
Maybe DBIMAGES or something, in this example I called it Test.
Inside that create a .ashx file.
Name it whatever you like, I'll call it Test.ashx. Found the root of this concept at http://msdn.microsoft.com/en-us/library/bb457204.aspx<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ WebHandler Language="C#" Class="HelloHttpHandler" %> using System; using System.Web; using Microsoft.SharePoint; public class HelloHttpHandler : IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { SPWeb myWeb = SPContext.Current.Web; context.Response.ContentType = "jpg"; System.IO.FileStream fs = new System.IO.FileStream("c:/temp/bookOfPossibilities.jpg", System.IO.FileMode.Open); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); context.Response.BinaryWrite(br.ReadBytes(Convert.ToInt32(br.BaseStream.Length))); fs.Close(); br.Close(); } }
For your bdc xsl find the rowview section and add this someplace:
<img style="width:75px; height: 75px;"> <xsl:attribute name="src"> http://{URLTOSITE}/_layouts/test/test.ashx?Position=<xsl:value-of select="position()"/> </xsl:attribute> <xsl:attribute name="alt"> <xsl:value-of select="position()"/> </xsl:attribute> </img>
Oh, I just noticed, at the end of the URL I used, I put in position(), that's not needed, I was wanting to see what happens if I wanted to put some sort of parameter in the string to see if and how it would be passed. You can remove it.
Another way to do it is to going through this:
http://www.developer.com/net/article.php/10916_3812301_2
I did find another web article, bit I can't seem to find it.
Hope this helps.- Als Antwort markiertMike Walsh MVPMVP, ModeratorFreitag, 3. April 2009 03:50
Alle Antworten
- I was able to accomplish this by editing the XSL in the BDC web part. Open up the BDC web part properties and then click the button for XSL. I was storing the full path to the image and basically changed the default XSL to output in an img tag. Here is the XSL before editing it:
<td class="ms-vb"> <xsl:attribute name="style"> <xsl:choose> <xsl:when test="$dvt_1_form_selectkey = @*[name()=$ColumnKey]">color:blue</xsl:when> <xsl:otherwise /> </xsl:choose> </xsl:attribute> <xsl:variable name="fieldValue"> <xsl:call-template name="LFtoBR"> <xsl:with-param name="input"> <xsl:value-of select="@Picture" /> </xsl:with-param> </xsl:call-template> </xsl:variable> <xsl:copy-of select="$fieldValue" /> </td>
And here is the XSL after I edited it (to show you how I accomplished this):
<td class="ms-vb"> <xsl:attribute name="style"> <xsl:choose> <xsl:when test="$dvt_1_form_selectkey = @*[name()=$ColumnKey]">color:blue</xsl:when> <xsl:otherwise /> </xsl:choose> </xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"><xsl:value-of select="@Picture" /></xsl:attribute> </xsl:element> </td>
I am not sure if you will be able to have the byte array output on the fly so I would suggest refering to the image like this or use some other ASPX page to pull the image from the database and output it in the response.
Thanks,
- Als Antwort vorgeschlagenRandy.WilliamsMVPDonnerstag, 11. September 2008 19:31
- Nicht als Antwort vorgeschlagenMike Walsh MVPMVP, ModeratorFreitag, 3. April 2009 03:49
- BearbeitetMike Walsh MVPMVP, ModeratorFreitag, 3. April 2009 03:49sig including admonition to propose as answer removed
- I don't know if this is the best way to do it, but this will produce an image from a file on the file system. Easily adapted to meet your requirements for pulling in from database.
Create this file in a subdirectory of TEMPLATE\LAYOUTS
Maybe DBIMAGES or something, in this example I called it Test.
Inside that create a .ashx file.
Name it whatever you like, I'll call it Test.ashx. Found the root of this concept at http://msdn.microsoft.com/en-us/library/bb457204.aspx<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ WebHandler Language="C#" Class="HelloHttpHandler" %> using System; using System.Web; using Microsoft.SharePoint; public class HelloHttpHandler : IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { SPWeb myWeb = SPContext.Current.Web; context.Response.ContentType = "jpg"; System.IO.FileStream fs = new System.IO.FileStream("c:/temp/bookOfPossibilities.jpg", System.IO.FileMode.Open); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); context.Response.BinaryWrite(br.ReadBytes(Convert.ToInt32(br.BaseStream.Length))); fs.Close(); br.Close(); } }
For your bdc xsl find the rowview section and add this someplace:
<img style="width:75px; height: 75px;"> <xsl:attribute name="src"> http://{URLTOSITE}/_layouts/test/test.ashx?Position=<xsl:value-of select="position()"/> </xsl:attribute> <xsl:attribute name="alt"> <xsl:value-of select="position()"/> </xsl:attribute> </img>
Oh, I just noticed, at the end of the URL I used, I put in position(), that's not needed, I was wanting to see what happens if I wanted to put some sort of parameter in the string to see if and how it would be passed. You can remove it.
Another way to do it is to going through this:
http://www.developer.com/net/article.php/10916_3812301_2
I did find another web article, bit I can't seem to find it.
Hope this helps.- Als Antwort markiertMike Walsh MVPMVP, ModeratorFreitag, 3. April 2009 03:50
- Shemesh,
I ran in to a problem that I was unaware of, and for the life of me don't understand why it does it.
Firefox and Google's Chrome have a problem with doing it this way. For some reason they don't produce an image, and if alt is added to the image the alt tag is displayed, not like a broken image but just like normal text as part of the page. I'll be posting the problem on this board and maybe somebody will have an idea as to why.
Could you verify this behavior and let me know if it does the same thing for you?- BearbeitetAnonymousToday Sonntag, 5. April 2009 16:04added a question for original poster to follow up as to why or why not this may be a problem with firefix and chrome.

