简体   繁体   中英

Error when trying to create site collection using CSOM PowerShell

I am trying to run a PowerShell script that would generate the site colletion on the SharePoint tenant. However, whenever I am trying to execute the site collection creation, I get an error:

Exception calling "ExecuteQuery" with "0" argument(s): "Unknown Error" At C:\\projects\\SP\\Scripts\\Taxonomy\\Add-NavigationTerms1.ps1:120 char:5 + $TenantContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException

How would I go around this error? How can I fix it? Thank you very much for all your help!

Here is the code:

param (    
    [Parameter(Mandatory = $false)]    
    [string]    $sourceWeb = '',    
    [string]    $destinationWeb = '',       
    [Parameter(Mandatory = $false)]    
    [string]    $sourceListName = 'CurrentSites', #$sourceListName = 'O365 Sites',       
    [Parameter(Mandatory = $false)]    
    [string]    $sharepointAdmin = ''
)

<# 
Add-Type -Path "C:/Program Files/SharePoint Client Components/Assemblies/Microsoft.Online.SharePoint.Client.Tenant.dll" 
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.dll"   
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.Runtime.dll"   
Add-Type -Path "C:/Program Files/Common Files/microsoft shared/Web Server Extensions/16/ISAPI/Microsoft.SharePoint.Client.UserProfiles.dll"  
#>

$path = 'C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/15/ISAPI/' 
$simpleLinkUrlPropertyName = '_Sys_Nav_SimpleLinkUrl' 
$assemblies = 'Microsoft.SharePoint.Client.dll', 
'Microsoft.SharePoint.Client.Runtime.dll', 
'Microsoft.SharePoint.Client.Taxonomy.dll' 

$assemblies | % { Add-Type -Path (Join-Path $path $_) }

Add-Type -Path "C:/Program Files/SharePoint Client Components/Assemblies/Microsoft.Online.SharePoint.Client.Tenant.dll"

#$context = New-Object Microsoft.SharePoint.Client.ClientContext($sourceWeb)
#$password = Read-Host -Prompt 'Please enter your password' -AsSecureString
#$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sharepointAdmin,$password)

$password = ''


#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-SPOContext([string]$Url, [string]$UserName, [string]$Password) {
        $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
        return $context 
}

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {

        $list = $Context.Web.Lists.GetByTitle($listTitle)
        $Context.Load($list)
        $Context.ExecuteQuery()

        $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $list.GetItems($qry)
        $Context.Load($items)
        $Context.ExecuteQuery()
        return $items  
}

Function Create-Site-Collection([String]$url, [String]$title) {
        $TenantURL = ''
        $Title = 'New Published Collection List';

        [string]
        $TenantUserName = '';

        [Security.SecureString]
        #$TenantPassword = ConvertTo-SecureString 'Anew6731' -AsPlainText -Force
        $TenantPassword = convertto-securestring "Anew6731" -asplaintext -force

        #Open the Tenant Administration Context with the Tenant Admin Url
        $TenantContext = Get-SPOContext -Url $TenantUrl -UserName $TenantUserName -Password "Anew6731"


        #$TenantContext = New-Object Microsoft.SharePoint.Client.ClientContext($TenantUrl)
        #$TenantCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($TenantUserName, $TenantPassword)
        #$TenantContext.Credentials = $TenantCredentials

        #Get the tenant object
        $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($TenantContext)


        #Set the Site Creation Properties values
        $TenantProperties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
        #$TenantProperties.Url = $url
        $TenantProperties.Template = "CMSPUBLISHING#0 "
        $TenantProperties.Owner = $TenantUserName
        $TenantProperties.StorageMaximumLevel = 1000
        $TenantProperties.UserCodeMaximumLevel = 300
        $TenantProperties.Title = $title

        $TenantContext.Load($Tenant)
        $TenantContext.ExecuteQuery()

        #$TenantContext.Load($TenantProperties)
        #$TenantContext.ExecuteQuery()

        #Create the site using the properties

        $Tenant.CreateSite($TenantProperties) | Out-Null

        Write-Host "Creating site collection "
        #Create the site in the tennancy
        #$TenantContext.Load($Tenant)
        #$TenantContext.Load($spOnlineOperation)
        $TenantContext.ExecuteQuery()
        #$TenantContext.Dispose()    
}


$UserName = $sharepointAdmin
#$Password = Read-Host -Prompt "Enter the password"     
$Url = $sourceWeb

$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password $items = Get-ListItems -Context $context -ListTitle "O365 Sites"  

foreach ($item in $items) {    
        $Context.Load($item)    
        $Context.ExecuteQuery()   
        Write-Host $item["NEWURL"]    
        Write-Host $item["Title"]    
        #Write-Host $item.FieldValues    
        Create-Site-Collection -URL $item["NEWURL"] -Title $item["Title"]   
        #Write-Hsost $item.DisplayName 

} 

$context.Dispose()

I am honestly not sure what went wrong but this code/method worked just fine

Function Create-Site-Collection([String]$fullUrl, [Microsoft.SharePoint.Client.ClientContext] $TenantContext)
{
    Write-Host "Now configuring the new Site Collection"

    #Get the tenant object
    $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($TenantContext)

    #Set the Site Creation Properties values
    $properties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
    $properties.Url = $fullUrl
    $properties.Template =  "BLANKINTERNETCONTAINER#0"
    $properties.Owner = $AdminSiteUsername
    $properties.StorageMaximumLevel = 1000
    #$properties.UserCodeMaximumLevel = 100
    $properties.TimeZoneId = 10 # (UTC-05:00) Eastern Time (US and Canada)


    #Create the site using the properties
    $tenant.CreateSite($properties) | Out-Null

    $TenantContext.ExecuteQuery()

    Write-Host "Creating site collection"
    #Create the site in the tennancy
    try
    {
    $TenantContext.ExecuteQuery()
    Write-Host "Site Creation request completed. Note that the creation process is asynchronous and provisioning may take a short while."
    }
    Catch [Exception] 
    {
        Write-host $_.Exception.Message -f Red
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM