Showing posts with label webpart. Show all posts
Showing posts with label webpart. Show all posts

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=1.0.0.0, 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.

Moss 2007 Custom webparts (Part I-Environment Setup.)

The first thing any Moss2007 developer think about is how to create custom webpart and see how it will enhance his website functionality even if this start is the most famous Microsoft starting application "Hello World".


But here i'll go directly to the point which is "Start with environment setup, some concepts, developing simple webpart does something useful and walk through some enhancements to get robust one".


So with this series whatever your proficiency level you will find your start-point for doing this stuff.


Sorry for long intro. But i see it is nice to explain the strategy i'll follow at any issue i'll blog.


let's have some fun.....


Tools i use:
  • VS.NET 2008.
  • SharePoint Object Model(APIs-Application programming interface- for programmers).
  1. Open VS.NET 2008, from menu File --> New -->Projects.
  2. From left pane"Project types" select Visual C#--> Windows.
  3. At right pane"Templates" select Class Library.
  4. At text boxes down type your project "webpart" name "FirstWebPart".


  5. Let Visual Studio create the necessary project components for you.
  6. From Solution Explorer rename Class1.cs to FirstWebPart.cs.
  7. NOTE: At code editor, text after namespace and class are changed automatically(save seconds to rename it manually-thanks Microsoft).



  8. It's time to add some "DLLs" to help us to do our job.
  9. From Solution Explorer, right click on References --> Add Reference.

  10. Dialog Add Reference will appear and from .NET tab select tow references(System.Security - System.Web).

  11. To finish this step we missed only one important reference..Guess what? YES, it's SharePoint reference

  12. To get SharePoint DLL to be referenced at your project it depends on where you develop your webpart now, is it on machine with MOSS 2007 installed? or your own winXP machine(sharepoint not installed locally).

  13. CASE 1 "Machine with MOSS 2007 installed": Dialog Add Reference go to Browse tab and go to this URL(assuming your moss installed on c: drive)
    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

  14. CASE 2 "Local winXP machine": Ask your system administrator to locate this DLL on MOSS 2007 server and send it for you from mentioned ISAPI hive and save it at your local machine and locate it using previous step.

  15. At Solution Explorer the References node will look like the image below.
  16. Add directives to get access to different class inside references added earlier as Step1 shown in image below.
  17. Let your class inherits from WebPart class "That's why we add SharePoint reference-POINTS 12, 13 and 14"as Step2 shown in image below..

  18. After all these steps we have readymade environment for our development.

Now we have space to start our development.

Wait me next article and it will be soon...