Friday, February 18, 2011

Setting time out for Silverlight web part

There is the way to set time out for Silverlight web part. This time out happens when web part can't load xap file.
The value is hard-coded within web part and equal 5 sec which is not so much.
First of all where this value is hard-coded? If you viewed source of the page with Silverlight web part you could found javascript line like that

window.setTimeout("_spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999();", 5000);

This is our bad guy. Value 5000 is hard-coded and function name contains web part Id (ctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999 in my case).

So, the easiest way to handle it is to eliminate time out at all by creating same named dummy JavaScript function:

1. Go to page source and search for _spCheckIfSilverlightPluginIsLoaded_WebPart phrase. You suppose to find function which starts with this phrase. Copy function name to clipboard.

2. Add content editor web part after Silverlight web part and put empty JavaScript function with saved name there like that

<script type="text/javascript">
function _spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999(){}
</script>


where _spCheckIfSilverlightPluginIsLoaded_WebPartctl00_m_g_0f7cd87d_f3fd_49d9_91ec_ad4b46cfe999 is a function name I've found on my page.

> Update! - one more (even better!) workaround:

You can override _spSetSLPluginNotLoadedErrorMessage function which is called by web part if XAP file wasn't loaded in 5 sec.

1. Just drop this tag in Content Editor web part on the page:


<script type="text/javascript"> function _spSetSLPluginNotLoadedErrorMessage(wpId){ 

</script>

> end of update

This is working just fine if you have couple pages with Silverlight web part and you are Ok do not have any time out.

Better but little bit more complicate way is creating javascript to set a new time out value. Main idea is put some code in our dummy function with new time out value. I created a web part for that. It is on codeplex.
It scans the page for Silverlight web parts and renders javascript for each instance. It has just one property - time out :)

Please note this web part should be on your page after Silverlight web part.

How I tested it:
I put two Silverlight web parts to my page with wrong reference to xap file. I got time out in 5 secs on both. Then I put my web part in between. I got time out in 15 secs (my default value) for the first Silverlight web part and still 5 secs for the second.

You can check out wsp package and source code.

Have fun with Silverlight!

2 comments:

  1. Hello, codeplex download links are not working, anyone could download it?

    ReplyDelete
  2. Nice code, thanks.

    I just increase my silverlight app upto 50 MB for testing, it run OK (after 15 seconds loading).

    ReplyDelete