Inspired by the latest SharePoint PodShow topic; The SharePoint Data View Web Part-Episode 31 and Laura Rogers post on building a permissions dashboard I just had to write this post. In the last paragraph Laura quickly mentions how you can reuse the created Data View Web Part. I think this is really important to know how to export and import this Web Part. What I don't want people to do is to fire up SharePoint Designer and create these Data View Web Parts in the production environment so that the pages get's unghosted or customized. Also when you are working with solutions packages and features to deploy your applications in environments it's necessary to know how to do this.
Once you have created and finalized your Data View Web Part in your development environment or sandbox, then you can export your customizations. This is the same for all kinds of Web Parts, as long as they allow exporting. When you export a Web Part through the web interface you will get a .webpart file (read earlier published post on this), containing all the properties and metadata specified for the Web Part.
Once you saved it you will have a nnn.webpart file on your disk, which essentially is an XML file with the metadata.
Note: Your exported Web Part will contain the exact properties as you specified in your original location. For example if you connected to it to a list this list id will not be present in the location where you import it. So before importing it you might have to change some values in the .webpart file or at least edit the properties of the Web Part after uploading it.
Importing to the Web Part Gallery
If you have created a Data View Web Part that you will reuse many times throughout a Site Collection it might be worth uploading it to the Web Part Gallery. Just go to the Site Settings and then the Web Part Gallery and select Upload and upload your .webpart file. After this your Web Part is available for all authors to use.
Importing to a page
Importing the Web Part is not that difficult, though it can be hard to find where to do it. To import the Web Part, go to the page where you want to insert it and edit the page and then choose to add a new Web Part. Then you have to select Advanced Web Part gallery options, located in the lower right corner. When the page is re-loaded click on the Browse header and choose Import.
After that you can click on Browse to select your .webpart file and finally click Upload. Once the upload is done the Web Part will appear and you can drag and drop it onto your Web Part page.
Once it is on the page you may have to edit the properties of the Data View Web Part. You might have to change the data sources, if you for instance configured your Data View Web Part to work with a certain Site id or a List id.
Using the Web Part in features
You can also reuse this exported .webpart file in your SharePoint features and have them added to the Web Part gallery when activating the feature.
Another scenario, which I've used to build dashboards and dashboard sites is that you use the SPLimitedWebPartManager to create dashboards on feature activations or certain events. The SPLimitedWebPartManager has an ImportWebPart method that takes an XmlReader as input, which essentially is your .webpart file.
I hope you got something interesting out of this and if you haven't listened to the SharePoint PodShow about the Data View Web Part - do that immediately and understand the power of it!