How to: Export an ASP.NET Web Part and Import it to a Windows SharePoint Services Site

You can reuse ASP.NET Web Parts in your Windows SharePoint Services 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 Windows SharePoint Services application, and the Web Part is displayed with the Windows SharePoint Services look and feel.

To export an ASP.NET Web Part

  1. 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;
  2. Modify the <system.web> section of the web.config file as follows:

    <system.web>
       <webparts  enableExport="true" />
    <sytem.web>          
  3. From the Web Part menu, choose Export to create a .webpart file you can import into a Windows SharePoint Services Web Part Page.

To import a .webpart file into Windows SharePoint Services

  1. Place the assembly for your Web Part in the bin or the global assembly cache.

    If you place your assembly in the global assembly cache, your assembly must be strong named and 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).

  2. Add the Web Part to the Safe Controls list in your web.config file, for example:

    <SafeControl 
       Assembly="MyWebPart"
       Namespace="MyWebParts"
       TypeName="*" 
       Safe="True"
    />
  3. In Design mode, select Add a web part. At the bottom of the Add Web Parts dialog box, click Advanced Web Part gallery and options.

  4. On the Add Web Parts pane, select Import and then navigate to the .webpart file you created in the preceding procedure.

  5. Click Upload and your Web Part appears in the list of Uploaded Web Parts.

  6. Drag it into a Web Part zone on the page and you should see the same Web Part as on your ASP.NET page, only this time it has standard Windows SharePoint Services chrome.

See Also

Tags : export webpart


Community Content

Stanley Roark
Export tip

Known issue:

When exporting DataFormWebPart, check the ListName property in web part definition. Sometimes it have to be removed, otherwise "Unknown error" may occur when using web part import from SharePoint web UI. At the same time such definition can be imported without errors using SharePoint Designer. To reproduce behavior:

1. Create DataFormWebPart displaying list of Pages in publishing site;

2. Export web part definition using SharePoint web UI;

3. Import web part definition file using Sharepoint web UI. At this step error occurs.

Workaround:

1. Open web part definition file;

2. Remove ListName web part property;

3. Save the file.

4. Import web part definition file using Sharepoint web UI. Web part is imported without errors.


Alexi Husky
Importing a web-part to sharepoint using code?
I have a .webpart xml file that was exported from the web interface.

I want to import this .webpart file into a specific zone on a specific page.

I.e.
Page = Default.aspx
Zone = TopZone

Is there any method that takes a location to the file and performs the import?

If there isn't, why not?
Tags :

Page view tracker