Hi all, while playing around with SPGridView, the power creating a new class by inheriting the SPBoundField to render any Html element gave me the thought of displaying Adobe flash content on Sharepoint SPGridView. The .swf files are stored in an Sharepoint document library, the SPGridView added to an webpart or a custom aspx renders the information from that document library. The document library is used as the flash content store. Even though I am not that familiar with Flash rendering and tags, I managed to get that the <object> tag is the used to show flash content. The object tag has some specified properties and parameters which in turn renders the flash object in the browser. Basically the SPBoundField can be inherited and by overriding ChildControlDataBinding and the custom html element can be rendered. One of the parameters of ChildControlDataBinding provides the dataItem which has the current SPListItem of the row. So the SPListItem properties can be accessed and from that the swf file’s serverRelativeUrl is fetched.
The SPFlashField class is given below,
public class SPFlashField : SPBoundField
{
protected override void ChildControlDataBinding(System.Web.UI.Control childControl, object dataItem, System.ComponentModel.MemberDescriptor dataFieldPropertyDescriptor)
{
PlaceHolder placeHolder = (PlaceHolder)childControl;
SPDataSourceViewResultItem spDataSourceViewResultItem = dataItem as SPDataSourceViewResultItem;
Microsoft.SharePoint.SPListItem spListItem = spDataSourceViewResultItem.ResultItem as Microsoft.SharePoint.SPListItem;
string fileUrl = spListItem.File.ServerRelativeUrl;
LiteralControl literalControl = new LiteralControl();
literalControl.Text = " ";
//Only if the file is a flash file
if (fileUrl.ToLower().Contains(".swf"))
{
literalControl.Text = this.GetFlashObjectHtml(fileUrl, spListItem.Name, "250", "538");
}
placeHolder.Controls.Add(literalControl);
}
protected override System.Web.UI.Control GetChildControlInstance()
{
return new PlaceHolder();
}
private string GetFlashObjectHtml(string swfFileUrl, string swfName, string height, string width)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<object height='" + height + "' width='" + width + "' name='" + swfName + "' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' classid='clsid:" + new Guid().ToString() + ">");
stringBuilder.Append("<param value='" + swfFileUrl + "' name='movie'>");
stringBuilder.Append("<param value='high' name='quality'>");
stringBuilder.Append("<param value='transparent' name='wmode'>");
stringBuilder.Append("<param value='always' name='allowscriptaccess'>");
stringBuilder.Append("<embed height='" + height + "' width='" + width + "' allowscriptaccess='always' type='application/x-shockwave-flash' flashvars='strm=stream3' bgcolor='#ffffff' quality='high' src='" + swfFileUrl + "' name='" + swfName + "' wmode='transparent' id='" + swfName + "'>");
stringBuilder.Append("</object>");
return stringBuilder.ToString();
}
}
Note: Replace > with > and < with <, my live editor didn’t allowed me to copy paste that code snippet ;-)
The final output is like shown below,
The function GetFlashObjectHtml can be also used to render flash in a webpart, or even in a custom SPField.
No comments:
Post a Comment