Monday, May 19, 2008

Hiding webpart and its properties through programming

Hi All,

There is a way how we can manage webparts placed at different page levels like "AllItems.aspx" , "default.aspx" etc.

To manipulate these webparts placed on pages you first need to make SPLimitedWebPartManager object and initialize it with calling GetLimitedWebPartManager of current site.

Once you have object of webpartmanager then you can use foreach loop to navigate to each webparts on that page and set several properties of it and finally call savechanges method of webpartmanager class and update the web.

Here is the way we can accomplish this task.


Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;

spWeb1.AllowUnsafeUpdates = true;


spWeb1.GetLimitedWebPartManager(
"default.aspx",
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

foreach (System.Web.UI.WebControls.WebParts.WebPart myWebPart in mgr.WebParts)
{
if (myWebPart.Title == "MyWebPart")
{
myWebPart.AllowEdit = false; //don't allow edit
myWebPart.AllowClose = false; // don't allow close

}

if (myWebPart.Title == "MySecondWebPart")
{
myWebPart.Hidden = true; //don't show webpart

}


}

mgr.SaveChanges(myWebPart);

spWeb1.Update();

spWeb1.AllowUnsafeUpdates = false;

That's it. your job is done.

2 comments:

Ariharaselvan said...

Hi
I am creating a custom toolpane using EditorWebPart, I would like to hide the default properties.
But its not happening. Is there any way to do that?
Following is my code.. which adds the tool pane.

EditorPartCollection IWebEditable.CreateEditorParts()
{
ArrayList editorArray = new ArrayList();

CommonEditorPart cEP = new CommonEditorPart();
cEP.ID = this.ID + "_commonEP1";
cEP.Add(cEP);

MyMainPageEditorPart editorPart = new MyMainPageEditorPart();
editorPart.ID = this.ID + "_editorPart1";
editorArray.Add(editorPart);

return editorParts;
}

SharePoint Kings said...

Ariharaselvan,

which property you want to hide?

because your code is different then as shown in article.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com