Sunday, March 20, 2011

How to get WebPart ZoneId from WebPart page?

After getting SPLimitedWebPartManager instance through SPWeb.GetLimitedWebPartManager(), how can we get the ZoneId array of that WebPart page?

It seems we cannot get the ZoneIds directly, although we can see it in watch window during debugging. It is in SPLimitedWebPartManager.WebPartManager.WebPartZoneIds[]. However "WebPartManager" is not public variable.


Below is the screen shot of that WebPart page:

So we can easily guess out the ZoneId of each WebPart zone.

        public const string Zone_TitleBar = @"TitleBar";
        public const string Zone_Header = @"Header";
        public const string Zone_LeftColumn = @"LeftColumn";
        public const string Zone_MiddleColumn = @"MiddleColumn";
        public const string Zone_RightColumn = @"RightColumn";
        public const string Zone_Footer = @"Footer";

Now, at least we know what we should do if we want to add a WebPart into the "Left Column" zone.

spLimitedWebPartManager.AddWebPart(objListViewWebPart, Zone_LeftColumn, 1);

2 comments:

  1. A Thing to note :

    In SharePoint 2010 the property ZoneId does not exist anymore...

    You must use the method

    GetZoneID(WebPart wp) of the SPLimitedWebPartManager to get the ZoneId

    ReplyDelete
  2. Thanks.

    I guess the best way is to get them through SPLimitedWebPartManager (http://social.technet.microsoft.com/Forums/br/sharepoint2010programming/thread/d83f7485-c983-4497-b92a-bfc9b3827011):

    SPSecurity.RunWithElevatedPrivileges(delegate() {
    SPSite mySiteCollection = new SPSite(siteURL);
    SPWeb w = mySiteCollection.OpenWeb();
    SPLimitedWebPartManager wpm= w.GetLimitedWebPartManager(pageURL, PersonalizationScope.Shared);

    SPWebPartManager manager = this.Parent.Parent as SPWebPartManager;//this.Parent is not //accesible or not present
    WebPartZoneCollection zones = manager.Zones;
    string zid=string.Empty;
    if (zones != null && zones.Count > 0)
    {
    foreach (System.Web.UI.WebControls.WebParts.WebPartZone zone in zones)
    {
    zid+= zone.ID.ToString();
    }
    }
    });

    ReplyDelete