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!!).
- 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.
- 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.
- CreateChildControls(): This method can be used to add child controls to a web part and define event handlers for those child controls.
- PreRender(): This is the last event that occurs before the web part output is rendered to the page.
- Render(): This method sends the web part to its HTML writer. This method calls the following methods: RenderBeginTag(), RenderContents(), and RenderEndTag().
- RenderContents(): This method is responsible for adding content to the web part’s
- 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.
- Now Open our solution FirstWebPart and we will override method from WebPart class called RenderContents (defined above).
- We need to modify file called "Assemblyinfo.cs" with adding 2 lines "Using System.Security" as directive and Code line "[assembly: AllowPartiallyTrustedCallers()]".
- This will write message at your page as webpart output .
- 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).
- 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...."
- At Solution Explorer note that new file has been added to the solution "key".
- Now Build your project and be sure you don't have any errors.
- 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).
- Keep Marked information from previous image for later use and delete the file from (C:\WINDOWS\assembly)-I'll tell you why!! later -
- 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")
- 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.
- Open Web.Config file (Ex.:C:\Inetpub\wwwroot\wss\VirtualDirectories\1234)
- Add new line at
node as shown below.
- Now it is time to know how we can deploy our webpart from my SharePoint site itself.
- Follow images below.
< assembly="FirstWebPart, Version=22.214.171.124, Culture=neutral, PublicKeyToken=7dea06abe781fd42" namespace="FirstWebPart" typename="*" safe="True">
Here we did all required steps for moss webpart development and deployment.