How to: Export an ASP.NET Web Part and Import it to a SharePoint Foundation Site
Last modified: April 09, 2010
Applies to: SharePoint Foundation 2010
You can reuse Microsoft ASP.NET Web Parts in your Microsoft SharePoint Foundation application by exporting them to create .webpart files, which are XML files that contain property values, state data, and assembly or source file details about your Web Part. Because export functionality is not available by default, you must make some modifications to your Web Part code and your configuration settings.
After you have a .webpart file, you can import it into any SharePoint Foundation application, and the Web Part is displayed with the SharePoint Foundation look and feel.
To export an ASP.NET Web Part
In your Web Part code, set the ExportMode property to allow properties to be exported. In the following code, we set the value to All, which allows sensitive properties to be exported.
this.ExportMode = WebPartExportMode.All;
Modify the <system.web> section of the web.config file as follows:
<system.web> <webparts enableExport="true" /> <sytem.web>
From the Web Part menu, choose Export to create a .webpart file you can import into a SharePoint Foundation site page.
To import a .webpart file
Place the assembly for your Web Part in the Solution Gallery, bin, or global assembly cache.
If you place your assembly in the Solution Gallery, your assembly must be packaged as a sandboxed solution and will not have full trust permissions. For more information on sandboxed solutions, see Sandboxed Solutions in SharePoint 2010.
If you place your assembly in the global assembly cache, your assembly must be strong named and will run with full trust code permissions by default. The Web Part is available to all Web applications.
If you place your assembly in the bin, you do not have full trust code permissions when your Web Part executes. Because the permissions for the bin directory are very low by default, you may have to create a new trust policy file for your Web Part (recommended), or raise the trust level in the web.config file (the default is WSS_Minimal). For information on creating a CAS seucrity policy, see Securing Web Parts in SharePoint Foundation.
Add the Web Part to the Safe Controls list in your web.config file, for example:
<SafeControl Assembly="MyWebPart" Namespace="MyWebParts" TypeName="*" Safe="True" />
Navigate to the Web Part Gallery. On the Documents tab of the Library Tools contextual group in the Server ribbon, click the Upload Document button.
In the Upload Web Part dialog, click Browse and navigate to the .webpart file you created in the preceding procedure. Click OK. Your Web Part appears in the list of Web Parts.
Navigate to the page were you want your Web Part to appear. Click the Edit button on the Page tab in the Server ribbon to put the page into edit mode.
On the Insert tab inside the Editing Tools contextual group, click the Web Part button.
In the Categories list, find the Web Part category and choose the Web Part in the list. Click Add. The Web Part will appear in the page as on your ASP.NET page, only this time it has standard SharePoint Foundation chrome.