Export (0) Print
Expand All

Walkthrough: Create a Basic Site Definition Project

This walkthrough shows you how to create a basic site definition that contains a visual Web part with some controls on it. For the sake of clarity, the visual Web part that you create has only a few controls. However, you can create more sophisticated SharePoint site definitions that include more functionality. For more information, see Walkthrough: Creating a Site Definition with Additional Content.

This walkthrough demonstrates the following tasks:

  • Creating a site definition by using the Visual Studio project template.

  • Creating a SharePoint site by using a site definition in SharePoint.

  • Adding a visual Web part to the solution.

  • Customizing the site's default.aspx page by adding the new visual Web part to it.

NoteNote

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

You need the following components to complete this walkthrough:

  • Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.

  • Visual Studio 2010.

First, create the site definition project in Visual Studio.

To create a site definition project

  1. Display the New Project dialog box by pointing to New on the File menu, and then clicking Project.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, then SharePoint, and then click 2010.

  3. In the Templates pane, select Site Definition.

  4. In the Name box, type TestSiteDef and then click OK.

    The SharePoint Customization Wizard appears.

  5. On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site where you want to debug the site definition, or use the default location (http://system name/).

  6. In the What is the trust level for this SharePoint solution? section, take the default value of Deploy as a farm solution.

    All site definition projects must be deployed as farm solutions. For more information about sandboxed solutions versus farm solutions, see Sandboxed Solution Considerations.

  7. Click Finish. The project appears in Solution Explorer.

Next, create a visual Web part to display on the site definition's main page.

To create a visual Web part

  1. Click Add New Item on the Project menu to display the Add New Item dialog box.

  2. Click the Show All Files button in Solution Explorer so that all files appear.

  3. In the Installed Templates tree view, select either the Visual Basic or Visual C# node, then SharePoint, then 2010, and then click Visual Web Part in the list of project item templates. Use the default name VisualWebPart1.

    This opens the file VisualWebPart1UserControl.ascx.

  4. At the bottom of VisualWebPart1UserControl.ascx, add the following markup to add three controls to the form: a text box, a button, and a label:

    <table>
      <tr>
        <td>
          <asp:TextBox runat="server" ID="tbName"></asp:TextBox>
        </td>
        <td>
          <asp:Button runat="server" ID="btnSubmit" Text = "Change Label Text" OnClick="btnSubmit_Click"></asp:Button>
        </td>
        <td>
          <asp:Label runat="server" ID="lblName"></asp:Label>
        </td>
      </tr>
    </table>
    
  5. Open the file VisualWebPart1UserControl.ascx.cs (for Visual C#) or VisualWebPart1UserControl.ascx.vb (for Visual Basic) located under VisualWebPart1UserControl.ascx, and add the following code:

    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {            
        lblName.Text = tbName.Text;
    }
    
    
    

    This code adds functionality for the web part's button click.

Next, add the visual Web part to the site definition's default ASPX page.

To add a visual Web part to the default ASPX page

  1. Open the default.aspx page and add the following under the WebPartPages tag:

    <%@ Register Tagprefix="MyWebPartControls" Namespace="TestSiteDef.SiteDefinition.VisualWebPart1" Assembly="$SharePoint.Project.AssemblyFullName$" %>
    

    This line associates the name MyWebPartControls with the Web part and its code. The Namespace parameter is the same as the namespace used in the VisualWebPart1Usercontrol.ascx code file.

  2. After the </asp:Content> element, replace the entire ContentPlaceHolderId="PlaceHolderMain" section and its contents with the following:

    <asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
        <MyWebPartControls:VisualWebPart1 runat="server" />    
    </asp:Content>
    

    This code creates a reference to the visual Web part that you created earlier.

Next, run the project and deploy it to SharePoint.

To run and deploy the site definition

  • Press F5. Visual Studio compiles the code, adds its features, packages all of the files into a WSP file, and deploys the WSP file to SharePoint Server. SharePoint then installs the files and then activates the features.

Next, create a site by using the new site definition.

To create a site by using the site definition

  1. On the SharePoint site, the New SharePoint Site page appears.

  2. In the Title and Description section, enter My New Site for the title and a description of the site.

  3. In the Web Site Address section, enter mynewsite in the URL name box.

  4. In the Template section, click the SharePoint Customizations tab, then select TestSiteDef in the Select a template list.

  5. Leave the other settings at their default value and then click Create.

    The new site appears.

Next, test the new site to make sure that it works correctly.

To test the new site

  • In the text box on the default ASPX page, enter some text and then click the small, square button next to the text box.

    The text appears in the label to the right of the button.

Community Additions

ADD
Show:
© 2014 Microsoft