Export (0) Print
Expand All
Expand Minimize

Adding SharePoint Project Items to SharePoint 2010 in Visual Studio 2010

SharePoint 2010

SharePoint Visual How To

Summary:  Learn how to add and work with SharePoint project items in Microsoft SharePoint 2010 to quickly build business solutions by using the SharePoint development tools in Microsoft Visual Studio 2010.

Last modified: September 12, 2012

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ted Pattison, Critical Path Training, LLC (SharePoint MVP)

Overview

The SharePoint development tools in Microsoft Visual Studio 2010 make it easier to create business solutions, by using SharePoint project items. This Microsoft SharePoint 2010 Visual How To demonstrates how to add SharePoint project items to create a new list with event handlers that enforce custom business rules.

Code It

Start by creating a new SharePoint site by using the Blank site template. You will use this site as a test site. Next, launch Microsoft Visual Studio 2010 and create a new SharePoint project named Movies by using the Empty SharePoint Project template. When you are prompted by the SharePoint Configuration Wizard, add the URL of a local test site and select Deploy as a sandboxed solution. Now, build the project using the following steps.

To build the SharePoint project and add SharePoint project items

  1. In Solution Explorer, right-click the Movies project node, click Add, and then click New Item. In the Add New Item dialog box, create a new List Instance project item named Movies. When you create a new list instance, you are prompted for information by the SharePoint Configuration Wizard. Give the new list a display name of Movies. Instantiate the new list as a Custom List. Provide a relative URL of Lists/Movies for the list, and then click Finish.

  2. In Solution Explorer, notice that there is a new project item named Movies and a new feature in the Features node named Feature1. Right-click the node for Feature1, and then rename this feature MainSite.

  3. Open MainSite in the Feature Designer. Change the Title of the feature to Movies and change the Scope of the feature from Web to Site. This change configures the feature to activate at the level of the site collection instead of at the site level.

  4. In Solution Explorer, select the new project item named Movies, and examine its properties in the Visual Studio property sheet. Also examine the Elements.xml file.

  5. Ensure that the Output Window is showing in Visual Studio 2010. If you cannot see the Output Window, on the Visual Studio 2010View menu, click Output. Or, use the keyboard shortcut CTRL+ALT+O.

  6. In Solution Explorer, right-click the Movies project node, and then execute the Deploy command. Watch the progress of the Deploy command in the Output Window.

  7. After the Deploy command completes, return to the test site in the browser and refresh the home page. After the page refreshes, you should see a link to the new Movies list in the Quick Launch list.

  8. In the browser, navigate to the Movies list, and then add a new list item.

  9. Return to Visual Studio 2010, and rerun the Deploy command. A Deployment Conflicts dialog box appears that informs you that the target test site already contains a list with a title of Movies. Click the Resolve Automatically button, which deletes the existing Movies list and re-creates a new one. After the Deploy command completes, return to the browser and refresh the page. Verify that the Movies list no longer contains the item that you created earlier. This is because the list was automatically deleted and re-created by the deployment conflict resolution capability that is built into the SharePoint development tools in Microsoft Visual Studio 2010.

  10. In Solution Explorer, select the new project item named Movies, and examine its properties in the Visual Studio property sheet. Locate the Deployment Conflict Resolution property and change its value to Automatic. Now rerun the Deploy command. The SharePoint development tools in Microsoft Visual Studio 2010 should now delete and then re-create the Movies list without prompting you each time that you run the Deploy command.

  11. Now, you will add a new project item for an event receiver. In Solution Explorer, right-click the Movies project node, select Add, and then click New Item. In the Add New Item dialog box, create a new Event Receiver project item named MoviesListEvents. The SharePoint Configuration Wizard prompts you for more information to create the new project item. Select List Item Events for the type of event receiver that you want to create. For the event source, select Custom List. For events to handle, select an item is being added and an item was added, and then click Finish.

  12. Open the Elements.xml file in the MoviesListEvents project item folder. Modify the opening Receiver tag by removing the ListTemplateId attribute and adding the ListUrl attribute, so that your event handler is bound to a single list instance instead of being bound to all the lists of a specific type.

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers ListUrl="Lists/Movies" >
    ...
    
    
  13. You must now update the Receiver element for the after event named ItemAdded, to make it a synchronous event. To do this, add a Synchronization element that contains a value of Asynchronous, as shown in the following example.

    <Receiver>
      <Name>MoviesEventListItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>Movies.MoviesEventList.MoviesEventList</Class>
      <SequenceNumber>10000</SequenceNumber>
      <Synchronization>Asynchronous</Synchronization>
    </Receiver>
    
    
  14. Now you can update your event handlers. Implement the ItemAdding event handler with a validation rule to prohibit the user from adding an ampersand (&) in movie titles, as shown in the following example.

    public override void ItemAdding(SPItemEventProperties properties) {
      string val = properties.AfterProperties["Title"].ToString();
      // Prohibit user from adding ampersand (&) in movie title.
      if(val.Contains("&")){
        properties.Status = SPEventReceiverStatus.CancelWithError;
        properties.Cancel = true;
        properties.ErrorMessage = "Ttitle cannot contain ampersand (&)";
      }
    }
    
    
  15. Implement the after event handler named ItemAdded to convert the text in every movie title to uppercase letters.

    public override void ItemAdded(SPItemEventProperties properties) {
      string val = properties.ListItem["Title"].ToString();
      properties.ListItem["Title"] = val.ToUpper();
      properties.ListItem.UpdateOverwriteVersion();
    }
    
    
  16. Test your work by pressing the F5 key. When the browser opens, play the part of a user and add a few new movie titles into the Movies list. Verify that you receive an error when you attempt to add a new movie title that contains an ampersand. Also, verify that all movie title text is converted to uppercase letters automatically.

