Wednesday, June 20, 2007

Importing and exporting webparts in SharePoint 2007 using the object model

I have been working on a little utility to move webparts from one site to another using the ImportWebPart and ExportWebPart methods of the SPLimitedWebManager class. Here are already some code snippets which give you an idea what I'm doing.... I'm also going to incorporate some ideas which I found in this blog posting - Customizing MOSS 2007 My Sites within the Enterprise.

SPSite sitecollection = new SPSite(ConfigurationManager.AppSettings["ServerUrl"]);
SPWeb site = sitecollection.OpenWeb("/");

SPLimitedWebPartManager mgr = GetWebPartManager(args[5].ToString(),site);
sTargetDir = args[3].ToString();

switch (args[1].ToString())
  case "import":
    Console.WriteLine("Import operation started ....");
  case "export":
    Console.WriteLine("Export operation started ....");


SPLimitedWebPartManager is one of the key classes to use if you want to manipulate webparts in SharePoint Server 2007 or WSS 3.0. (Remember that you should probably add in some error trapping...)

static SPLimitedWebPartManager GetWebPartManager(string sUrl,SPWeb site)
     SPLimitedWebPartManager mgr = null;
     SPFile file = site.GetFile(sUrl);
     mgr = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
      return mgr;

static void ExportAllWebParts(SPLimitedWebPartManager mgr,string targetdir,WPEDataset ds)
XmlWriter writer = null;

foreach (WebPart wp in mgr.WebParts)
//Always do a flush and close of the xmltextwriter
writer = new XmlTextWriter(Path.Combine(targetdir,wp.Title)+ ".xml", Encoding.UTF8);
Console.WriteLine("Export" + wp.Title + " in zone " + wp.ZoneID);
mgr.ExportWebPart(wp, writer);

static void ImportAllWebParts(string sDirectory, SPLimitedWebPartManager mgr, string sWPZone)
string[] filenames;
string sfilename = "";
string sWPTitle = "";
filenames = Directory.GetFiles(sDirectory);
string sError = "";
System.Web.UI.WebControls.WebParts.WebPart wp = null;
XmlReader reader = null;
int i=0;

IEnumerator en = filenames.GetEnumerator();
while (en.MoveNext())
sfilename = en.Current.ToString();
sWPTitle = sfilename.Replace(".xml", "");
sWPTitle = sWPTitle.Replace(sDirectory + @"\","");
reader = new XmlTextReader(sfilename);
wp = mgr.ImportWebPart(reader, out sError);
mgr.AddWebPart(wp, sWPZone,i);


Servé Hermans said...

Hi Joris,
When I use the SPLimitedWebPartManager to loop through all the webparts on a page, sometimes I get the ErrorWebPart type returned. Further investigation showed me that it happens for our custom webparts. The custom webparts however are working. Do you know why custom webparts appear as errorwebpart types (using wp.GetType())? It drives me crazy.

Chris White said...

I get the same issue - custom web parts (in this case, not really custom as the web parts I'm using are the SQL Report Viewer which comes as part of the SQL Reporting Add-in) are returned as ErrorWebPart so I'm unable to use them even though the web parts are installed and working fine.

swastik nath said...

Hi Joris,
i have tried the code.i am facing a typical error. it says
"the type or namespace name "WPEDataset" cound not be found"
and its asking if we forgot to give any directive.
could you assist me in this issue.
i need the solution ASAP.
thanks in advance.

jopx said...

WPEDataset is the name of the typed dataset which I created to temporary store data about the exported/imported webparts. You should add this to your solution as well.

swastik nath said...

can you please guide me in detail on how to do that. i have tried adding a new item--dataset and named it "WPEDataset" but that is not solving the issue.
please assist me with this.
thanks in advance.

jopx said...

Try and change your code to whatever typed name you have for your dataset...