none
Cloning/Moving webpart pages with existing webparts

    Pertanyaan

  • Hi All,       

            We need to support clone/move of the webpart pages with pre populated webparts. We have our custom webpart page layout. Users can create webpart page using our custom page layout and can add webparts init. On cloning of the webpart page we just read the file(webpart page) from the document libary and saving it with different name in the same document library. But the cloned page does not retain the webparts added to it. The user can view only the empty webpart page layout.

    Please suggest me how to move/clone the webpart page with the existing webparts.

    Thanks,

    R.Prakash

    02 Desember 2010 7:22

Jawaban

  • The web part are not saved in the file. You can look in the file to see (/Pages/Forms/AllItems.aspx -> send to -> download a copy). You should only have a few lines of ASPX code that reference the page layout.

    If you detach the page from the page layout, you should have basically the same code as your page layout in the file. No web parts will be in the file.

    If you only copy the file, you will not retain the web parts. I guess it should be doable (if the web parts are exportable, using SPLImitedWebPartManager) and doing export/import ...

    PS: why don't you use content migration API to save the export package to a library (maybe you don't even need to physically save it) then reimport it?


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    02 Desember 2010 19:13
  • Hi Florin,

              Thanks a lot for your reply. I though that the webparts are stored in the file itself.

              I am planning to clone the Webpart page by using the following approach.

    1. Use SPContext.Current.Web.GetLimitedWebPartManager("Pages/Source.aspx", PersonalizationScope.Shared)) to get the source Webpart page.
    2. Use SPLimitedWebPartManager.ExportWebPart() to export webparts from source webpart page layout into xml.
    3. Use SPContext.Current.Web.GetLimitedWebPartManager("Pages/Destination.aspx", PersonalizationScope.Shared)) to get the destination Webpart page.
    4. Use SPLimitedWebPartManager.ImportWebPart() to import System.Web.UI.WebControls.WebParts.WebPart from xml
    5. Use SPLimitedWebPartManager.AddWebPart() to add the webpart into the destination webpart page.

    Please provide your thoughts whether this approach will work.

    Thanks,

    R.Prakash

     

    03 Desember 2010 13:06
  • I suppose it should work however in some cases some webparts are not "moveable" (in another subsite for example). For instance a list view.

    Also some web parts can be marked as exportable and some web parts can have properties that do not get exported. Yes this is not very common but know that it can comme in some situations.


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    03 Desember 2010 15:45
  • Hi Florin,

                Thanks a lot for your help.

                Based on your suggestion we completed the cloning of the WebPart pages.

    Code snippet:

    SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {

                        using (SPWeb web = site.OpenWeb())
                        {

                            SPLimitedWebPartCollection lstWebPart = null;

                            web.AllowUnsafeUpdates = true;

                            using (SPLimitedWebPartManager webPartManager =
                                        SPContext.Current.Web.GetLimitedWebPartManager("Pages/BaseWebPartPage.aspx,  PersonalizationScope.Shared))
                            {
                                lstWebPart = webPartManager.WebParts;
                            }
                          

                            using (SPLimitedWebPartManager webPartManager =
                                         SPContext.Current.Web.GetLimitedWebPartManager("Pages/ClonedWebPartPage.aspx, PersonalizationScope.Shared))
                            {

                               
                                foreach (WebPart webPart in lstWebPart)
                                {
                                   
                                   
        webPartManager.AddWebPart(webPart, webPart.Zone.Id, webPart.ZoneIndex);
                                   
                                    web.Update();

                                }

                            }

                            web.AllowUnsafeUpdates = false;


                        }
                    }
                });

     

    Thanks,

    R.Prakash

    06 Desember 2010 8:04

Semua Balasan

  • you can not move/clone webpart page with existing webparts, I think in sharepoint only content deployment can do this,but in this scenario that will not help

    Either you need to write code to place the webparts on webpart page dynamically using event handlers or you have to use <alluserwebpart > elements with features, what it does dynamically this will add webparts while creating pages using page layouts

    http://www.andrewconnell.com/blog/archive/2007/10/07/Having-Default-Web-Parts-in-new-Pages-Based-Off-Page.aspx

     


    Raj. CodePlex : http://pagelayoutgenerator.codeplex.com Blog : http://mspbox.blogspot.com
    02 Desember 2010 8:02
  • hello

    you can try Web Part Page Cloner free tool - according to its description it does what you want. If you need to do it programmatically you can investigate the code of this tool using Reflector in order to use the same approaches in your custom code. Also as far as I remember Sharepoint Designer supports copying of web parts when you copy publishing page in it - so try it also.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    Graphs visualization in Sharepoint - http://spgraphviz.codeplex.com
    02 Desember 2010 8:26
  • You could also try using SharePoint Deployment Wizard (http://spdeploymentwizard.codeplex.com/). It has a nice GUI with lots of options, it also exports related content and it is based on PRIME (the SharePoint content migration API, the one that is also used internally for content deployment).
    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    02 Desember 2010 8:53
  • Hi All,

    We use the following code to clone the webpart page.

    SPListItem item = documentLibrary.GetItemById(id);

    documentLibrary.RootFolder.Files.Add("Title", item.File.OpenBinary());

    but it is not working.

    Are the webparts in the webpart page layout stored in different place?

    Is there anyway to read the webpart page with its content and save it back with different name?

    Please help me.

    To Sadomovalex,

             I tried to access the link mentioned by you, but i could not.

    Thanks,

    R.Prakash

     

    02 Desember 2010 9:58
  • The web part are not saved in the file. You can look in the file to see (/Pages/Forms/AllItems.aspx -> send to -> download a copy). You should only have a few lines of ASPX code that reference the page layout.

    If you detach the page from the page layout, you should have basically the same code as your page layout in the file. No web parts will be in the file.

    If you only copy the file, you will not retain the web parts. I guess it should be doable (if the web parts are exportable, using SPLImitedWebPartManager) and doing export/import ...

    PS: why don't you use content migration API to save the export package to a library (maybe you don't even need to physically save it) then reimport it?


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    02 Desember 2010 19:13
  • Hi Florin,

              Thanks a lot for your reply. I though that the webparts are stored in the file itself.

              I am planning to clone the Webpart page by using the following approach.

    1. Use SPContext.Current.Web.GetLimitedWebPartManager("Pages/Source.aspx", PersonalizationScope.Shared)) to get the source Webpart page.
    2. Use SPLimitedWebPartManager.ExportWebPart() to export webparts from source webpart page layout into xml.
    3. Use SPContext.Current.Web.GetLimitedWebPartManager("Pages/Destination.aspx", PersonalizationScope.Shared)) to get the destination Webpart page.
    4. Use SPLimitedWebPartManager.ImportWebPart() to import System.Web.UI.WebControls.WebParts.WebPart from xml
    5. Use SPLimitedWebPartManager.AddWebPart() to add the webpart into the destination webpart page.

    Please provide your thoughts whether this approach will work.

    Thanks,

    R.Prakash

     

    03 Desember 2010 13:06
  • I suppose it should work however in some cases some webparts are not "moveable" (in another subsite for example). For instance a list view.

    Also some web parts can be marked as exportable and some web parts can have properties that do not get exported. Yes this is not very common but know that it can comme in some situations.


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    03 Desember 2010 15:45
  • Hi Florin,

                Thanks a lot for your help.

                Based on your suggestion we completed the cloning of the WebPart pages.

    Code snippet:

    SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {

                        using (SPWeb web = site.OpenWeb())
                        {

                            SPLimitedWebPartCollection lstWebPart = null;

                            web.AllowUnsafeUpdates = true;

                            using (SPLimitedWebPartManager webPartManager =
                                        SPContext.Current.Web.GetLimitedWebPartManager("Pages/BaseWebPartPage.aspx,  PersonalizationScope.Shared))
                            {
                                lstWebPart = webPartManager.WebParts;
                            }
                          

                            using (SPLimitedWebPartManager webPartManager =
                                         SPContext.Current.Web.GetLimitedWebPartManager("Pages/ClonedWebPartPage.aspx, PersonalizationScope.Shared))
                            {

                               
                                foreach (WebPart webPart in lstWebPart)
                                {
                                   
                                   
        webPartManager.AddWebPart(webPart, webPart.Zone.Id, webPart.ZoneIndex);
                                   
                                    web.Update();

                                }

                            }

                            web.AllowUnsafeUpdates = false;


                        }
                    }
                });

     

    Thanks,

    R.Prakash

    06 Desember 2010 8:04
  • Hi all,

    I had the same issue, I needed to provide a way to clone webpart pages from a template (Webpart page with content). I have been able to solve it as follows:

    1.     Create your webpart page with the webparts you need.

    2.     Open Microsoft Frontpage 2003 or Microsoft Visual Studio (I have not checked with other applications but maybe it could work with dreamweaver).

    3.     Save as… new_name.aspx. This file contains all the webparts and content.

    4.     Upload it to the desired document library.

    5.     Now, you can copy and paste this file from "Site Content and Structure Manager" or from "Windows Explorer".

    I hope this helps.

    09 Desember 2010 13:57
  • I realize this is a really old thread, but I wanted to respond for those who might be looking for an OOB method for copying pages with web parts included. By the way, this is is in SharePoint 2010 and for pages that you want to keep within the same site. Here is what I did:

    1) Site Actions / Manage Content and Structure
    2) Check-mark the page you want to copy
    3) Actions / Copy
    4) Copy to one of the Documents sub-folders
    5) Navigate to that folder
    6) Check out the page
    7) Use the drop-down for that page and choose Edit Properties
    8) Rename the page, update any other information, and click Save
    9) Check it back in
    10) Check-mark, then Actions / Move
    11) Choose the Pages folder
    12) Then open the page and you should have an exact copy under the new page name

    Hopefully this helps.

    10 Juli 2012 11:58
  • Thanks, Sings4Fun.  Your method worked like a charm for me!
    12 Oktober 2012 1:30
  • Hi All,

          If you want to clone a webpart page the best way is using SPFile.CopyTo(newUrl). For example you have a site page in "Shared Documents" document library and you have added some webparts and created a connections between them. Now if you want to create a clone of this page in your custom document library named "Custom DocLibrary" then use the below code,

          SPFile baseFile = spWeb.GetFile("Shared Documents/BasePage.aspx");

           baseFile.CopyTo("Custom DocLibrary/NewPage.aspx");

            Now if you visit the NewPage.aspx it has all the webparts with connections from the BasePage.aspx.

    Thanks,

    R.Prakash

    28 Nopember 2012 8:06
  • Hi R.Prakash:

    How exactly do you implement this SPFile.CopyTo code? create a new page and add it to the source? sorry for the newb-type question I'm just not sure how to roll this out on a new page.

    27 Februari 2014 20:19