If you’ve got an existing page in each environment e.g. default.aspx and want to add a web part and promote to each environment what is the best approach?

  1. Create a WSP with a Feature Receiver and add to page via activation
  2. Create a WSP with a Feature and add a module with AllUserWebParts (requires deploying aspx page with it though)
  3. Manual export and import web part
  4. PowerShell script using SPLimitedWebPartManager
  5. Other…

You might find the SP2010 Client Object Model usefule here. Export the Web Part to a Web Part Control Description file (.dwp or .webpart) and then flip it in like this:

using (ClientContext context = new ClientContext("http://server")) {

    Web web = context.Web;
    File file = web.GetFileByServerRelativeUrl("/SitePages/Default.aspx");
    LimitedWebPartManager wpMgr = file.GetLimitedWebPartManager(

    WebPartDefinition webPartDef = wpMgr.ImportWebPart(  
    wpMgr.AddWebPart(webPartDef.WebPart, "Left", 1);

  • I really wanted to wrap this up in a feature, in this instance it wouldn't make sense to use ClientContext in a server side Feature Activation right? So I'd just use SPLimitedWebPartManager...
    – Anonymous
    Apr 28 '10 at 1:23
  • Thats correct,but if you would like to make it scriptable it's easy to cook this into an .exe that takes url, page and webpart as args. Apr 28 '10 at 6:42

In the "Other" category, simply copy and paste the code from the page in one environment to the other in SharePoint Designer. It all depends on the business requirements, doesn't it?

  • Yes I guess, but if I'm deploying changes every day for the next three months, copying and pasting might make me go crazy ;-)
    – Anonymous
    Apr 28 '10 at 1:21
  • Agreed, but you didn't mention "every day for the next three months" in your requirements. ;-) I would venture that packaging up everything for deployment every day for the next three months may not be much fun, either! Apr 28 '10 at 12:28
  • copy and paste does work to a basic degree...but if you have list id's hanging in there it'll fail :-( it's always fun mate!
    – Anonymous
    Apr 29 '10 at 5:28
  • Depends on what kind of Web Part, but for some switch ListIDs for ListNames, and copy and paste work just fine. Apr 29 '10 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy