Thursday, March 13, 2008

SharePoint - Web Part Deployment

This post is applicable to Microsoft Office SharePoint Server (MOSS) 2007. See my previous posts on Installing SharePoint, Creating Web Site, and Developing Web Parts.

This walk-through provides the steps for deploying a basic custom SharePoint Web Part by either copying the assembly dll to the Server (if not strong named) or by deploying to GAC (if strong named) and configuring it through SharePoint administration site.

1. Copy the web part dll file to the bin directory on the server’s web location if the assembly is not strong named.
a. Log into the computer where SharePoint Server is installed.
b. Copy the web part dll (HelloWebPart.dll) to the SharePoint web’s bin directory. Usually this location will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.
c. If the SharePoint was installed on a different port (say 37729), this location will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\37729\bin.
d. This location can be easily found from the IIS Manager (Locate the appropriate portal, for which u want to deploy the web part, identified with the port number. Right click and have Properties. Under the Home Directory Tab, note the path in Local path text box).
e. If the bin folder doesn’t exist at this location, then you can create one.
2. If the assembly is strong named, deploy it to the GAC. (Note that either Step-1 or Step-2 is required; not both.)
a. Log into the computer where SharePoint Server is installed.
b. Drag and drop the web part dll to the C:\Windows\Assembly folder using windows explorer. This will install the assembly to Global Assemble Cache. After that, right click on the assembly name from here and choose Properties. Copy the Public Key Token value for using in the configuration.
3. Add a Safe Control entry in the web configuration file of SharePoint Server.
a. Open the web.config file from the server’s web location which will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\80\ or at a port number as mentioned above.
b. Add a new entry within the section as below:

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

This indicates that all(*) classes in the HelloWebPart namespace in the HelloWebPart assembly are safe web parts.

If the assembly is strong named, it should have the Public Key Token copied from the previous step.

<SafeControl Assembly="ControlSampleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c627d6df943a8393" Namespace="ControlSampleWebPart" TypeName="*" Safe="True" />

c. Save the web.config file and close it.
4. Configure the server to use this new web part.
a. Open the SharePoint Central Administration site from a browser using http://servername or http://servername:port ensuring that the current logged in user has the administrative rights on the portal site.
b. Go to Site Actions > Site Settings (top right menu button).
c. Go to Galleries > Web Parts to open the web part gallery.
d. Click on the New button.
e. The New Web Parts page displays all the web parts marked as safe on the server. Scroll down to the required web part in the list, check the check box on the left and click on the Populate Gallery button at 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. Notice that the Web Parts developed in latest .NET Frameworks has .webpart as extension while the older ones has .dwp as extension.
5. Add this web part to a page in a site.
a. Go to Site Actions > Edit Page (top right menu button). This displays the current page in edit mode.
b. Click on the Add a Web Part button on the desired section of the page.
c. Select the required web part (Hello) from the All Web Parts > Miscellaneous section of the web part gallery.
d. Click on the Add button.
e. Click on the Exit Edit Mode link on the top right corner to exit the edit mode of this page.

2 comments:

  1. this does not work

    ReplyDelete
  2. Don't forget your target framework in your dll should match the framework used by IIS. If the target framework is > that used by IIS, you may not be able to deploy your dll to the GAC. In visual studio, modify the project properties so the target FrameWork matches what is used by your IIS. Unfortunately you get no error message when this fails, so it took me 3 days to figure this out, thanks Microsoft. Even with this advice, the tutorial does not work.

    ReplyDelete