A way of hiding ListViewWebParts in all pages of SharePoint site in a single shot

Recently, I got a support request from one of my customer and their requirement was to hide all the listview web parts from all the pages in a SharePoint site based upon the log in user's security permission. First, I planned to implement that functionality using a custom HTTPModule, but after that I got an idea on implementing the same functionality using a custom master page and it was really a feasible solution. What I did that I have created a user control and implemented the logic in the OnLoad() event to hide the webparts. After that, I have registered that control in a custom master page and applied it to the SharePoint site and it worked well.

If you get this type of requirement then you can use this work-around and it will be really feasible and scalable solution. Now I will give the details about development of this solution. First you need to create a class library project to implement the logic of hiding the webparts.Below I have added a sample code snippet for hiding all the ListViewWebParts from a page. (change the logic according to your requirment)

namespace HideWebPartControlLogic

{

public class HideWebPartControl: System.Web.UI.UserControl

{

private void IterateThroughChildren(Control parent)

{

try

{

foreach (Control c in parent.Controls)

{

if (c.GetType().ToString().Equals("Microsoft.SharePoint.WebPartPages.ListViewWebPart"))

{

Microsoft.SharePoint.WebPartPages.ListViewWebPart webpart = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)c;

webpart.Hidden = true;

}

else

{

IterateThroughChildren(c);

}

}

}

catch(Exception ex){}

}

 

protected override void OnLoad(EventArgs e)

{

SPWeb oWeb = SPContext.Current.Web;

            if (oWeb.CurrentUser.Email == "sowmyans@microsoft.com")

            {

                foreach (Control c in Page.Controls)

                {

                    IterateThroughChildren(c);

                }

}

}

} } }

You can also hide the webparts in a page using WebPart Manager class (SPLimitedWebPartManager), but here the concern is, since we are updating the changes using oweb.Update() it will update the changes to the content DB and it will hide the webparts for all users. But using the above code we are hiding the webpart controls from the control collection of Page, so it will hide the controls only for that specific user with email ID – sowmyans@microsoft.com (Below I am giving the code for hiding the webparts in that way - it will help, if we have some requirement to hide or edit something in all pages for all users )

SPWeb oWeb = SPContext.Current.Web;

SPLimitedWebPartManager oWebPartManager = oWeb.GetLimitedWebPartManager("Shared%20Documents/MyCustomPage.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);// change the wepart page absolute URL to your webpart page URL

foreach (System.Web.UI.WebControls.WebParts.WebPart oWebpart in oWebPartManager.WebParts)

{

if (oWebpart.GetType().Name == "ListViewWebPart")

{

myWebPart.Hidden = true;

oWebPartManager.SaveChanges(myWebPart);

}

}

oWeb.Update();

Ok, now we can come back to our solution implementation part. After creating that class library, compile it with a strong name and deploy it to the GAC. Now, we have created our class library which will provide the code behind logic to our user control. Our next step is - create a user control. For that, you can go to the following location C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES and copy any of the existing user control and rename it - say HideWebPartControl.ascx.

Now, remove all the content from the renamed user control and add only the directive that I given below.

<%@ Control Language="C#" Inherits="HideWebPartControlLogic.HideWebPartControl,HideWebPartControlLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=75deafbb27b17b8e" compilationMode="Always" %>

Now our user control is ready to execute our code implemented to hide to the webparts at the time of page load. Our next step is to add this user control in our custom master page and apply that master page to our SharePoint site. If you are using default.master as your master page then follow the below steps to create a custom master page.

  1. Make a copy of default.master page by taking it from the following location C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL.
  2. Rename it, say mydefault.master, and open it for editing.
  3. Register and create a control tag of our user control using the below tags.

    <%@ Register TagPrefix="wssuc" TagName="HideWebPartControl" src="~/_controltemplates/HideWebPartControl.ascx" %>

    <wssuc:CiscoControl id="CiscoControl" runat="server" EnableViewState="false"></wssuc:CiscoControl>

Now we are ready with our custom master page with the functionality hide all the ListViewWebParts in all SharePoint pages. Our next step is to apply this custom master page to the SharePoint site. To do that follow the below steps.

1.       Open the SharePoint site, and navigate to the Master Page Gallery - Site Actions à Site Settings à Galleries à Master Pages

2.       Upload the custom master page to this gallery

3.       Now apply the master page to the site by going to the following location - Site Actions à Site Settings à Look and Feel à Master Page

 

After completing the above steps your SharePoint site will be using the custom master page and you can see all list view webparts are hiding from every page.

Comments