creating sharepoint features

download creating sharepoint features

of 13

Transcript of creating sharepoint features

  • 8/7/2019 creating sharepoint features

    1/13

    You searched for 'sharepoint 2007 installation step bystep'

    Here are some results for the search term on this website

    1. Developing a SharePoint 2007 Feature

    2. Added a couple of SharePoint Experiments...

    3. Increase your VMWare disk size...

    O c t o b e r

    - - -

    2 0 0 7

    Developing a SharePoint 2007 Feature...

    a step by step tutorial.

    I have worked with SharePoint for a while now, mostly mostly writing code for integration scenarios,

    where data needs to be "pulled" or "pushed" into SharePoint involving other enterprise softwareapplications.

    SharePoint 2007 Features are basically a mechanism to extend SharePoint in any way you need.

    Mike Ammerlaan wrote a brief and concise technical article describing features:

    I will try to describe how to develop a SharePoint 2007 Feature from zero. In this case our feature will

    handle the event that SharePoint usually handles when a file gets added to a Document Library. When

    a user uploads a new document to a Document Library, we want to run some custom code. In our

    example we will simply be creating a text file on the desktop containing data from the file that fired

    such event. Obviously you would want to make it something meaningful, like pass that data to an

    external workflow application or do womething with that document, but this is just an example.

    1. Setting up our project

    Launch Visual Studio 2005 and create a new Class Library project.

    File > New Project > Class Library

    http://www.miguelmoreno.net/post/Developing-a-SharePoint-2007-Feature.aspxhttp://www.miguelmoreno.net/post/Couple-of-SharePoint-Experiments.aspxhttp://www.miguelmoreno.net/post/Couple-of-SharePoint-Experiments.aspxhttp://www.miguelmoreno.net/post/Increase-your-VMWare-disk-size.aspxhttp://www.miguelmoreno.net/post/Increase-your-VMWare-disk-size.aspxhttp://sharepoint.microsoft.com/blogs/mike/Lists/Posts/Post.aspx?ID=7http://www.miguelmoreno.net/post/Couple-of-SharePoint-Experiments.aspxhttp://www.miguelmoreno.net/post/Increase-your-VMWare-disk-size.aspxhttp://sharepoint.microsoft.com/blogs/mike/Lists/Posts/Post.aspx?ID=7http://www.miguelmoreno.net/post/Developing-a-SharePoint-2007-Feature.aspx
  • 8/7/2019 creating sharepoint features

    2/13

    Name it SharePointEventHandler and let Visual Studio create the project files for you in the Solution

    Explorer.:

    Add a reference to the SharePoint Services API assembly. Right-click in your solutions explorer on the

    reference folder and select Add reference

    The list of available .NET assemblies shows up

    Scroll almost all the way to the bottom and select Windows SharePoint (r) Services, Version 12.0.0.0

    Go back to the Solution Explorer and rename you class1.cs file to ItemEventreceiver.cs

  • 8/7/2019 creating sharepoint features

    3/13

    2. Writing our class with our custom code

    We will be using the WSS API and we will be writing a text file, so we need to write these two directives

    using System.IO;

    using Microsoft.SharePoint;

    Rename your class name to ItemEventReceiver and inherit from SPItemEventReceiver as shown

    below:

    Declare a StreamWriter object:

    Now we will write the method we want to override so we can run our own custom code. In this case,

    the event we want to trap is the ItemAdded event (after someone adds an item).

    The ItemAdded method is the one that will be called and the properties parameter contains detailed

    information about the file that fired this event.

  • 8/7/2019 creating sharepoint features

    4/13

    Our intent is that a text file is written to our desktop and we will write to it with data coming from the

    Properties parameter.

    Finish your class as shown below.

    ItemEventReceiver.cs code

    using System;using System.Collections.Generic;

    using System.Text;

    using System.IO;

    using Microsoft.SharePoint;

    namespace SharePointEventHandler

    {

    public class ItemEventReceiver : SPItemEventReceiver

    {

    private StreamWriter SW;

    public override void ItemAdded(SPItemEventProperties properties)

    {

    base.ItemAdded(properties);

    try

    {

    //Set this path below to suit your needs...

    SW=File.CreateText(@"[...]\Desktop\eventoutput.txt");

    SW.WriteLine("CurrentUserId: " + properties.CurrentUserId.ToString());

    SW.WriteLine("BeforeUrl: " + properties.BeforeUrl.ToString());

    SW.WriteLine("SiteId: " + properties.SiteId.ToString());SW.WriteLine("ListId: " + properties.ListId.ToString());

    SW.WriteLine("ListItem: " + properties.ListItem.ToString());

    SW.WriteLine("ListTitle: " + properties.ListTitle.ToString());

    SW.WriteLine("ReceiverData: " + properties.ReceiverData.ToStr ing());

    SW.WriteLine("WebUrl: " + properties.ToString());

    SW.Close();

    }

    catch (Exception ex)

    {

    properties.Cancel = true;properties.ErrorMessage = ex.Message;

    throw ex;

    }

    finally

    {

    this.SW.Dispose();

    }

  • 8/7/2019 creating sharepoint features

    5/13

  • 8/7/2019 creating sharepoint features

    6/13

  • 8/7/2019 creating sharepoint features

    7/13

    In this SharePointEventHandler folder create the following two blank xml files:

    Open the feature.xml file and replace any code with the following:

    We will need to fill in a valid GUID for the Id (left intentionally blank).

    In VS2005, click on Tools > Create GUID

  • 8/7/2019 creating sharepoint features

    8/13

    Click on Copy to copy the string and place it in the Id attribute value in your feature.xml file, leaving

    out the curly brackets!

    feature.xml code

    * I used an image (ImageUrl) that already existed for the icon that displays in the feature list in

    SharePoint. If you want to use your own, simply include the image in the IMAGES foler and point to it.

    Now, open the elements.xml file and replace any code with the following

    elements.xml code

    SharePointEventHandler

    ItemAdded

    1000

  • 8/7/2019 creating sharepoint features

    9/13

  • 8/7/2019 creating sharepoint features

    10/13

    Basically what we are doing is compiling our code with a strong name, installing it into the GAC, then

    we copy the folderstructure into the 12 Hive, then we install our feature and finally reset IIS.

    Before you build, ensure that SharePoint is working and navigate to the Site Features page. (Home>

    Site Settings > Site Features)

    Now, in Visual Studio 2005, build your solution (this may take a few seconds...). Ensure you have the

    Output (View > Output) window open when you compile so you can monitor step-by-step what is

    happening in the background and if you have any errors.

    Once the build is successful, refresh yoru browser window and it should look like this:

    Click on Activate to activate your feature and you are ready to go!

    6. Testing our feature

    Ok, let's test our feature... Open up a browser and go to your home page in SharePoint.

  • 8/7/2019 creating sharepoint features

    11/13

    create a new Document Library by clicking on Documents (left hand side) and then select Create.

    Now, select Document Library.

    Type in a name, for instance, Feature Site Test and select Word Document as the document type

    (or any other type...).

    And there is your Document Library.

  • 8/7/2019 creating sharepoint features

    12/13

    Now, click on New and because we selected the Document type to be Word, it will launch Microsoft

    Word. Type in anything in this document and close. It will prompt you to save it.

    SelectYes and give it any name and Save.

    This will add the document to the Document Library and it this event is trapped by SharePoint thatnow redirects it to our custom code, which creates a text file for us on the desktop (shown below on

    the desktop).

  • 8/7/2019 creating sharepoint features

    13/13

    When we open the text file, we will see it contains specific data about the document that fired this

    event.

    Enjoy!

    Skills: C#, ASP.NET, SharePoint 2007.