Sunday, 1 February 2015

Hiding getting started web part from each team site

Hi All,

Today new scenario, solution J


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

Easiest way to remove the above web part is just click on “REMOVE THIS” link as shown in above figure.
But we need to automate this process. So customer no need to explicitly click the link every time new workspace is getting created.  So we can have a control / user control in which we will right the code to hide the web part. 
But again challenge is this should happen only once when first time page is getting loaded. Code should not be executed every time when workspace is accessed.
There is a hidden Site feature called “Basic WebParts”, which populates this “GettingStartedWebPart” in web part gallery. Also we don’t want to remove this web part from web part gallery.
Related to this there is one more concept, there is a web level feature “Getting Started” as

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

We have written one user control “RemoveGettingStartedWPFromPage.ascx”. From code behind we have hide the “GettingStartedWebPart” web part as (just sample code :))
           web.AllowUnsafeUpdates = true;
            string welcomePageUrl = web.RootFolder.WelcomePage;
           SPFile welcomePage = web.GetFile(welcomePageUrl);
if (welcomePage.CheckOutType == SPFile.SPCheckOutType.None || (welcomePage.CheckOutType != SPFile.SPCheckOutType.None && welcomePage.CheckedOutByUser.LoginName.Equals(web.CurrentUser.LoginName)))
              if (welcomePage.CheckOutType == SPFile.SPCheckOutType.None){
            using (SPLimitedWebPartManager webPartMngr =  
          foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartMngr.WebParts)
              var webBrowsableObject = wp.WebBrowsableObject.ToString();   
             if (webBrowsableObject ==
                     "Microsoft.SharePoint.WebPartPages.GettingStartedWebPart") {
                  wp.AllowEdit = true;
                    wp.Hidden = true;
           welcomePage.CheckIn("Getting Started WebPart is Hidden!!");
 Added one feature and using feature added our user control to “AdditionalPageHead” control as
<Elements xmlns="">
  <Control Id="AdditionalPageHead" ControlSrc="~/_controltemplates/15/  
                                                /RemoveGettingStartedWPFromPage.ascx" />

Now this code will be executed when page will be loaded. But we want to execute this code only once and not every time workspace is get executed.
So we are deactivating the feature from the user control after hiding the web part as
// trying to deactivate the feature that this control is linked to so that this code will not run again.
                bool hasFeature = false;
                foreach (SPFeature f in web.Features)
                    if (f.DefinitionId.ToString().Equals(customizationFeatureGuid, StringComparison.InvariantCultureIgnoreCase))
                        hasFeature = true;
                if (hasFeature)
                    web.Features.Remove(new Guid(customizationFeatureGuid));
And we are done. This is the way to execute the code only once :)
But this solution is purely for farm solution, I’ll write new post soon for SharePoint online.
Also important point here is an idea to execute the particular code only once on the page.
Enjoy reading :)
Feel free to comment / feedback if any or if you have any query :)

No comments: