Wednesday, March 5, 2008

ASPX Pages as SharePoint Features

So here’s the requirement:
You need a custom page in SharePoint (WSS 3.0) that must do something. For arguments sake, we’ll say it needs to allow a user to select information from some data source via a combo box and use that to populate a list.
How do you do this?
Part 1: Develop the web part.
Part 2: Create the web page.
Part 3: Add the web page as a feature to your site collection.

Part 1:
1. Create a new Visual Studio SharePoint project.
2. Add a web part to the project. For the purposes of my demonstration, I’ve created a web part that has a text box (for the list title), a drop down list populated by ten items and an ok button (code at the end of this part).
3. Deploy the web part to your site (in VS, just select deploy from the build menu. This should also create the setup.bat file & the MOSS solution, making future deployment incredibly easy).
4. Create a new web part page in your MOSS site.
5. Create a custom list with fields that match the fields you want to update (in my case, the title text field and an additional text field to hold my selected value).
6. Add the web part to your newly created MOSS page.
7. Test.

using System;
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 AddItem
public class AddItem : System.Web.UI.WebControls.WebParts.WebPart
protected DropDownList ddlAvailableValues;
protected System.Web.UI.WebControls.TextBox txtItemTitle;
protected System.Web.UI.WebControls.Button btnAddItem;

public AddItem()
this.ExportMode = WebPartExportMode.All;

protected override void CreateChildControls()

ddlAvailableValues = new DropDownList();
ListItem[] memoryItems = new ListItem[11];
memoryItems[0] = new System.Web.UI.WebControls.ListItem("Please select a list item", "0");

for(int i = 1; i < l =" new" value =" i.ToString();" text =" string.Format(" cssclass = "ms-RadioText" txtitemtitle =" new" cssclass = "ms-long" btnadditem =" new" text = "OK" cssclass = "ms-ButtonHeightWidth" bloglist =" SPContext.Current.Web.Lists[" item =" blogList.Items.Add();">
My web part looks like this:

Part 2:
So far so good. Now we come to the “let’s cheat to make this easy part”. Hehe, I knew you’d like that.
You don’t want your web part page lying around in the library right? You want it to be part of your site collection, prefrably as a feature. That would be the ideal.
So, we fire up SharePoint Designer.
We open up the web part page that we created and added the web part to.
We copy the ASPX code.
We create a new ASPX file in the windows directory we want to use as our feature deployment folder (in my case BlogPages).
We copy the code into the new ASPX file.
And that creates our ASPX file. Now for ease of deployment.

Part 3:
Create a feature file.
Create an element manifest.
Deploy the feature (I do this via a setup.bat file that resides in the parent directory above my BlogPages directory).
Now, on the site you deployed the feature on, enter the URL of the page, for instance: http://servername/blog.aspx
And there you are. The page is part of your site, activated as a feature.
Life is beautiful.

My files for all this:




Set stsadm="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe"
Set url=""
Set featureFolder="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\BlogPages\"

%stsadm% -o deactivatefeature -name BlogPages -url %url%
%stsadm% -o uninstallfeature -name BlogPages

rd /s %featureFolder% /q

xcopy /e "Feature\*" "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\"

%stsadm% -o installfeature -name BlogPages

%stsadm% -o activatefeature -name BlogPages -url %url%


No comments: