Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, October 5, 2012 12:08 PM
Hello,
I have a requirement where I need to copy the pages from one site to another. The issue I'm facing is that the web parts get copied to the correct zone in the destination page, but the position of the web part within that zone is not correct.
My code is below. Has anyone come across this issue? Our source page has web parts added and removed in it. Also, the zone index id of the web part is not set manually.
SPWebParts.SPLimitedWebPartManager _sourceWebpartManager = sourceWeb.GetLimitedWebPartManager(sourceUrl, PersonalizationScope.Shared);
SPWebParts.SPLimitedWebPartManager _destinationWebpartManager = destinationWeb.GetLimitedWebPartManager(destinationUrl, PersonalizationScope.Shared);
//deletes all the webparts on destination page
DeleteAllWebParts(_destinationWebpartManager);
foreach (WebPart wp in _sourceWebpartManager.WebParts)
{
string webpartZoneID = _sourceWebpartManager.GetZoneID(wp);
if (!string.IsNullOrEmpty(webpartZoneID))
{
_destinationWebpartManager.AddWebPart(wp, webpartZoneID, wp.ZoneIndex);
}
}
if (_sourceWebpartManager != null)
{
_sourceWebpartManager.Dispose();
}
if (_destinationWebpartManager != null)
{
_destinationWebpartManager.Dispose();
}
Any help on this is appreciated.
Thanks,
Neha
All replies (4)
Tuesday, October 9, 2012 12:12 AM âś…Answered
Hi Neha,
When you add a web part to a web part zone, the zoneindex for a given zone has to start at 0. What is happening is that you are adding the web parts out of order, not starting at the zoneindex of 0, because, as you have already mentioned, they are delivered to you out of order.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.webparts.webpart.zoneindex.aspx
My suggestion is to go through your source web parts first and create a collection that points to the different web parts in the _sourceWebpartManager.WebParts, recording their zoneid and their wp ZoneIndex. Now sort this collection. Finally run through this collection and add back your web parts to each zone in the proper order, starting at zoneindex of 0 for each webpartZoneID.
Eric Overfield - PixelMill - blog.pixelmill.com/ericoverfield - @EricOverfield
Friday, October 5, 2012 12:18 PM
You need to use some counter to add the webpart one after another ,
Zone index is the main perameter for the webpart position, you need to handle it as per your requirement
_destinationWebpartManager.AddWebPart(wp, webpartZoneID, 0);
_destinationWebpartManager.AddWebPart(wp, webpartZoneID, 1);
_destinationWebpartManager.AddWebPart(wp, webpartZoneID, 2);
Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.
Friday, October 5, 2012 12:26 PM
Hello,
Thanks for your suggestion. I have already tried this. The issue is that if you move the web part in your web part zone in the source page, SPLimitedWebPartManager does not return the collection in correct order.
I was just thinking if there was another way to get the position if we do not have the Zone index.
Thanks and Regards,
Neha
Tuesday, October 9, 2012 10:29 AM
Hi Eric,
I did as you suggested and it is working now.
Thanks for your help.
Neha