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.
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:
This feature is responsible for provisioning the “GettingStarted.aspx” page and adding a link to the site action menu.
Solution
The following is the procedure:
- We have written one user control “RemoveGettingStartedWPFromPage.ascx”. From the code behind we hid 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!!");
- }
- We added one feature and using the 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 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:
-
- 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 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.