Skip to content


How to create a new feature in sharepoint using Visual Studio

In this post I will describe how to make pretty much the simplest Feature ever known to SharePoint :)
It will give you a glimpse on how a feature is constructed and also how you can deploy it on your testserver which will most probably be the same machine as you develop on.

What is a feature

Features consist of a feature.xml file and 0 or more element XML files. These XML files contain CAML (Collaborative Aplication Markup Language) This is placed in a folder and copied into the 12 hive of sharepoint, in the folder called features:
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES

The feature.xml file is the manifest file for the feature. It contains the features unique GUID, its title, description, scope etc.
The element files are actually the working horses of the features. They contain the CAML for creating Content Types, uploading files, added webparts to WebPart Pages, lists, adding items to the menues etc.

Create the feature

Create a new project in Visual studio and create folder structure

“File” -> “New” -> “Project”

Choose “Class Library” template and give it a name (SimpleFeature)

SharePoint needs the feature to be deployed in a special folder in what is called the “12 hive”.
“C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”
This Features folder contains all features on the server. This is where we will deploy our new feature as well naturally. The features each have their own subfolder and inside it there will be at least 2 files: a feature.xml file which contains caml that describes the feature (Title, ID etc.), and most often an elements.xml file (can be renamed since its referenced in feature.xml).
In order to deploy to this folder its easiest to replicate the folder structure in the Visual Studio Project.

Create a folder structure in the project that looks like this

12\TEMPLATE\FEATURES\MySimpleFeature\ (Make a folder named “12″, inside the folder, create one named “TEMPLATE” etc.)

A lot of the examples in books and online will use TEMPLATE\FEATURES\MySimpleFeature as the folder structure, but I have gotten used to having 12 hive as root folder, since I often package my solutions using WSPBuilder from codeplex. This in conjunction with SharePoint Solution Installer makes it easy to install features etc, without much trouble, and without console. It gives a nice .EXE file the customer or yourself can use (will describe this at another time).
In the MySimpleFeature folder create an xml file and name it feature.xml

Enable Intellisense in the xml files (LOTS easier)

Open the file and click inside the window that shows the content. Notice that the properties panel has a field called “Schemas”. Click in it and Click “…”.

Click “Add…” and browse to the folder named “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML”
This folder contains the schema for wss so intellisense will be enabled.

Choose “wss.xsd” and click “Open” and then “OK”.

Intellisense will now work. In the xml file type “<" and notice it gives some options. One of them is "Feature", choose that one.

Create feature.xml and elements.xml

Write the following in the feature.xml file

<feature>
  Id="66174B7C-91DD-4E73-94B3-2DD86D9C6EE9"
  Title="My Simple Feature"
  Description="This is a simple feature that will only create a link in Site Actions"
  Scope="Site"
  Hidden="FALSE"
  ImageUrl="HTML32.gif"
  xmlns="http://schemas.microsoft.com/sharepoint/">
 
  <elementmanifests>
    <elementmanifest location="elements.xml"></elementmanifest>
  </elementmanifests>
 
</feature>

ID is a unique identifier for this feature, and can be used to reference the feature through code or CAML.
Title is the name you will see it under when you see the list of features in SharePoint that you can Activate / Deactivate
Description will be listed under the title in SharePoint
Scope can be one of 4; Farm, WebApplication, Site or Web depending on what the feature is meant to change in some way. Site here means Site Collection.
Hidden determines wether or not it should be listed in the ManageFeatures.aspx page in SharePoint or not. Hiding features that local Site admins should not deactivate can be a good idea, although it might cause a few headaches for the aspiring developer that overlook them.
ImageUrl is the url of the image to be displayed in the ManageFeatures.aspx page.
ElementManifest is a reference to an xml-file that will contain a lot more than the feature.xml. It will be CAML that can add menuitems to various places in SharePoint, provision files, Content Types, Fields, Workflows etc.

Create a new file called elements.xml, also in the MySimpleFeature folder

Insert the following into it:

<elements>
  <customaction>
    Id="MyLink"
    GroupId="SiteActions"
    Location="Microsoft.SharePoint.StandardMenu"
    Sequence="1000"
    Title="Code-Journey"
    Description="Link to Code-Journey - A SharePoint and .NET blog"
    ImageUrl="_layouts/images/HTML32.gif">
      <urlaction url="http://code-journey.com">
 
  </urlaction>
</customaction></elements>

GroupID and Location specifies where the new menuitem is to be added. There are other places than just Site Actions to place menuitems.
Sequence determines where on the menu the link will be placed. The smaller the number the higher on the menu.

Create deployment batch script

Create a new textfile and copy the following into it:

@SET HIVE="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12"
@SET STSADM="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm" 
 
Echo Copy files from VS project to 12 Hive
xcopy /e /y 12\* %HIVE%
 
Echo Install MySimpleFeature
%STSADM% -o installfeature -filename MySimpleFeature\feature.xml -force
 
Echo Restarting IIS
IISRESET

(If you are using VS2008 read this post to correct encoding problems that will probably surface when you try to use the file)

You can either just manually click this file after build, or you can create a post-build script in Visual Studio (Right-click project in “Solution Explorer”, choose properties, choose “Build Events tab”, and enter the following in the Post-Build textbox).

cd $(ProjectDir)
install.bat

Batch scripts are one of the easiest ways (if not the easiest way) to deploy some code and files to your test environment, especially when they are added to the post-build event. It’s pretty much a 1-click test deploy.
The batch file will get a little more complex when you actually have a dll to deploy too, since you will need to use gacutil.exe then.

Build the project and if you didn’t use postbuild script, double-click the install.bat in explorer.

Activate and test the feature

Open up your WSS or MOSS site and go to “site Actions” -> “Site Settings” for WSS, or “site Actions” -> “Site Settings” -> “Modify All Site Settings” for MOSS.

In the Left most column click “Site collection features” (this is the and click “Activate” for “My Simple Feature”.

This was a quick rundown of how to create a feature and deploy it for testing.
You can download the Visual Studio Solution file below.

Simple Feature (21)

DeliciousDiggTwitterFacebookLinkedInTechNetStumbleUponNewsVineTechnorati FavoritesSlashdotMSDNShare

Related posts:

  1. Enable intellisense in Visual Studio for SharePoint / WSS xml files
  2. Add an Event Handler (Feature Receiver) to a SharePoint Feature
  3. Create Simple SharePoint Hello World WebPart And Add It To a Web Part Page
  4. Feature ’75a0fea7-c966-4e74-b74e-8f77e7bae175′ is not installed in this farm, and can not be added to this scope.
  5. Quick intro to creating custom SharePoint (MOSS, WSS) site themes

Posted in SharePoint 2007.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. someone says

    A nice post. But, i am able to do this but have some confusions in the middle . :P



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin