Sometimes we need to modify the out-of-the-box templates to deploy an entire site hierarchy based on the designed architecture. There situations require a code-based approach; many times we can simply create our site template modifications in SharePoint's graphical user interface and then deploy the site hierarchy with a PowerShell script, avoiding the more complex development and code lifecycle issues.
In this article helps to accomplish the following tasks
- Create and configure a Web template in the SharePoint graphical user interface.
- Modify and deploy the template for global use on the farm.
- Create and execute a PowerShell script to deploy a hierarchy of sites.
Create and Configure a Web Template
- Go to your web application
- From the Site Actions menu, click New Site.
- Choose the Team Site template, fill in the site Title and URL and click Create.
- In your new site, in the left Quick Launch navigation, click Shared Documents.
- In the Ribbon, click the Library tab; then click the button labeled Library Settings.
- Click Version Settings.
- Change the Document Version Settings to Create Major and Minor (Draft) Versions.
- Set the Require Check Out setting to Yes.
- Click OK.
- In the left Quick Launch navigation, click Shared Documents.
- Upload a file or two to this library.
- Make other changes to list/library settings, and add other content to other places within the site.
- From the Site Actions menu, click Site Settings.
- In the Site Actions group, click Save Site as Template.
- Set the file name and template name to Demo, and ensure the Include Content box is checked.
- Click OK.
- When the template has been captured successfully, click the Solutions Gallery link that appears on the success confirmation page.
- Open another browser window and navigate to your Central Administration site. Click Create Site Collections under the Application Management group. In the Template Selection area, click through all the tabs. You notice that your Demo template is not listed and therefore not available for use outside the site collection in which it was created. The next section solves this issue.
Modify and Deploy a Web Template for Global Farm Use
The web template created above has been deployed to the site collection sandbox solution gallery. This means that it can now create new sites within that site collection only. But our aim is to build a hierarchy of sites in a new site collection, modify the web template in Visual Studio and deploy it to the farm.
- Navigate there by going to Site Settings (from the Site Actions menu) and then from theGalleries group, click Solutions.
- You should see the template that you created above. Click its name, and you will be prompted to download a .wsp file.
- Open Visual Studio by opening your Start Menu and selecting the Microsoft Visual Studio 2010 icon.
- From the Start Page in Visual Studio, on the left side click New Project.
- In the left list, expand the SharePoint node, and choose 2010. In the right list, choose the Import SharePoint Solution Package template.
In the Name text box, type TemplateDemo as the name for your new Visual Studio Project. See the following screenshot for guidance.
- Click OK. In the SharePoint Customization Wizard that follows, ensure that the local debugging site is pointed at your local SharePoint URL and change the trust level for your solution to Deploy as a Farm Solution. Click Next.
- When prompted for the path to the existing solution package, use the Browse button to navigate to the WSP file we saved in step 1. Click next.
- Ensure that all items are selected for import; then click Finish.
- In the Solution Explorer, find the four features that are labeled Feature1, Feature2, Feature3, and
Feature4. (See the image on the right.) Right-click on each one and choose Rename. Rename them as follows:
a. Feature1 ->ListInstances
b. Feature2 -> Modules
c. Feature3 ->WebTemplate
d.Feature4 ->PropertyBag
- If the Properties window is not visible, turn it on by opening the View menu in the main toolbar, and choose Properties Window (toward the bottom).
- Expand the feature that is now labeled ListInstance, and double-click the ListInstances.Feature node as shown here.
- In the Properties window, change the Is Hidden property to True, as shown in the screenshot to the right. Repeat this process for the Modules and PropertyBag features as well (but not the WebTemplate feature).
- Double-click the WebTemplate.feature file (as you have the others), but do not set it as hidden. Instead, find the scope setting, and change it from Site to Farm as shown in the following screenshot.
In the Properties window, find the Feature Id property and copy the GUID value (be sure to get all of it) and paste it into a Notepad document. You will need this later.
- Click the File menu, and choose Save All.
- In the Visual Studio Solution Explorer, right-click the project, and choose Package.
- Right-click again on the project, and choose Open Folder in Windows Explorer. In Explorer, navigate into the bin\Debug folder. Copy the TemplateDemo.wsp file from this folder to a location that has a shorter path, such as c:\Demo\ TemplateDemo .wsp.
- Close Visual Studio and open the SharePoint 2010 Management Shell from the Start Menu.
- Type in the following command to add your new solution to the farm:
Add-SPSolution c:\Demo\ TemplateDemo.wsp
- Type in the following command to deploy your new solution globally in the farm:
Install-SPSolution TemplateDemo.wsp -GACDeployment
- Close the SharePoint 2010 Management Shell, and open your Central Administration site in a browser.
- Click Create Site Collections under the Application Management group. In the Template Selection area, click the Custom tab, and notice that DemoTemplate now displays as a choice. You have now successfully made your web template available for global use! Use PowerShell to Deploy a Hierarchy of Sites.
Our last task is to create a hierarchy of sites using the template we modified in step 2 and create a PowerShell script that we develop. The following is the site taxonomy that we need to produce:
Normally, you would create this type of hierarchy at the root (/) of a Web application, but simply use the managed path/sites for convenience in your development environment.
- Open a new instance of Notepad. Create a PowerShell script in this simple editor for this portion of the lab. Also, find the Feature Id GUID that you saved earlier in a Notepad document, and copy it to your Clipboard.
- Add the following lines at the top of the script to set some variables and values to reuse throughout the script:
$scUrl = "Url of the site"
$scOwner = "domain\userID"
$template = "{pasteFeatureIdGUID}#DemoTemplate"
Paste the Template Id GUID from your Clipboard over pasteFeatureIdGUID (retain the { } brackets). Also replace the domain\userID with the account that you use (that is a farm admin).
- Add the following line to create a new site collection and provision its rootweb:
New-SPSite -Url $scUrl -OwnerAlias $scOwner-template $template -name "Home/News"
- Next, provision the Executives site with the next line:
New-SPWeb ($scUrl + "/Executives") -template $template -name "Executives" | ft -p url
The| ft -p url simply formats the output that you see when you run the script.
- Continuing adding lines for all the sites listed in the desired site taxonomy. You need to copy and paste only the preceding line and change the two places where Executives displays, making sure that in the first instance you add the entire hierarchical path to the site. After you complete your script, save it into the c:\Demo folder, and call it ProvisionSites.ps1
- Open the SharePoint 2010 Management Shell.
- Type in the following line to execute your provisioning script:
c:\lab2\ProvisionSites.ps1
- When your script completes without errors, open a web browser to the url we given earlier.
- In addition to verifying that all the sites in the hierarchy have been created, you should also test to see that all the customizations you made to your template (versioning settings in the Shared Documents library) and initial content (the files you uploaded) are present.