This tutorial is for people making their first steps in coding a Web Part for a Sharepoint server. This tutorial describes how to configure Visual Studio 2005 to write Webparts, how to deploy the Web part to the Sharepoint Server and giving hints how to get access the content of the Sharepoint Server like documents, discussions, folders and users.
1. Get Visual Studio 2005 prepaired for the developement of Windows Sharepoint Web Parts
First of all you need to install the Visual Studio 2005 Extensions for Windows SharePoint Services. On following site you get all the informations on how to get the extensions and how to create your first Web Part project:
As you have read on the site of the upper link, the deployment is realy easy and in fact it is!
If you end up with the deployment error “No Sharepoint Site exists at the specified URL” simply right-click your project in the Solution Explorer, select Properties and go to the debug section. There you only have to insert into the field “Start browser with URL” the URL to the SharePoint site.
2. Writing a”Hello World” Web Part
After creating a new Web Part project you end up with various files and a class file with the name of your project . The following code shows the initial code of your Web Part class file. The only change I made is done in the write statement where I wrote “<h1>Hello World</h1>”.
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Web_Part1
{
Guid(”1b215a64-9c05-4183-9026-735a0a68b772″)]
public class Web_Part1 : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write(”<h1>Hello World</h1>”);
}
}
}
Now you have just to press F5 to compile and deploy it on the SharePoint Server. From now on you can use this Web Part on the SharePoint Server.
3. Accessing the content of the SharePoint Server
After getting the
In order to access the content of the SharePoint Server you have to include the Sharepoint library. Just add the following statment to the top of your class file
Next you need to reference your site you want the content of. The following example shows how to access all the files on a site. It parses all the files and creates a simple list of those files:
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Web_Part1
{
Guid(”1b215a64-9c05-4183-9026-735a0a68b772″)]
public class Web_Part1 : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
//Create the reference to my site
SPSite mysite = new SPSite(”http://url/to/my/site”)
SPWeb myweb = mysite.OpenWeb();
writer.Write(”<ul>”);
//parse the folders for files
for (int i = 0; i < myweb.Folders.Count; i++)
{
SPFolder currentlibrary = myweb.Folders[i];
SPFileCollection files = currentlibrary.Files;
for (var k = 0; k < files.Count; k++)
{
//Print the filenames
SPFile file = files[i];
writer.Write("<li>");
writer.Write(file.Name);
writer.Write("</li>");
}
}
writer.Write("</ul>");
}
}
}