Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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…
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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(
        PersonalizationScope.Shared);

    WebPartDefinition webPartDef = wpMgr.ImportWebPart(  
        System.IO.File.ReadAllText("Content_Editor.dwp"));
    wpMgr.AddWebPart(webPartDef.WebPart, "Left", 1);

    context.ExecuteQuery();
}
share|improve this answer
    
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. –  Wictor Wilen MCA MCM MVP Apr 28 '10 at 6:42
    
that's very true, thanks mate ;-) –  Anonymous Apr 29 '10 at 5:27

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?

share|improve this answer
    
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! –  Marc D Anderson 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. –  Marc D Anderson Apr 29 '10 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.