Friday, December 22, 2017

Create a SharePoint Online Site - Office 365 using Powershell

Create a SharePoint  Online Site - Office 365 using Powershell

# Usage
#       .\create-site-csom.ps1 -siteTitle "new site2" -siteUrl "newsite2" -cred $false -parent "https://pravahaminfo.sharepoint.com/teams/appdev1/"
#
#       .\create-site-csom.ps1 -siteTitle "new site2" -siteUrl "newsite2" -cred $true -parent "https://pravahaminfo.sharepoint.com/teams/appdev1/"
#               Will ask for Credentials

Param(
    [Parameter(Mandatory=$true)]  [string] $parent, 
[Parameter(Mandatory=$true)]  [string] $siteTitle,
[Parameter(Mandatory=$true)] [string] $siteUrl,
[string] $siteDescription,
    [string] $template = "STS#0",
    [bool] $cred #Use custom Credentials
)

#Clear-Host


Write-Host "Initializing..."
$resources = split-path $script:MyInvocation.MyCommand.Definition
if(-not $(Test-Path "$resources\bin"))
{
$resources = split-path $script:MyInvocation.MyCommand.Definition | split-path -parent
}
try {
    if ($cred)
    {
        Add-Type -Path "$resources\bin\2013\Microsoft.SharePoint.Client.dll" 
        Add-Type -Path "$resources\bin\2013\Microsoft.SharePoint.Client.Runtime.dll" 
        Add-Type -Path "$resources\bin\2013\Microsoft.SharePoint.Client.Publishing.dll"
        $Username = Read-Host -Prompt "Please enter your username"
        $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($parent)
        $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
        $Context.Credentials = $Creds
    }
    else {
        Add-Type -Path "$resources\bin\2010\Microsoft.SharePoint.Client.dll"
        Add-Type -Path "$resources\bin\2010\Microsoft.SharePoint.Client.Runtime.dll"
        #Add-Type -Path "$resources\sp2010\Microsoft.SharePoint.Client.Publishing.dll"
        Add-Type -Path "$resources\bin\2013\ClaimsAuth.dll"
        $context = [MSDN.Samples.ClaimsAuth.ClaimClientContext]::GetAuthenticatedContext($parent);
    }
}
catch [System.Exception] {
    Write-Host "Error when connecting to parent site: " $_.Exception.Message -ForegroundColor Red
    return
}

Write-Host "Creating..."
try {
    #Create SubSite
    $WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $WCI.WebTemplate = $template
    $WCI.Description = $siteDescription
    $WCI.Title = $siteTitle
    $WCI.Url = $siteUrl
    $WCI.Language = "1033"
    $SubWeb = $Context.Web.Webs.Add($WCI)
    $Context.ExecuteQuery()

<#
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Context)
$scProps = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
$scProps.Url = $siteUrl
$scProps.Title = $siteTitle
$scProps.Owner = $Username
#$scProps.StorageMaximumLevel = 100
#$scProps.UserCodeMaximumLevel = 50
$scProps.Template = $template
$scProps.Lcid = 1033
$operation = $tenant.CreateSite($scProps)
$Context.Load($scProps)
$Context.ExecuteQuery()
#>
}
catch [System.Exception] {
    Write-Host "Error when creating site: " $_.Exception.Message -ForegroundColor Red
    return
}

No comments:

Post a Comment