I am adding multiple list viewer web parts to a SharePoint [web part] page using the code below:

// Create the object SPLimitedWebPart Manager
SPLimitedWebPartManager WebPartMgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
// Track the zone index (order of placement, starting with zero) of web parts
int zoneCounter = 0;

// Add "Shared Documents" ListViewer webpart
// Get the object of the list of which we are creating the list viewer webpart
SPList SharedDocsLst = web.Lists["Shared Documents"];
ListViewWebPart SharedDocsLstViewWP = new ListViewWebPart();
// Set the properties of the webpart
SharedDocsLstViewWP.ChromeType = PartChromeType.Default;
SharedDocsLstViewWP.ChromeState = PartChromeState.Minimized;
SharedDocsLstViewWP.ListName = SharedDocsLst.ID.ToString("B").ToUpper();
SharedDocsLstViewWP.ViewGuid = SharedDocsLst.DefaultView.ID.ToString("B").ToUpper();
// Define the zone in which webparts need to be added
WebPartMgr.AddWebPart(SharedDocsLstViewWP, "Body", zoneCounter++);

//.... repeat 9 more times, to add 9 more document library list viewer web parts ....


The web part added to the page at the 5th position, is actually showing up at the end of the page. In other words, web parts 1-4 reflect the same order as the code. Web part 5 is showing up at the end, i.e. the 10th position. Web part 6-10 are showing up in positions 5-9.

I tried changing the order in the code. Somehow, the behavior is frustratingly consistent... the 5th web part added in the code is showing up at the end on the page. Anybody know why this could be happening?

  • I recognize this problem, but not sure if I have solved it. My first try though was to change the zoneCounter from zero-based to one-based, e.g start at position 1 – Robert Lindgren Jun 11 '13 at 6:29
  • That gave me tremendous hope, albeit for only a few moments :-( When I change zoneCounter to use one-based indexing, the fourth webpart inserted via code now shows at the end in the page. Somehow, any web part with zoneIndex = 4 (regardless of zero or one based) is pushed to the last! – Web User Jun 11 '13 at 13:28
  • 1
    Yeah now I remember how I did it. Since I had no webparts there from the beginning, I added them from the top (giving all index 0, and started adding them from the bottom instead of the top :P) It's a bit of a hacky way though – Robert Lindgren Jun 11 '13 at 13:37
  • I understand giving all web parts index = 0, but how do I add web parts from the bottom instead of the top in the code? You mean reverse the order of the web parts I want to add, and using zoneIndex of 0 for all of them? – Web User Jun 11 '13 at 13:47
  • 1
    I came across this post (blogs.msdn.com/b/jjameson/archive/2009/06/05/…), which elaborated on the sort of hack that you brought up. I did not have to reverse the order of the web parts in the code, but I had to manipulate the zoneCounter based on the ZoneIndex of each web part that was added in the code. – Web User Jun 11 '13 at 15:52

With a good tip (thanks to Robert) and another post on the web, I altered the last 2 lines of the web part addition code with the following to fix the issue. Note the removal of the ++ increment on zoneCounter in the AddWebPart method.

The issue surfaces when adding a fifth web part to the page. The weirdness of the issue has been acknowledged and it may be affecting only SharePoint 2007 (the version I am working on currently).

WebPartMgr.AddWebPart(SharedDocsLstViewWP, "Body", zoneCounter);
if (SharedDocsLstViewWP.ZoneIndex > zoneCounter)
    zoneCounter = SharedDocsLstViewWP.ZoneIndex + 1;

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.