Speeding up SharePoint Portal Server Web Parts are relative easy if you understand the asynchronous data retrieval built into the Web Part framework. Just one problem, the second asynchronous thread runs under the system account's permissions.
This article is not on how to get asynchronous data retrieval working (even though the code is included), but rather on how to get the current logged on user's credentials to be used while running the second asynchronous thread.
While developing a treeview navigation Web Part, I had the opportunity to get my hands dirty with the asynchronous data retrieval functionality built into the Web Part framework. It was easy enough to get to work. The only problem that I found was that when I wanted to determine (during the asynchronous routine) whether the logged on user had permissions to view an area, it would always return
- You are familiar with creating Web Parts in VS.NET.
- You have the Web Part framework installed and running on your development PC.
Using the Code
You have to return
True for the
GetRequiresData method so that the Web Part will know to use asynchronous data retrieval.
Public Overrides Function GetRequiresData() As Boolean
As you have indicated that you want to use asynchronous data retrieval, the framework will next check the
GetData method to see which callback method to call. You use
RegisterWorkItemCallback to register a method of type
Threading.WaitCallback. No value is returned, but you have to pass a value of type
Object to the callback method.
Public Overrides Sub GetData()
AddressOf ImpersonatedCallbackMethod), Nothing)
Now that the asynchronous data retrieval is ready, you have to get the callback method configured so that the currently logged on user is impersonated.
Private Sub ImpersonatedCallbackMethod(ByVal usr As Object)
Impersonate the currently logged on user:
Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
currentWindowsIdentity = CType(System.Security.Principal.WindowsIdentity.GetCurrent, _
impersonationContext = currentWindowsIdentity.Impersonate()
Run any script that needs to use the permissions of the current user, and finally remove the impersonation.
Points of Interest
The asynchronous thread will timeout after the default 7 seconds, and is controlled by the
WebPartWorkItem element in the web.config. You will therefore have to add a
RenderWorkItemTimeout method to the Web Part with the custom message that you want to display when a timeout occurs.