SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the following code to hide all WebParts in a page.

string siteUrl = "http://site";
SPSite site = new SPSite(siteUrl);
SPWeb web;
web = site.RootWeb;
web.AllowUnsafeUpdates = true;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;
mgr = web.GetLimitedWebPartManager("pages/test.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
mgr.Web.AllowUnsafeUpdates = true;
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in mgr.WebParts)
            wp.Hidden = true;

Test Page has a Content Editor WebPart, Few List View WebParts and a Custom Webparts. Code runs without any errors but does not hide the Content Editor WebPart and Custom Webpart.

I am running this code in a Sharepoint 2010 installation. Any ideas?


  1. I am not able to hide them using Web Interface as well.
  2. I am able to change other properties like Title using code and it works perfectly fine for all thr Web Parts.
  3. This code works if the user is given Full Permission for the whole site (I am expecting it to work when the user is given contribute permission for the page alone)This code used to work in MOSS 2007 and the user was given contribute permission for this page alone.
share|improve this question
not related to question, but remember to dispose your SPSite and your SPLimitedWebPartManager :-) – Anders Rask May 3 '11 at 11:24
and possibly set "web.AllowUnsafeUpdates = false;"? – Mauro May 3 '11 at 12:04
@Anders I was about to add a disclaimer about it ;-) This is not the final code. Its a sample from the main module. – Shoban May 3 '11 at 12:20
@Mauro we have that line of code in our main module. – Shoban May 3 '11 at 12:43
@Mauro and hopefully set it back to true in a try/finally? – Anders Rask May 3 '11 at 13:19
up vote 1 down vote accepted

Finaly got it working. Had to change Web Part Security to fix it. Thanks for the help.

enter image description here

share|improve this answer
If this is the solution mark it as answer :) – Toni Frankola May 4 '11 at 19:29
@Toni ... experience with SO! I cant mark it as answer before 16 hours from posting :) – Shoban May 4 '11 at 20:01
I know :((((((( – Toni Frankola May 4 '11 at 20:22

Maybe you're looking for AllowHide property? :)

Update: I was able to hide a ContentEditorWebPart in my SharePoint 2010 environment. Maybe this may help you, here is the full list of properties of the webpart.

ContentLink                        :
Content                            : Content
PartStorage                        :
ContentHasToken                    : False
Title                              : Content Editor
FrameType                          : Default
SuppressWebPartChrome              : False
Description                        : Allows authors to enter rich text content.
IsIncluded                         : True
ZoneID                             : Left
PartOrder                          : 0
FrameState                         : Normal
Height                             :
Width                              :
AllowRemove                        : True
AllowZoneChange                    : True
AllowMinimize                      : True
AllowConnect                       : True
AllowEdit                          : True
AllowHide                          : True
IsVisible                          : True
Hidden                             : False
CatalogIconImageUrl                : /_layouts/images/mscontl.gif
TitleIconImageUrl                  :
TitleUrl                           :
DetailLink                         :
HelpUrl                            :
HelpLink                           :
HelpMode                           : Modeless
Dir                                : Default
PartImageSmall                     :
MissingAssembly                    : Cannot import this Web Part.
ImportErrorMessage                 : Cannot import this Web Part.
PartImageLarge                     : /_layouts/images/mscontl.gif
IsIncludedFilter                   :
AuthorizationFilter                :
ExportControlledProperties         : True
ConnectionID                       : 00000000-0000-0000-0000-000000000000
ID                                 : g_7762e716_abae_47f1_a4f6_fcafe0411f83
StorageKey                         : 7762e716-abae-47f1-a4f6-fcafe0411f83
Connections                        :
UnknownXmlElements                 : {}
SerializedAttachedPropertiesShared :
SerializedAttachedPropertiesUser   :
CssStyleSheet                      :
UseDefaultStyles                   : True
Qualifier                          :
ClientName                         : varPart
Caption                            :
Subtitle                           :
Permissions                        : PersonalProperties
BrowserDesignMode                  : False
EffectiveTitle                     : Content Editor
EffectiveStorage                   : Personal
EffectiveFrameType                 : Standard
AllowClose                         : True
ChromeState                        : Normal
ChromeType                         : Default
ConnectErrorMessage                :
Direction                          : NotSet
DisplayTitle                       : Content Editor
ExportMode                         : All
HasUserData                        : True
HasSharedData                      : False
IsClosed                           : False
IsShared                           : True
IsStandalone                       : False
IsStatic                           : False
Verbs                              : {}
WebBrowsableObject                 : Microsoft.SharePoint.WebPartPages.ContentE
Zone                               :
ZoneIndex                          : 0
Controls                           : {}
BackImageUrl                       :
DefaultButton                      :
GroupingText                       :
HorizontalAlign                    : NotSet
ScrollBars                         : None
Wrap                               : True
AccessKey                          :
Attributes                         : System.Web.UI.AttributeCollection
BackColor                          : Color [Empty]
BorderColor                        : Color [Empty]
BorderWidth                        :
BorderStyle                        : NotSet
ControlStyle                       : System.Web.UI.WebControls.PanelStyle
ControlStyleCreated                : True
CssClass                           :
Style                              : System.Web.UI.CssStyleCollection
Enabled                            : True
EnableTheming                      : True
Font                               :
ForeColor                          : Color [Empty]
HasAttributes                      : False
SkinID                             :
TabIndex                           : 0
ToolTip                            :
ClientID                           : g_7762e716_abae_47f1_a4f6_fcafe0411f83
EnableViewState                    : True
NamingContainer                    :
BindingContainer                   :
Page                               :
TemplateControl                    :
Parent                             :
TemplateSourceDirectory            :
AppRelativeTemplateSourceDirectory :
Site                               :
Visible                            : True
UniqueID                           : g_7762e716_abae_47f1_a4f6_fcafe0411f83

And here is the PowerShell code I used (it's actually similar to yours C#, I don't see any difference, as soon PowerShell simply calls SharePoint object model):

$w = Get-SPWeb http://localhost
$wpm = $w.GetLimitedWebPartManager("default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::User);

$wpm.WebParts[3].Hidden = $true
share|improve this answer
AllowHide is set as True. I did this when I added the WebPart manually. Let me check this via code as well. I am breaking my head with this issue for the past 2 days. :-) Thanks – Shoban May 3 '11 at 11:20
Update : Just for the heck of it I set this property via code and then hid the WebPart but it does not work :( – Shoban May 3 '11 at 11:21
I've just hidden ContentEditorWebPart in my own SharePoint 2010 environment, using your code. I think, something is wrong with your webpart. Could you please paste here all the properties values (you can grab them from PowerShell, for example)? Or maybe try to add a blank new ContentEditorWebPart and try to hide it? – Andrey Markeev May 3 '11 at 11:30
I updated my answer with full list of properties of my webpart. Hope this will help you! – Andrey Markeev May 3 '11 at 11:45
Thanks omlin I am going to try this... Did the user have full permission for the site or just the page? – Shoban May 3 '11 at 11:55

Are you running this code on a Publishing Site? Try running it on a non-publishing site.

I wonder if it's related to check-in / versioning of publishing pages.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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