This is about the Web Part Framework in ASP.NET 2.0. What I wanted: a homepage where users have room to include custom web parts and to customize default web parts, but also containing some info that they cannot change. This fixed section would include stuff like “Announcements from the board” or “Motivational message of the day”.
You can of course place these controls outside the zones, so they can not be modified without touching the code. But another solution would be to have these controls also be web parts, but have them changed only in the shared scope. Then we can easily allow only certain people to switch to shared scope on the homepage (you can set up roles with this privilige in web.config). Sadly, you can not configure a zone or a web part to deny personalisation based upon the zone it is in.
The solution: we create a custom web part zone, called UnpersonalizedZone, which only allows any modification to it's contents when the current scope is “Shared”. This will not only deny users to perform the Edit, Delete, Minimize, Maximize and Close verbs, but it will also block the dragging and dropping of web parts in design mode. Also, in Catalog mode, the zone will not appear in the list of target zones for adding new web parts.
The code is surprisingly easy:
public class UnpersonalizedZone : WebPartZone
{
public UnpersonalizedZone()
{
}
public override bool AllowLayoutChange
{
get
{
if (this.WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
{
return base.AllowLayoutChange;
}
else
{
return false;
}
}
set
{
base.AllowLayoutChange = value;
}
}
protected override void OnPreRender(EventArgs e)
{
if (this.WebPartManager.Personalization.Scope == PersonalizationScope.User)
{
EditVerb.Visible = false;
foreach (WebPart wp in this.WebParts)
{
wp.AllowEdit = false;
}
}
}
}