See It

Watch the video

Watch video

Length: 00:18:41

Explore It

About the Author

MVP Contributor Ted Pattison is an author, instructor, and co-founder of Critical Path Training, a company dedicated to education about SharePoint technologies. As a Microsoft SharePoint Most Valuable Professional (MVP), Ted frequently works with the Microsoft Developer Platform Evangelism group to research and author SharePoint training material for developers early in the product life cycle, while the product is in its alpha and beta stages. Ted is also co-author of Inside Microsoft SharePoint 2010.

Read It

You can use several different types of SharePoint project templates to create SharePoint project items when you are creating business solutions by using the SharePoint development tools in Microsoft Visual Studio 2010. These are described in Table 1. For more information, see SharePoint Project and Project Item Templates.

Table 1. Project templates used to create SharePoint project items

Template Name

Description

Solution Type Supported

Visual Web Part

Web Part that relies on a user control to provide its user interface elements.

Farm only

Web Part

Standard Web Part that does not rely on a user control.

Sandboxed or Farm

Sequential Workflow

Workflow template for creating sequential workflows.

Farm only

State Machine Workflow

Workflow template for creating state machine workflows.

Farm only

Workflow Association Form

Application page used to accept user input to configure a workflow association.

Farm only

Workflow Initiation Form

Application page used to accept user input just before a workflow instance begins to execute.

Farm only

Business Data Connectivity Model

Component with a visual designer for creating a Microsoft .NET Connector to integrate external data sources into solutions that are based on Microsoft Business Connectivity Services (BCS) and SharePoint Search.

Farm only

Event Receiver

Class that has an event receiver and a feature element to wire it to a specific event.

Sandboxed or Farm

Module

Folder into which you add Elem,element files, which automatically maintains a Module element with an inner File element for each file.

Sandboxed or Farm

Content Type

Collaborative Application Markup Language (CAML) element for creating a SharePoint content type.

Sandboxed or Farm

List Definition from Content Type

CAML elements for creating a SharePoint List Definition from a specific content type.

Sandboxed or Farm

List Definition

CAML elements for creating a SharePoint List Definition from a specific list type.

Sandboxed or Farm

List Instance

CAML element for provisioning a list instance.

Sandboxed or Farm

Empty Element

Blank element manifest into which you add any CAML elements.

Sandboxed or Farm

User Control

An .ascx file and supporting code-beside file for creating a user interface by using a visual designer.

Farm only

Show:
© 2014 Microsoft