SharePoint: render Microsoft.SharePoint.WebControls.AspMenu web control asynchronously

Hello everyone!

Today, I’m going to follow up on the post I wrote recently about how I created a custom navigation provider for SharePoint’s top navigation, and how it was quite a resource costly module, resulting in slowness of page loading, thus substracting from end user experience and satisfaction.

A number of customizations I already have in place, such as

  • custom masterpage
  • custom masterpage code behind c# file
  • deployment of resources into SharePoint hive layouts folder

allowed me to piece together a working code that in essence

  1. Upon page load fired a JavaScript code that calls a web service
  2. Web service renders Microsoft.SharePoint.WebControls.AspMenu web control inside a surrogate page
  3. HTML is obtained from rendering the control and is returned to the main page as web service result
  4. Upon HTML recieval the JavaScript code appends this to original page
  5. At this point we have the HTML but we do not have all the JavaScript event hooks that AspMenu control applies to its HTML.
  6. I have figured out the code that re-applies the hooks in JavaScript, so this is the last step of my JS handler.

And, for the code itself

Firstly, masterpage markup and code-behind excerpts:

In the masterage code-behind we are injecting a javascript code into the page, that produces a variable called “currentNavigationProviderName” that holds the name of currently used global navigation provider. I chose this place to pull the value from, as at this point any possible delegate controls for top navigation data source have been applied and we are safe to retrieve the name.

Description how to create and use UlsLogging class can be found in my other post here.

AspMenuAsync web control

Nothing much here, we’re spawning a div that will hold the results from web service call and registering the javascript file

JS code calls custom web service (covered below), on successful receival of HTML appends it and re-applies the out of the box AspMenu control JS hooks on the created HTML.

Web service part

I will not delve deeply here on how to provision your web service, please refer to my earlier post, part of which describes the process.

Once you have web service provisioned with .svc and code-behind in place it’s time to look at my code-behind. What the web service does is create an instance of Page class, add our AspMenu control to it (you might want to tweak the property assignment to suit your needs) and then executes the page in-memory using the HttpContext.Current.Server.Execute method. Resulting HTML is put into a string writer and returned as HTML.

Piecing it all together

In the end we have decoupled loading of top navigation control from the main page loading cycle. This approach could be tailored to loading other types of controls, such as AspTreeView and the like.

Hope this information was valuable in some way, it surely has made UX of our end users more smooth.

Leave a Reply

Your email address will not be published. Required fields are marked *