More in this section
Blogs RSS feed

Microsoft Media Player control in Sitefinity

by Georgi Chokov
Creating a simple wrapper for the Media Player is easy. Such control can be used to play *.wmv files directly from Sitefinity Custom/Video library or from outside source - such as physical file or a video streaming web source. The first case is demonstrated in VideoWrapper.


How to:

1. Open MS Visual Studio and create a User Control file (*.ascx). Here, I will call it VideoWrapper

2. You will need to register the Microsoft Silverlight assembly. To do so just copy/paste following snippet:


<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %> 

3. Add the asp:MediaPlayer control

The file should look like this:

<%@ Control Language="C#"AutoEventWireup="true" CodeFile="VideoWrapper.ascx.cs" Inherits="VideoWrapper" %> 
<%@ Register assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagprefix="asp" %> 
<asp:MediaPlayer ID="mediaPlayer" runat="server" /> 

4. Now, for the code. Open the control's code-behind file.

4.1 We are going to use the Sitefinity LibraryManager to retrieve the needed items from the database, so add the following references:

using System; 
using Telerik.Cms.Engine.Data; 
using Telerik.Libraries.Data; 
using Telerik.Libraries; 


4.2 In order to make the new control more flexible you can create few custom properties like this:

public int PlayerHeight{ getset;} 
public int PlayerWidth { getset; }    
public string LibraryProvider { getset; } 
public string LibraryName { getset; } 
public string ItemGuidID { getset; } 

Later, when the control is added to Sitefinity, these properties will be exposed directly in the CMS so you will be able to set them through the UI.

4.3 Now to the Page_Load event. The following code will stretch the Media Player:

this.mediaPlayer.Height = PlayerHeight; 
this.mediaPlayer.Width = PlayerWidth; 

the next lines are dedicated for retrieving the Video file from the database:

var manager = new LibraryManager(LibraryProvider); 
var libraries = manager.GetAllLibraries(); 
at this point the variable "libraries" contains references to all libraries in the previously specified provider.


Now we need to get the item and set it as the MediaSource for the MediaPlayer:


foreach (var library in libraries) 
            var currentLibrary = (Library)library; 
            if (currentLibrary.Name == LibraryName) 
                var lItems = currentLibrary.GetItems(); 
                foreach(var it in lItems) 
                    var item = (CmsContentBase)it; 
                        mediaPlayer.MediaSource = item.UrlWithExtension; 

The code above is simply searching for the specified library and the specified item in this library, then the item Url is set as the mediaPlayer.MediaSource.

Remember that the Url should also contain the Item's Extensions!

5. Save and Upload the new Control to Sitefinity.

That is all it takes.

If you want to use external data sources just expose on string property and set the Media Player's Media Sources to it.




Leave a comment
  1. Steve May 20, 2009
    User needs to have the silverlight player installed?
  2. N/A May 21, 2009
    Yes, you will need Silverlight installed.
  3. MedicalWebGeek May 22, 2009
    My Sitefinity site is running on Windows 2003 and my media server on another server running Windows 2008 - how can I extend this so that I only have to include the URL for my .wmv file?
  4. N/A May 25, 2009
    You can try this:
    instead of searching the libraries just declare one public "string field Url" for example and then set mediaPlayer.MediaSource = Url;
    Now you will just need to add the Url to the .wmv file.
  5. almny May 28, 2009
  6. MedicalWebGeek May 29, 2009
    Is there anyway to tie the Rad MediaPlayer template to the player?  I have figured out how to use the standard MS templates, but not the Telerik ones.
  7. N/A Jun 01, 2009
    Rad MediaPlayer is different control than MS MediaPlayer, if you want to use the Rad MediaPlayer resources you should use it instead of the MS control.
  8. Gray.Blog Jun 02, 2009
    Pingback from:

    Leave a comment