Sunday, 1 February 2015

Hiding getting started web part from each team site


Hi All,

Today new scenario, solution J

Scenario:



 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.
Background:
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



Solution:
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){
                   welcomePage.CheckOut();
              }
            using (SPLimitedWebPartManager webPartMngr =  
              welcomePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
         {
          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;
                    webPartMngr.SaveChanges(wp);
                 }
             }
          }
           welcomePage.CheckIn("Getting Started WebPart is Hidden!!");
}
 Added one feature and using feature added our user control to “AdditionalPageHead” control as
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead" ControlSrc="~/_controltemplates/15/  
                                                /RemoveGettingStartedWPFromPage.ascx" />
</Elements>

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;
                        break;
                    }
                }
                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.
Thanks!
Enjoy reading :)
Feel free to comment / feedback if any or if you have any query :)

No comments: