How to: Deploy a Web Part Page in a Sandboxed Solution

Overview

Many Microsoft® SharePoint® applications require customized Web Part pages, either to provision additional Web Part zones or to provide alternative layouts. This how-to topic describes how to deploy a custom Web Part page in a sandboxed solution.

For a practical example of the deployment of a Web Part page to the sandbox environment, see the Sandbox Proxy Reference Implementation. The reference implementation displays a custom Web Part page as a modal dialog box.

Summary of Steps

This how-to topic includes the following steps:

  • Step 1: Create and Configure a List Instance. In this step, you create a feature manifest file that provisions a new document library in which to store your Web Part page.
  • Step 2: Create and Configure a Module. In this step, you create a feature manifest file that contains a Module element. This specifies which files to deploy and where to deploy them.
  • Step 3: Add the Module to a Feature. In this step, you create a feature and add your feature manifest files to the feature. This enables you to provision the list instance and deploy the module to the SharePoint environment.
Ff798497.note(en-us,PandP.10).gifNote:
This how-to topic assumes that you have used the Microsoft Visual Studio 2010 development system and one of the SharePoint 2010 templates to create a project. It also assumes that you have a custom Web Part page ready to deploy.

Step 1: Create and Configure a List Instance

This procedure creates a feature manifest that provisions a new document library named Pages on your SharePoint site. You will use this library to store your custom Web Part page.

Ff798497.note(en-us,PandP.10).gifNote:
If you are deploying a page to a site that has the Publishing feature activated, you do not need to complete step 1. Instead, you can deploy your page to the Pages library that is automatically created by the Publishing feature. However, if you are using SharePoint Foundation 2010, the Publishing feature is unavailable and you must manually create a Pages library as described in this step.

To create a list instance

  1. In Solution Explorer, right-click the project node, point to Add, and then click New Item.
  2. In the Add New Item dialog box, expand SharePoint in the Installed Templates pane, and then click 2010.
  3. Click List Instance, type a name for the list instance in the Name text box, and then click Add. This example uses the name PagesLibrary for the module.

    Ff798497.c9f9b2fd-f6a9-4b6f-9f52-4357d9d711cd(en-us,PandP.10).png

  4. In the SharePoint Customization Wizard, under Which list do you want instantiate? [sic], click Document Library.
  5. Provide appropriate values for the display name, description, and relative URL, and then click Finish.

    Ff798497.758a890b-0931-4b08-be0a-1eab7855553f(en-us,PandP.10).png

    Notice that Visual Studio creates and displays a feature manifest file based on the settings that you provided. Your feature manifest should resemble the following code.

    <Elements xmlns="http://schemas.microsoft.com/sharepoint"/>
      <ListInstance Title="Custom Pages"
                    OnQuickLaunch="TRUE"
                    TemplateType="101"
                    FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"
                    Url="Lists/CustomPages"
                    Description="">
      </ListInstance>
    </Elements>
    
    

Step 2: Create and Configure a Module

This procedure creates a feature manifest that contains a Module element. The module is configured to add files to the content database, which enables you to deploy it within a sandboxed solution.

To create a Module element

  1. In Solution Explorer, right-click the project node, point to Add, and then click New Item.
  2. In the Add New Item dialog box, expand SharePoint in the Installed Templates pane, and then click 2010.
  3. Click Module, type a name for the module in the Name text box, and then click Add. This example uses the name Pages for the module.

    Ff798497.36567638-35e1-480d-9d76-3130687f8653(en-us,PandP.10).png

    In Solution Explorer, notice that Visual Studio has added a Pages node to represent your module.

    Ff798497.32ee6fba-8417-4939-a6b2-0065b278dc73(en-us,PandP.10).png

  4. Expand the Pages node and open the feature manifest file (Elements.xml). By default, the feature manifest includes a Module element with a placeholder File child element, as shown in the following code.
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="Pages">    <File Path="Pages\Sample.txt" Url="Pages/Sample.txt" />
      </Module>
    </Elements>
    
    
  5. In the Elements.xml file, delete the placeholder File element. You can also delete the Sample.txt file in Solution Explorer.
  6. In Solution Explorer, right-click the Pages node, point to Add, and then click Existing Item.
  7. Browse to the Web Part page that you want to deploy, and then click Add.
  8. Notice that Visual Studio adds a File element for the Web Part page, as shown in the following code. This example uses a Web Part page named SimplePage.aspx.
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="Pages">    <File Path="Pages\SimplePage.aspx" Url="Pages/SimplePage.aspx" />    
      </Module>
    </Elements>
    
    
  9. In the Module element, add a Url attribute to specify the destination for the files. This should be the relative URL of the target list instance, which in this case is Lists/CustomPages.
  10. In the Module element, add a List attribute to indicate the type of list to which you are deploying your files. Because this example deploys files to a document library, the attribute value should be List="101".
  11. Make the following changes to the File element:
    1. Leave the Path attribute value as Pages\SimplePage.aspx. This tells the feature where to find the file within your Visual Studio project.
    2. Change the Url attribute value to SimplePage.aspx. This specifies the virtual path to the file, within the virtual directory specified by the Module element.
    3. Add a Type="GhostableInLibrary" attribute value. This indicates that the file will be stored in the content database and that it should support library operations, such as check-in and check-out.
    Ff798497.note(en-us,PandP.10).gifNote:
    Visual Studio 2010 does not always automatically pick up the feature manifest schema. If you see schema errors or you lack Microsoft IntelliSense® support when you edit a feature manifest, check the properties of the XML document in the Properties window. The Schemas property should be set to 14\TEMPLATE\XML\wss.xsd.

  12. The feature manifest should resemble the following.
     <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="Pages" Url="Lists/CustomPages" List="101">    <File Path="Pages\SimplePage.aspx" 
              Url="SimplePage.aspx" 
              Type="GhostableInLibrary" />    
      </Module>
    </Elements>
    
    

Step 3: Add the List Instance and the Module to a Feature

This procedure adds the list instance and module to a feature, which provides the mechanism to provision your Pages library and deploy your files to the SharePoint environment.

To add items to a feature

  1. In Solution Explorer, right-click the Features node, and then click Add Feature.
    Ff798497.note(en-us,PandP.10).gifNote:
    Visual Studio 2010 may have already added an empty feature when you added other SharePoint components. In this case, you can either rename the empty feature or delete it and create a new one.

  2. In Solution Explorer, right-click the new feature node, and then click Rename. This example uses the name PagesFeature for the feature.
  3. If the Feature Designer is not already open, double-click the PagesFeature node to open the designer.
  4. In the Feature Designer, select an appropriate scope. You can use a feature scope value of Web or Site in a sandboxed solution.
  5. Use the arrow buttons to add both items to the feature.

    Ff798497.3b533c1d-aaa3-40ab-9891-796b454dcf6e(en-us,PandP.10).png

  6. To deploy the feature, right-click the project node in Solution Explorer, and then click Deploy.
  7. You can verify the deployment by browsing to the Custom Pages library on your test site. The library should contain your Web Part page.

For more information about provisioning files into SharePoint sites, see How to: Provision a File and How to: Include Files by Using a Module on MSDN.



Show: