Skip to main content

Develop Asynchronous Web Parts

By default, custom web part uses synchronous mechanism. If in a web part page, there are many web parts, each web part takes some time to retrieve data from either web service or back end database. The overall performance of the web part page will likely be bad. All the web parts rendered in sequence. Second web part must wait until first web part finishes its rendering, third web part must wait until second web part finished its work.

The solution is to use asynchronous mechanism. Asynchronous support is built into the SharePoint web part framework and is very easy to use. You need to follow these steps to use asynchronous call.

  1. Derive a custom Web Part class from base WebPart class in Microsoft.SharePoint.dll assembly.
  2. Override method named GetRequiresData.
  3. Override method named GetData
  4. In GetRequiresData method return a value of true. It tells the Web Part framework to call your overridden version of GetData method.
  5. In GetData method, register a custom callback method by calling a protected method of the WebPart class named RegisterWorkItemCallback. When calling RegisterWorkItemCallback, you must pass in two parameters—a System.Threading.WaitCallback delegate, which refers to a procedure that gets called when the work item is executed, and a parameter of type System.Object that you can use for providing any data that you want passed to the callback procedure when invoking the delegate. The System.Threading.WaitCallback delegate accept a parameter of type System.Object and no return (void) value.
Once you have registered a callback method, the Web Part framework will execute your callback method using a secondary thread.

Handling work item timeouts
By default, work items time out after 5000 milliseconds (5 seconds). Administrators can adjust this timeout period by editing the Timeout attribute of the WebPartWorkItem element in the Web.config file. The following entry creates a timeout period of 15 seconds:

<SharePoint>
<WebPartWorkItem Timeout="15000" />
</SharePoint>

The WebPart class has a RenderWorkItemTimeout method, which you can override to render HTML when the timeout period expires. If you do not override this method, then a default system error is rendered. Also, you can check the value of the Boolean WorkItemTimeout property of the WebPart class—if it is true then at least one pending work item has timed out.

Comments

Popular posts from this blog

Manage IIS 7 remotely using PowerShell and AppCmd

We can use  Windows PowerShell remoting features  to manage IIS 7 websites remotely.  Currently, remoting is supported on Windows Vista with Service Pack 1 or later, Windows 7, Windows Server 2008, and Windows Server 2008 Release 2.  Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator .  Enable PowerShell Remoting with Enable-PSRemoting -Force Starting a Remote Session using:  Enter-PSSession -ComputerName <COMPUTER> -Credential <USER> Now the PowerShell connected to the remote server. Any commands issued with work against the remote server. We can use the Appcmd.exe command line tool to manage remote server just as what we do locally. For example, to add an application pool: c:\windows\system32\inetsrv\appcmd add apppool /name:"Contoso" /managedPipelineMode:Integrated /managedRuntimeVersion:"v4.0" /enable32BitAppOnWin64:true To change application pool for a

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message: System . Security . Cryptography . CryptographicException : The system cannot find the file specified . at System . Security . Cryptography . CryptographicException . ThrowCryptogaphicException ( Int32 hr ) at System . Security . Cryptography . X509Certificates . X509Utils . _LoadCertFromBlob ( Byte [] rawData , IntPtr password , UInt32 dwFlags , Boolean persistKeySet , SafeCertContextHandle & pCertCtx ) at System . Security . Cryptography . X509Certificates . X509Certificate . LoadCertificateFromBlob ( Byte [] rawData , Object password , X509KeyStorageFlags keyStorageFlags ) at System . Security . Cryptography . X509Certificates . X509Certificate2 .. ctor ( Byte [] rawData , String password , X509KeyStorageFlags keyStorageFlags ) In orde

Entity framework code first error: OriginalValues cannot be used for entities in the Added state

When I was using Entity framework code first, I encountered an error when I tried to create an entity into database. The entity is: [ Table (" EmployeeProfile ")]     public partial class EmployeeProfile     {         [ Key ]         [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]         public int EmployeeProfileID { get; set; }         [ ForeignKey ("Employee")]         public int EmployeeID { get; set; }         public virtual Employee Employee { get; set; }         [ ForeignKey (" Profile ")]         public int ProfileID { get; set; }         public virtual Profile Profile { get; set; }         [ Required ]         [ StringLength (255)]         public string ProfileValue { get; set; }     } When creating the entity, some entities have the ProfileValue="", this causes the EntityValidationException with the detailed message " OriginalValues cannot be used for entities in the Added state ". I want to allow the Prof