Tuesday, July 21, 2009

SharePoint Solution Hello World Tutorial

By Hristo Yankov

Hi,

In this post, I will explain to you how to create your first SharePoint 2007 ASP.NET application. We will cover what tools you would need and what are the steps to creating a SharePoint Hello World app! I will keep the article short and clear on the necessary steps to be followed.


First, let's start with the tools. You will need Visual Studio 2008 SP1 (preferably) this freely available plugin for it Visual Studio 2008 extensions for Windows SharePoint Services 3.0. It is a toolset for developing custom SharePoint applications: Visual Studio project templates for Web Parts, site definitions, and list definitions; and a stand-alone utility program, the SharePoint Solution Generator.

We assume that you have setup and you do have available a SharePoint site, on which we will be deploying our solution.

So, start your Visual Studio 2008. From the menu choose to start a new Project.


Visual Studio will show this window. Select GAC and proceed.


Your Solution Explorer would look like this, initially:


We need to create a couple of folders. Recreate the folder structure described in the screenshot below:


Right click on HelloSharePointWorld and select add new item. Select text file, but name it "Hello.aspx"


Put the following content in your newly created ASPX page.

<%@ Page Language="c#"
Inherits="Hello, SharePointHelloWorld, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=3cf44204a7ad1f1e"
MasterPageFile="~/_layouts/application.master"
%>

<asp:Content ID="Content1"
ContentPlaceHolderID="PlaceHolderMain"
runat="server">
<asp:Label ID="lblLabel1" runat="server" Text="Hello World!">
</asp:Label>
</asp:Content>

Note the PublicKeyToken value. It will be changed later. Now, create a new class in the App_Code folder. Name it Hello.aspx.cs. Your directory structure should look like:


The content of your Hello.aspx.cs should read:

using System;
using Microsoft.SharePoint.WebControls;

public partial class Hello : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
}


Right click on your project, select Properties from the menu. Go to the Debug tab and set the hostname and port number of the SharePoint site where you want your project to be deployed.


Then save.

Now, right click on your project in Solution Explorer and rebuild it. Then right click on it and Deploy.


Now, open Windows Explorer and navigate to C:\WINDOWS\assembly. You should see an assembly called SharePointHelloWorld there. Right click it and select Properties.


Copy the Public Key Token (highlighted, note that it would be different for you) and click Cancel button. Now go back to your ASPX page in the project and replace the incorrect Public Key Token with the one you just copied.

Redeploy your application. Start Internet Explorer and navigate to http://[server name]:[port]/_layouts/HelloSharePointWorld/Hello.aspx

You should see something similar to:


Congratulations, you have created your first SharePoint solution. If you start the Central Admin for SharePoint and go to Operations -> Solution Management you should see a solution named sharepointhelloworld.wsp.

Let's add one more modification. Open the Hello.aspx.cs file and change it to:

using System;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

public partial class Hello : LayoutsPageBase
{
protected Label lblLabel1;

protected void Page_Load(object sender, EventArgs e)
{
lblLabel1.Text = "Hello world! Time is: " +
DateTime.Now.ToString();
}
}



By that, we utilize the code behind to output data on the screen. Rebuild, redeploy and refresh the page.


This is a good starting point for further learning by experimenting and example.

Of course, the full code for this project can be found here.
Bookmark and Share

5 comments:

Paul M said...

Hello,

From what I've seen in other, more complex tutorials, the steps outlined in the article seem like they should work.

I had questions about 2 things:
1) The App Page is having trouble finding the code behind file in the GAC. Error message: Could not load type 'Hello' from assembly 'SharePointHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5'.
Any ideas?
2) Do you know where the source code went?

Thanks,
Paul

HY said...

Hi,

1) Check if the assembly is in the GAC (C:\Windows\assembly) and that the key token is the same.

2) Do you mean the .aspx page? If yes, then that would be 'Program Files\Common Files\Microsoft Shared\web server extensions\12\templates\layouts\SharePointHelloWorld'

Paul M said...

Thanks for the quick reply.

Yeah, the token is the same in the Page directive and the assembly within the GAC.

The code I was referring to was the source for your sample. Clicking on the link to download your project ZIP brings you to a "Invalid/deleted file" at Mediafire. Do you think it got taken down by accident?

HY said...

Yes, I should have used a more reliable hosting for the demo project. I will try to find the source again and upload it.

As for the first issue, ensure that the assembly name in the GAC is 'SharePointHelloWorld' and that your codebehind page (the .cs) has no namespace (or if it does, modify the .aspx to reflect so). I know it sounds obvious, but I just can't think of what else could the problem be.

Except maybe if the user who is running the application doesn't have enough permissions to pull an object from the GAC.

Anonymous said...

i am having error in hello.aspx file.we need any master page for that