Hiding Getting Started Web Part From Each Team Site in SharePoint

Hi all.

For today this is a new scenario and solution.

Scenario: In our project, we are using an OOB team site for our team workspaces. So the customer will be creating team workspaces using the OOB team site. One of the requirements is, there should be no “GettingStartedWebPart” (as shown in the following figure) on the home page.

apps

The easiest way to remove the preceding web part is to just click on the “REMOVE THIS” link as shown in the preceding figure.

But we need to automate this process. So the customer does not need to explicitly click the link every time a new workspace is being created. So we can have a control / user control in which we will right the code to hide the web part.

But again the challenge is to make happen only once, the first time the page is being loaded. The code should not be executed every time the workspace is accessed.

Background

There is a hidden site feature called “Basic WebParts” that populates this “GettingStartedWebPart” in the web part gallery. Also we don't want to remove this web part from the web part gallery.

Related to this, there is one more concept. There is a web-level feature “Getting Started” as:

Getting Started

This feature is responsible for provisioning the “GettingStarted.aspx” page and adding a link to the site action menu.

site action menu

Solution

The following is the procedure:

  1. We have written one user control “RemoveGettingStartedWPFromPage.ascx”. From the code behind we hid the “GettingStartedWebPart” web part as (just sample code).
    1. web.AllowUnsafeUpdates = true;  
    2. string welcomePageUrl = web.RootFolder.WelcomePage;  
    3.        SPFile welcomePage = web.GetFile(welcomePageUrl);  
    4.   
    5. if (welcomePage.CheckOutType == SPFile.SPCheckOutType.None || (welcomePage.CheckOutType != SPFile.SPCheckOutType.None && welcomePage.CheckedOutByUser.LoginName.Equals(web.CurrentUser.LoginName)))  
    6. {  
    7.            if (welcomePage.CheckOutType == SPFile.SPCheckOutType.None)  
    8.      {  
    9.                    welcomePage.CheckOut();  
    10.      }  
    11.   using (SPLimitedWebPartManager webPartMngr =    
    12.               welcomePage.GetLimitedWebPartManager(PersonalizationScope.Shared))  
    13.          {  
    14.           foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartMngr.WebParts)  
    15.    {  
    16.       var webBrowsableObject = wp.WebBrowsableObject.ToString();  
    17.       if (webBrowsableObject ==   
    18.                      "Microsoft.SharePoint.WebPartPages.GettingStartedWebPart")  
    19.               {  
    20.                   wp.AllowEdit = true;  
    21.            wp.Hidden = true;  
    22.            webPartMngr.SaveChanges(wp);  
    23.          }  
    24.     }  
    25.   }  
    26.   welcomePage.CheckIn("Getting Started WebPart is Hidden!!");  
    27. }  
  2. We added one feature and using the feature added our user control to “AdditionalPageHead” control as:
    1. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
    2.   <Control Id="AdditionalPageHead" ControlSrc="~/_controltemplates/15/ /RemoveGettingStartedWPFromPage.ascx" />  
    3. </Elements>  
  3. Now this code will be executed when the page will be loaded. But we want to execute this code only once and not every time the workspace is executed.

    So we are deactivating the feature from the user control after hiding the web part as:
    1. // trying to deactivate the feature that this control is linked to so that this code will not run again.  
    2. bool hasFeature = false;  
    3. foreach (SPFeature f in web.Features)  
    4. {  
    5.     if (f.DefinitionId.ToString().Equals(customizationFeatureGuid, StringComparison.InvariantCultureIgnoreCase))  
    6.     {  
    7.         hasFeature = true;  
    8.         break;  
    9.     }  
    10. }  
    11. if (hasFeature)  
    12. {  
    13.     web.Features.Remove(new Guid(customizationFeatureGuid));  
    14. }  
    And we are done. This is how to execute the code only once.

    But this solution is purely for a Farm solution, I'll write a new article soon for SharePoint online.

    Also an important point here is an idea to execute the specific code only once on the page.

Thanks!

Enjoy reading.

Feel free to comment or provide feedback if you have any query.