Exporting ListViewWebParts!

ListViewWebPart cannot be exported by design, that’s true! ListViewWebPart elements are tied to the underlying list and views by GUID, and the GUID is unique for each view and each list in each site.

But what if you need to export it to add it to an AllUsersWebPart element in an onet.xml file ?

The following code snippet will export any web part (even if it is a ListViewWebPart ) from the page specified.

SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(

“default.aspx”

, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

foreach(WebPart webpart in mgr.WebParts )
{
          string Filename = @"c:\webparts\"+ webpart.ID + ".dwp";

System.Xml.XmlTextWriter writer =
new System.Xml.XmlTextWriter(Filename,Encoding.Default);

          webpart.ExportMode = System.Web.UI.WebControls.
                                    WebParts.WebPartExportMode.All;
          mgr.ExportWebPart(webpart, writer);
          writer.Close();
}

		
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s