SQLAuthority News – SharePoint – Steps To Create A Custom WebPart – Deploy It SharePoint Site

SharePoint is one interesting software from Microsoft. My outsourcing location unit is working on one large project of SharePoint. Based on users feedback and overwhelming response to article SQL Server – Error : Fix : SharePoint Stop Working After Changing Server (Computer) Name I am posting one more article which is very important for SharePoint developers.

SharePoint does not allow custom coding for any of the webpart. It is possible to create webpart in Visual Studio and integrate it with SharePoint. The process to create webpart in .NET framework and make it working in SharePoint often fails due to lack of guidance about this subject on internet. Software developer Rashmika and Dhruval has extensively worked in this subject and helped me to create this tutorial.

1. First of all we need to create a webpart in visual studio (2005,2008). For that, Go to File>> New>>Project.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step1

2. Under Templates section select WebPart (this option is available only in 2008). For VS 2005, select Class Library and add reference for System.Web.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site Step2

3. Write code WebPart you want to create. Here, code is written for a label having text “Hello World”.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace CustomWebPart
{
[Guid(“b8f18240-bc27-4f08-a03f-61bd44279d1a”)] public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label = new Label();
label.Text = “Hello World”;
this.Controls.Add(label);
}
}
}

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site Step3

4. Now, Buid Webpart and generate an assembly for this webpart. For that GoTo>>Build>>Build Solution. And the GoTo>>Build>>CustomWebPart.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step4

The WebPart is now Build, we need to deploy this assembly in sharepoint site. For deploying webpart we need to perform following steps.

5. Put the assembly in the bin folder of sharepoint site. The MOSS 2007 creates every portal in the inetpub\wwwroot\wss folder. To find the path of bin folder of the portal for which you want to deploy the webpart, identified with port number, run inetmgr(IIS).

Right Click the portal and click on Properties

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step5

6. Under HomeDirectory tab, Local Path describes the whole path, copy the path and verify it by opening it in a browser and see if the bin folder exists, if it does not exist then create a new folder and rename it bin.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step6

7. Now copy the assembly from project output folder.

C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\CustomWebPart\CustomWebPart\bin\Debug.

Paste it in the portal bin folder. C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step7

Now everytime we change the webpart we need to copy and paste the assembly in portal bin folder. Hence to avoid this stuff we can do the following step which will automatically replace the new assembly in the portal bin folder.

8. In VS .Net, Solution Explorer, Right Click the project name(CustomWebPart) Click on Properties, click on Build. Into the Output Path paste the path which we copied from inetmgr console.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step8

9. Now to make the webpart usable we need to modify the web.config file of the portal. To declare the control safe we need to add a <safecontrols> entry in web.config file.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step9

10. For <safecontrols> tag we require a parameter “PublicTokenKey” to generate this key drag the assembly from project folder and drop it in C:/Windows/assembly folder and it will generate a publictokenkey.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step10

Right Click on CustomWebPart and copy Public Key Token.

Open web.config file and enter the following text :

<SafeControls>
…..
…..
<SafeControl Assembly=“CustomWebPart, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=9f4da00116c38ec5”

Namespace=“CustomWebPart” TypeName=“*” Safe=“True”
AllowRemoteDesigner=“True” />

</SafeControls>

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step11

11. Now we need to Configure Portal to use CustomWebPart.

Till now the web part has been created and deployed to the site directory. Now the next part is how to use the web part on the Portal’s Site. The web part created can be placed on any site of the portal.

Open the portal site in the internet explorer, in my case its http://win2003, ensure that the logged in user has the administrative rights on the portal site, else it will not allow adding the web part.

12. Click on the Site Action>>Site Settings

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step12

13. On the site settings page under Galleries column click on the Web Parts.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step13

14. On the Web Part Gallery Page click on the New button, to add the new web part assembly to the gallery.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step14

15. On the New Web Parts page locate the CustomWebPart in the list, check the check box on the left and click on the Populate Gallery button the top of the page. This will result in the Web Part entry creation in the Web Part Gallery list, and hence it can be used from now on from the gallery.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step15

Now the Web Part is ready to be added to the page.

16. Open a site created in the Portal. Click on Site Action>>Edit Page

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step16

17. This will modify the appearance of the page to enable the edit view. In this view Web Part Zones are highlighted so that a user can add a web part to the zone, Click on the Add a Web Part button in the left zone to add the Web Part.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step17

18. Select the CustomWebPart from the web part list . It is found under the Miscellaneous section and then click on Add.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step18

19. Click on the Exit Edit Mode link on the page and the site will return to the view mode.

20. At the end the page will appear like this.

SQLAuthority News - SharePoint - Steps To Create A Custom WebPart - Deploy It SharePoint Site step19

Reference : Pinal Dave (https://blog.sqlauthority.com)

,
Previous Post
SQL Server – Error : Fix : SharePoint Stop Working After Changing Server (Computer) Name
Next Post
SQLAuthority News – 700 Articles and Author Updates

Related Posts

15 Comments. Leave new

  • Pinal,

    I work at Microsoft Bangalore. I wish you are with us. We use SharePoint here but no one knew we could have done what you wrote in post.

    Good. I do not wonder why you are MVP.

    Mohit Sagar
    Program Lead – (Microsoft – Bangalore)

    Reply
  • Steven Mahavindar
    September 10, 2008 9:15 pm

    Wow!!

    Just Great.

    Reply
  • Steven Mahavindar
    September 10, 2008 9:16 pm

    How can we meet you in person?

    Reply
  • Marek Śliwiński
    September 10, 2008 10:11 pm

    Good tut Pinal :) Thank you :) I think you should post more C# SQL Server articles :)

    Reply
  • Its Coo……..L. Really its very nice and appreciable. it will my pleasure when i will work under pinal Sir.

    Suraj kumar kushwaha
    (S/W Developer)
    (ComputerWare India Pvt. Ltd.)

    Reply
  • Great, thanks for the example. I would love to see a simple WebPart example using VS 2008 and SQL 2005 if you have the time or happen to know of one.

    Reply
  • Thats Great, Please Post more!!!!!

    Reply
  • Hello All,

    Thank you for your comments. I will soon post more.

    Regards,
    Pinal Dave

    Reply
  • Hey,Great article.Very nice

    Reply
  • Also open the AssemblyInfo.cs file and add

    using System.Security;
    [assembly: AllowPartiallyTrustedCallers]

    This will allow you to update the webpart whenevr any change is made in order to avoid this error:

    “Unable to add selected web part(s). Assemblies that implement ASP.NET Web Parts and are installed into a partially trusted location, such as the bin directory, must be compiled with the AllowPartiallyTrustedCallersAttribute set for import to succeed.”

    Reply
  • Adam D'Amato-Neff
    December 13, 2008 3:21 am

    good posting; problem I experience is that when I update the webpart and consequently the dll, the existing webpart doesn’t seem to update In SharePoint. Is there another step I am missing.

    Reply
  • Hmmm, I only got the assembly folder part. How do I make an assembly file?

    Reply
  • Hello,
    Thank you for the post.
    How we can add some interactions (events) of this WebPart?
    Also connections to an external DB?
    Thank you,
    Greg

    Reply
  • Hardik Shah [Guru]
    July 22, 2010 4:26 pm

    Thanks ! Was a very informative post … :)

    Regards,
    H.

    Reply
  • Webpart wsp file Deployed successful but not listed in the Web Part Gallery Page.Please help me

    Reply

Leave a Reply

Menu