2

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; mgr.SaveChanges(wp); } web.Dispose(); site.Dispose(); 

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?

Note:

  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.
5
  • not related to question, but remember to dispose your SPSite and your SPLimitedWebPartManager :-) Commented May 3, 2011 at 11:24
  • and possibly set "web.AllowUnsafeUpdates = false;"? Commented May 3, 2011 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. Commented May 3, 2011 at 12:20
  • @Mauro we have that line of code in our main module. Commented May 3, 2011 at 12:43
  • @Mauro and hopefully set it back to true in a try/finally? Commented May 3, 2011 at 13:19

3 Answers 3

1

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

enter image description here

3
  • If this is the solution mark it as answer :) Commented May 4, 2011 at 19:29
  • @Toni ... experience with SO! I cant mark it as answer before 16 hours from posting :) Commented May 4, 2011 at 20:01
  • I know :((((((( Commented May 4, 2011 at 20:22
0

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 ditorWebPart 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 $wpm.SaveChanges($wpm.Webparts[3]); 
11
  • 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 Commented May 3, 2011 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 :( Commented May 3, 2011 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? Commented May 3, 2011 at 11:30
  • I updated my answer with full list of properties of my webpart. Hope this will help you! Commented May 3, 2011 at 11:45
  • Thanks omlin I am going to try this... Did the user have full permission for the site or just the page? Commented May 3, 2011 at 11:55
0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.