Monday, August 30, 2010

Basic sharepoint web part in .NET

1     Created a new Windows Class Library project in VS2008
2     Added a reference to System.Web
3     Added the following ‘using’ statements:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
4     Changed the default class from public class Class1 to public class Hello: WebPart
5     Wrote an override for the “RenderContents” function (I got fancy and stripped off the leading domain portion of the login, and later wrote more code to change the font and add the little % bar)
protected override void RenderContents(HtmlTextWriter writer)
 string name = this.Context.User.Identity.Name;
 writer.Write(" You are logged in as " + name + "! ");
6     Built the dll in release mode and put it in a shared dir on my machine.
7     Logged onto the mossdev server and copied the dll to the sharepoint server into the web server’s ‘bin’ directory (\Inetpub\wwwroot\wss\VirtualDirectories\80\bin)
8     Added a line to web.config to make the server trust the dll (\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config). In the section of web.config, I added a entity that looked like this:
9     Went onto sharepoint, went to site settings (for the whole site), and chose ‘Web Parts’ under Galleries. Clicked New, found my ‘Hello’ part, and clicked ‘Populate Gallery’ which added it for use on the site.

1    Went to a site and added the part to my front page. Opah!

No comments:

Post a Comment