Prexens team dev' corner

Ultimate solution to get rid of "The Web site wants to run the following add-on: 'Name ActiveX Control'"

Download setup files (21.1 KB)
Download sources (926 KB)
 
Problematic overview
 
When you are developing internet or extranet website with SharePoint, a common issue you have to resolve is to fix the following message displayed to end-users running IE7 and later
 
The Web site wants to run the following add-on: 'Name ActiveX Control'
 
This message is caused by presence features of SharePoint requiring this ActiveX control to be installed on clients computer. Many solutions can be found on the Web. Even Microsoft gives some workarounds to get rid of this behavior. But to me, none of them are really reliable.
 
Let's study a bit solutions from Microsoft. First and second methods require doing something on clients computer... Hmmm, seems to be a bit tricky in case of an internet or extranet Web site!
 
Solution #3 is to duplicate and rename the JS file which contains the presence script, comment out some code and register this new version by specifying its name through the name attribute of a ScriptLink object. Great, it does the trick! But what if I want to be a good guy and do not load core.js for my branding website? Let's remind the solution for this tweak: I need to empty script link of any name attribute... a dog eating his tail. And what if I want to have presence features enabled when my users are Windows authenticated?
 
A true Best-of-Breed solution: Control Adapters
 
Control Adapter is a very handy solution when you want to update the renderings of WebControls you are not responsible for. In our case, the trick is to create a control adapter for Microsoft.SharePoint.WebControls.ScriptLink and change the rendering behavior when authentication method is not Windows based. If the user is anonymous or form-based authenticated, we load a light version of the presence JS script and we suppress core.js.
 
Let's talk about code
 
First, we create a class library project in Visual Studio, sign the assembly and create a class ScriptLinkAdapter inheriting from System.Web.UI.Adapters.ControlAdapter.
 
Then, we override the Render method and we paste the code below.
 
   1:  // Retrieve authentication type
   2:  string authenticationType = HttpContext.Current.User.Identity.AuthenticationType;
   3:   
   4:  // Replace output with a minimal output if authentication is anonymous or forms-based
   5:  if (!HttpContext.Current.Request.IsAuthenticated ||
   6:      String.IsNullOrEmpty(authenticationType) ||
   7:      authenticationType.Equals("Forms", StringComparison.InvariantCultureIgnoreCase))
   8:  {
   9:   
  10:      // Load the ouputted HTML in memory
  11:      using (MemoryStream memoryStream = new MemoryStream())
  12:      using (StreamWriter streamWriter = new StreamWriter(memoryStream))
  13:      {
  14:          using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
  15:          {
  16:              Control.GetType().InvokeMember(
  17:                  "RenderControl",
  18:                  BindingFlags.InvokeMethod 
  19:                  | BindingFlags.Instance 
  20:                  | BindingFlags.NonPublic,
  21:                  null,
  22:                  Control,
  23:                  new object[] { htmlTextWriter, null });
  24:          }
  25:   
  26:          // Flush the writer and rewind the stream
  27:          streamWriter.Flush();
  28:          memoryStream.Position = 0;
  29:   
  30:          // Read the stream as a string
  31:          using (StreamReader streamReader = new StreamReader(memoryStream))
  32:          {
  33:              string output = streamReader.ReadToEnd();
  34:   
  35:              // Remove core.js
  36:              string corePattern = @"\<script.+src\=.+core\.js.+\<\/script\>";
  37:   
  38:              output = Regex.Replace(output, corePattern, "", RegexOptions.IgnoreCase);
  39:   
  40:              // Replace init.js by a version excluding presence script
  41:              string initPattern = @"(\<script.+src\=\"")(init\.js)(\"".+\<\/script\>)";
  42:   
  43:              if (Regex.IsMatch(output, initPattern))
  44:              {
  45:                  output = Regex.Replace(output,
  46:                      initPattern,
  47:                      String.Format("$1{0}$3", Page.ClientScript.GetWebResourceUrl(
  48:                              typeof(ScriptLinkAdapter),
  49:                              "ScriptLinkAdapter.ClientResources.SPInitNoPresence.js")),
  50:                      RegexOptions.IgnoreCase);
  51:              }
  52:   
  53:              // Flush new content to response
  54:              writer.Write(output);
  55:          }
  56:      }
  57:   
  58:      // Registrations are done, so exit.
  59:      return;
  60:  }
  61:   
  62:  // Standard control rendering
  63:  Control.GetType().InvokeMember(
  64:      "RenderControl",
  65:      BindingFlags.InvokeMethod 
  66:      | BindingFlags.Instance 
  67:      | BindingFlags.NonPublic,
  68:      null,
  69:      Control,
  70:      new object[] { writer, null });
 
First, we retrieve the authentication type to apply transformation only for anonymous and forms authenticated users (line 1 to 8)
 
A key thing to know about control adapters is how to render the adapted control in standard way if no transformation has to be applied. A common mistake is to make a call to the RenderControl of the adapted control. If you do so you will enter an infinite loop: all controls are designed to look for an adapter before rendering themselves, if an adapter is found then the adapter is used and if the adapter call then render method again, the control will look for an adapter which calls the render... the dog and his tail again!

The solution is to use reflection to call a protected version of RenderControl that take a ControlAdapter as an input parameter. If we pass null, no adapter is assumed and the normal rendering cycle is called (line 14 to 24).
 
We are now able to get the original HTML content produced when rendering a ScriptLink control normally. We load this content into a MemoryStream, make some changes on it to suppress core.js and replace the old version of init.js and we flush this new content to the HtmlTextWriter attached to the Response object (line 33 to 54).
 
How to deploy
  1. Register assembly Prexens.SharePoint.WebControls.Adapters.ScriptLink.dll to the GAC of your SharePoint server
  2. Copy file Prexens.SharePoint.WebControls.Adapters.ScriptLink.browser to the folder App_Browsers of the application folder of your Web application (typically C:\Inetpub\wwwroot\wss\VirtualDirectories\<Your App Pool>)
  3. IMPORTANT: open the file App_Browsers\compat.browser in Notepad and save it (no need to change anything). This will force reloading the browser files. If you have multiple browser files already present you might need to repeat for each one.
  4. Restart IIS services (iisreset.exe in a command prompt)

Comments

smart asembler says :

How about a simple solution like the one described here: http://consulting.ascentium.com/blog/sp/Post14.aspx  ?

10/29/2009 2:54 AM

Olivier METRAL says :

True, this solution is straightforward.

But it prevents presence features ProcessImn is made for to occur, and this in any case. So your SharePoint site won&#39;t display users presence information even if the authentication method is compliant with this feature.

10/29/2009 8:51 AM

Add a new comment

Please sign-in to add a new comment
Categories
Links
  Prexens Web Site
  Charting for SharePoint
  Charting for SharePoint (Codeplex)
  Charting for SharePoint Starter kit

Microsoft Certified Partner