Post

Create new web in SharePoint Online with PowerShell
This is part 3 of 10 where we will be creating a new web in SharePoint Online with PowerShell. This is part of the following series: We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will […]

This is part 3 of 10 where we will be creating a new web in SharePoint Online with PowerShell. This is part of the following series:

We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will be creating a web for the newly created site. We then want to create a couple of site columns in part 4 which we will combine to a content type in part 5. This content type will be added (part 8) to our newly created document library in part 7 using a list template from part 6. After everything is set we will be setting the view in part 9 for this list to show the added columns we got from adding the content type. We only want to set permissions for myself so I’ll will be breaking the inheritance and setting permissions in part 10.

Create new web in SharePoint Online with PowerShell

This script will create a web in SharePoint Online. This web can be created under a site or another web by simply changing the $webURL variable (if you want to create a web under your current web you will need to start from the site and set “FirstWeb/SecondWeb” to create a web under FirstWeb). You can use the same template codes as a site which can be found using part 1. This script will be using CSOM to create new web in SharePoint Online. We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.

image

You will need to change the first variables to match your Office 365 tenant and copy this bit to PowerShell.

function new-spOnlineWeb {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com/sites/blogdemo"
    $webURL = "MyFirstWeb"
    $title = "My First Web"
    $template = "STS#0"
    $adminUrl = "https://spfire-admin.sharepoint.com"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    $useSamePermissionsAsParentSite = $true
    
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
        
    #Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    
    #create web using WebCreationInformatin object (wci)
    $wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation 
    $wci.url = $webURL
    $wci.title = $title
    $wci.webtemplate = $template
    $wci.useSamePermissionsAsParentSite = $useSamePermissionsAsParentSite
    $createWeb = $context.web.webs.add($wci)
    $context.load($createWeb)
    
    #send the request containing all operations to the server
    try{
        $context.executeQuery()
        write-host "info: Creating $($title)" -foregroundcolor green
    }
    catch{
        write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }
}
new-spOnlineWeb

image

You will be asked to enter the password and press enter

image

The request containing all operations to the server has been picked up without any errors. Navigate to the site/web to verify that the web has been created.

image

Tips

You can find more properties for the WebCreationInformation class at https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.webcreationinformation_members.aspx.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive