0

I have a Home.aspx page with three web parts on the top, and now I want to add a new one above them, but everytime is being created in the last position. I've tried modifying zoneIndex property for all of them from 1 to 3, and after that adding the new one with zoneIndex set to 0, but it stills being at the end of the layout. So this is what I want, but I'm having this (sorry for the poor images).

Code to move one position the existing web parts:

Dim webPartManager As SPLimitedWebPartManager = sitioWeb.GetLimitedWebPartManager(sitioWeb.ServerRelativeUrl & "/SitePages/Home.aspx", PersonalizationScope.Shared)
Dim webPartList As IEnumerable = From webPart In webPartManager.WebParts Select webPart
Dim zoneIndex As Integer = 1
Dim wpCount As Integer = webPartManager.WebParts.Count

For Each eachWebPart In webPartList
      Stop
      eachWebPart.Hidden = False
      eachWebPart.ZoneID = "wpz"
      eachWebPart.PartOrder = zoneIndex
      webPartManager.MoveWebPart(eachWebPart, eachWebPart.ZoneID, zoneIndex)
      zoneIndex = zoneIndex + 1
      webPartManager.SaveChanges(eachWebPart)
 Next

Code to add the new web part:

Dim webPartManager As SPLimitedWebPartManager = sitioWeb.GetLimitedWebPartManager(sitioWeb.ServerRelativeUrl & "/SitePages/Home.aspx", PersonalizationScope.Shared)
Dim newWebPart As ContentEditorWebPart = New ContentEditorWebPart()

newWebPart.Title = "WP_Header"
newWebPart.ZoneID = "wpz"
newWebPart.PartOrder = 0
newWebPart.ContentLink = Site.ServerRelativeUrl & "/SiteAssets/DocumOblig.txt"
newWebPart.ChromeState = PartChromeState.Normal
newWebPart.ChromeType = PartChromeType.None

webPartManager.AddWebPart(newWebPart, "wpz", 0)
webPartManager.SaveChanges(newWebPart)

There is also two things that are confusing me:

  1. On the new web part, if I set ZoneID as "wpz" it doesn't shows it, but if I name it with "Bottom" or "Top", it appears in the page.
  2. Always, when I go to the Edit mode on Home.aspx page, it just shows me the web parts which are already created, but not this new one.

Anyone knows why this could be happening?

0

Well, I discovered the problem. When I'm adding a new web part, I set the ZoneIndex property to 0, but in code web part is being writed at the end of the web parts list, so they're being renderized on this way. I found a solution here. I just needed to make a copy of the existing web parts, removing from the aspx page, and finally adding them in the proper order, one by one. I hope someone find useful this way to solve it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.