Tuesday, September 25, 2012

How To Deploy a Custom WebPart to Sharepoint

Deployment Methods

We can deploy a custom webpart in 2 ways:
  1. Deploying the Assembly (i.e. DatabaseConnWebpart.dll) to GAC
    This requires the assembly to be strong named, a change in manifest etc.
  2. Other simplest way is to deploy assembly to Sharepoint Virtual Directory folder in IIS, i.e. C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin
Note: Sometimes using way 2 may create trust problems, so if you encounter that issue, try copying your DLL to GAC too. It will solve the issue.

Copying Assembly to SharePoint BIN Folder

Sharepoint Virtual directory can be found at: C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin.
Copy the DLL of DatabaseConnWebPart that we created from your project folder and paste it in the bin folder of portal directory and also GAC, c:\windows\assembly (if you encounter any trust issues).

Adding the Safe Control Entry

Even though the assembly is present in the Portal’s Bin folder, there is another step required to make the Control (Web Part) assembly usable on the Portal Pages.
Since the control will need to render on multiple machines in different browsers with as many user accounts as the organizations have, there is a need to declare the control as “safe”.
To do so, open the web.config file of Sharepoint portal placed under the portal’s directory, and look for node in web.config and add the following Safe control code:
// Check for this node

<SafeControls>
………..
………....

// Add this line of code which ensures webpart is safe

<SafeControl Assembly="DatabaseConnWebPart " Namespace="DatabaseConnWebPart "
    TypeName="*" Safe="True" />

</SafeControls>
Now that we declared the webpart as safe, our webpart will be deployed to Sharepoint Portal.

Configuring Portal to Use NewWebPart

Open your Sharepoint portal and on your “Home”, click Site Actions -> Site Settings -> Modify all Site Settings as shown in this figure:

SiteSettings_small.jpg
Sharepoint will open the Settings page.
Click “Webparts” under Galleries as shown below:
webparts_small.jpg

Adding Custom Webpart to Webparts Gallery

Click “New” in toolbar to add a new webpart as shown below:
WebPartGallery_small.jpg

Sharepoint will open the webparts gallery, choose your DatabaseConnWebPart from the gallery and check the checkbox to the left of webpart and click Populate Gallery on top as shown below.
Note: You can rename your webpart anyway you want. Use the TextBox provided to the right of your webpart to rename it accordingly.

NewWebPart_small.jpg

Now we have successfully added our new custom web part to Sharepoint portal. We can go ahead and use that webpart in any site that we want.
Note : If you want to make sure whether your webpart is added to gallery or not, check the Webpart Gallery page. It should have your webpart.

Using DatabaseConnWebPart in Site

To use our new deployed webpart in a site, open the site and Click Site Actions -> Edit Page.
This will show the site in edit mode, now click "Add a WebPart" as shown below:

AddwebPart_small.jpg

This will open the Add WebPart dialog box. Choose your webpart (in Miscellaneous section) and check it and click "Add" as shown below:

Add_webparts_-WebPage_Dialog.jpg

This will add the webpart to your home page and now your homepage as shown below:

HomepageWithWP_small.jpg

Conclusion

This is how we can create custom webparts, deploy them to Sharepoint server and use them in our sites.

No comments:

Post a Comment