Friday, August 8, 2008

Moss 2007 Custom webparts (Part II-Development.)

Well, after we setup our world(VS.NET project) to start coding MOSS custom webpart PART I here we will go into development phase(cheer up developers).

Below i'll talk about webpart life cycel to know (when!! we can do what!!).

  1. OnInit(): This event handler is called immediately before the OnInit() method of the pagethat hosts the web part. This method can be used to initialize values required within the web part.
  2. OnLoad(): This event is called immediately before the OnLoad() method of the page that hosts the web part. This method is typically used to interact with the controls that are part of the web part.
  3. CreateChildControls(): This method can be used to add child controls to a web part and define event handlers for those child controls.
  4. PreRender(): This is the last event that occurs before the web part output is rendered to the page.
  5. Render(): This method sends the web part to its HTML writer. This method calls the following methods: RenderBeginTag(), RenderContents(), and RenderEndTag().
  6. RenderContents(): This method is responsible for adding content to the web part’s
    HTML writer.
  7. UnLoad(): This event occurs when the instance of the web part is discarded; at that time, the response is already sent back to the client. This is a good place to release any handles to resources that are still left open.
I think these events are clear and understandable.
  1. Now Open our solution FirstWebPart and we will override method from WebPart class called RenderContents (defined above).
  2. We need to modify file called "Assemblyinfo.cs" with adding 2 lines "Using System.Security" as directive and Code line "[assembly: AllowPartiallyTrustedCallers()]".
  3. This will write message at your page as webpart output .
  4. To add your wepart to sharepoint, your DLL should be strongly signed(i.e. should has strong name key file this file grant we have only one PublicTokenKey across rebuilding your webpart project).
  5. To get this key file right click on your peoject name -->Properties -->sign -->check sign the assembly --> select "New" from combobox "choose strong name key file"/"Browse if you will use already exist key file-not recommended-" -->enter key file name "key" --> unchecked "protect my key file...."
  6. At Solution Explorer note that new file has been added to the solution "key".

  7. Now Build your project and be sure you don't have any errors.
  8. try yo drag your Firstwebpart.dll from your project directory (Ex.:C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\FirstWebPart\FirstWebPart\bin\Debug) and drop it at (C:\WINDOWS\assembly) to get ProductTokenKey and other information needed to register the Firstwebpart.dll into Sharepoint. (you can use any refactor tool to extract these information).

  9. Keep Marked information from previous image for later use and delete the file from (C:\WINDOWS\assembly)-I'll tell you why!! later -
  10. Now it's the time to deploy our webpart to sharepoint as every thing is ready and we have many ways to deploy it and we will discuss all in coming series but here I'll use manual deployment and we have 2 ways here (Deploy your DLL to GAC:Not recommended as your dll will be available to be used in range wider than desired or to your portal Bin directory: Recommended for this reason we modify "Assemblyinfo.cs" file in Point "2")
  11. Define your DLL as safecontrol to tell sharepoint this new control is completely safe to deal with by open your webapplication BIN directory (Ex.:C:\Inetpub\wwwroot\wss\VirtualDirectories\1234\bin) and paste FirstWebPart.DLL into it.
  12. Open Web.Config file (Ex.:C:\Inetpub\wwwroot\wss\VirtualDirectories\1234)
  13. Add new line at node as shown below.

  14. < assembly="FirstWebPart, Version=, Culture=neutral, PublicKeyToken=7dea06abe781fd42" namespace="FirstWebPart" typename="*" safe="True">

  15. Now it is time to know how we can deploy our webpart from my SharePoint site itself.
  16. Follow images below.

Here we did all required steps for moss webpart development and deployment.

No comments: