Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

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.

Wednesday, March 12, 2008

Developing SharePoint Web Part using Visual Studio

Refer my earlier posts on installing Microsoft Office SharePoint Server (MOSS) 2007 and creating website if you need more information.

This walkthrough provides the steps for creating a basic custom SharePoint Web Part using Visual Studio 2008 or 2005. It is a very simple Web Part that displays the logged-in user name with a Hello message.

1. Create a new Visual C# Class Library Project in Visual Studio.
  • Start Visual Studio.
  • On the File menu, point to New, and then click Project.
  • In the New Project dialog box, click Visual C# Project Type, and then select the Class Library template.
  • Type HelloWebPart as the name and specify the location for the project files, and then click OK.
2. Add reference to System.Web..
  • On the Project menu, click Add Reference.
  • On the .NET tab, double-click System.Web.
  • Click OK.
3. Add Namespace Directives.
  • First, rename the class file from Class1.cs to Hello.cs (Confirm by clicking yes).
  • Add the following using directives near the top of your code.
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
4. Inherit From the WebPart Class.
  • Derive the Hello class from System.Web.UI.WebControls.WebParts.WebPart class.
    namespace HelloWebPart
    {
    public class Hello: WebPart
    {

    }
    }
5. Override the RenderContents base class method.
  • This method is used to render contents on the web part. Here we are displaying a Hello message to the current user.
    protected override void RenderContents(HtmlTextWriter writer)
    {
    writer.Write("Hello, " + this.Context.User.Identity.Name);
    }
6. Build this project to get the assembly HelloWebPart.dll
If we need to render a control instead of contents, override the CreateChildControls and RenderControl method as shown below:
     protected override void CreateChildControls()
{
//Create a Calendar control and set its properties.
Calendar helloCalendar = new Calendar();
helloCalendar.Enabled = true;
helloCalendar.ShowGridLines = true;
helloCalendar.ShowTitle = true;
helloCalendar.EnableViewState = true;
helloCalendar.SelectedDate = DateTime.Now;

//Add this Calendar to the Web Part.
this.Controls.Add(helloCalendar);
}
public override void RenderControl(HtmlTextWriter writer)
{
this.RenderChildren(writer);
}
In the above example, we are creating a calendar control and setting its properties and then adding it to the web part. The RenderControl method is used to call the RenderChildren method, which causes the children controls to be rendered on the particular HtmlTextWriter passed as a parameter to the method.

If this assembly needs to be strong named (to enable GAC deployment), follow the below steps:
  • Launch the Visual Studio Command Prompt.
  • Type the following command to generate a key pair file:
    sn –k c:\keypair.snk
  • That will generate the key pair file at c:\
  • Refer this file in the web part project’s AssemblyInfo.cs file as below:
    [assembly: AssemblyKeyFile("c:\keypair.snk")]
  • Make sure that the version is also specified here as below:
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
  • Build the project to generate a strong named assembly.
If you are using Visual Studio 2008 or higher, you can can skip all these steps and then go to the Signing tab of the project properties, and choose 'Sign the assembly' option.
We will see how to deploy this web part on SharePoint Server in the coming posts.

Tuesday, March 11, 2008

SharePoint - Creating Web Site

A new web site can be created in Microsoft Office SharePoint Server (MOSS) 2007 easily by following below steps:
1. Launch the SharePoint Central Administration site from a browser using http://servername or http://servername:port.
2. Go to Site Actions > Site Settings (top right menu button).
3. Go to Site Administration > Sites and Workspaces.
4. Click on the Create button.
5. Provide a Title, URL Name, and select a Template. (Choose Team Site for a team web site)
6. Choose the Permissions and Navigation Styles. (Leave it at the default values.)
7. Click on the Create button to create the site.

Web Parts are the building blocks of a SharePoint site. You can add a web part to an existing page in the site by editing the page. Whatever web parts installed and configured on the server will be available to use in any pages.
1. Go to Site Actions > Edit Page (top right menu button). This displays the current page in edit mode.
2. To delete an existing web part, click on the Edit > Delete menu of that web part.
3. To add a new web part, click on the Add a Web Part button on the desired section of the page. All the available web parts will be populated in a popup window. Choose the required one and click the Add button.
4. Click on the Exit Edit Mode link on the top right corner to exit the edit mode of this page.

You can add more pages to the site following below steps:
1. Go to Site Actions > Create (top right menu button).
2. Click on Web Pages > Basic Page link.
3. Provide a Name, and it will be saved under the default Document Library called Shared Documents.
4. Click on the Create button. This will create a blank page where you can add contents.
More such items can be added to a site to make it more interactive. For example, the Wiki Page Library creates a set of wiki pages where the end user can edit the contents of the pages.

Monday, March 10, 2008

MOSS (Microsoft Office SharePoint Server) 2007

Today I got some time to see MOSS in action. Microsoft Office SharePoint Server 2007 is a new server program that is part of the 2007 Microsoft Office system. We can use Office SharePoint Server 2007 to facilitate collaboration, provide content management features, implement business processes, etc.

Installation:
You need Windows Server 2003 to get this installed. Choose the Basic option to install a stand alone version. Advanced option is to install this in a server farm. You get to choose specific SQL Server in this case. There are no other things to remember during installation. Installation automatically creates a new instance of SQL Server (\OfficeServers) and around seven databases.

Configuration:
After the installation, configuration window pops up. Everything is configured automatically including all the services required for MOSS.

Administration:
The server can be administered from the admin web site through the shortcut at:
Start > Programs > Microsoft Office Server > SharePoint 3.0 Central Administration
or directly accessing it from a browser using http://servername:port

Reset the Internet Explorer Security to Low (Tools > Internet Options > Security > Custom Level > Reset to Medium-low) to enable all the administrative tasks in MOSS.

Adding other Administrators:
In the admin portal, we can add more admins to the site using following link:
Site Actions > Site Settings > Site collection administrators

The users are picked from the active directory of the server's domain.

Creating a web site with multiple web parts (items like calendar, discussion forum, etc) is also as easy as clicking few links from the admin portal.