all.
I am new to PowerShell, but I’m trying to learn it to manage my SharePoint site.
I thought this would be a good opportunity to try something new.
I found a PowerShell script on the SharePoint Diary which adds multiple document libraries to a SharePoint online site, using a .csv template as reference; however, I get the following error message. Any help would be appreciated.
Error: this is repeated for each library on the .csv file:
Ensuring Document Library 'Team Docs'
Error: Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
New-Object:
Line |
72 | … edentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCr …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling ".ctor" with "2" argument(s): "The 'username' argument cannot be null. (Parameter 'username')"
MethodInvocationException:
Line |
77 | $Ctx.executeQuery()
| ~~~~~~~~~~~~~~~~~~~
| Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
Script: My information removed
>> Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
>> Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
>>
>> #Function to Create a SharePoint Online document library
>> Function CreateSPO-DocumentLibrary()
>> {
>> [cmdletbinding()]
>> param
>> (
>> [Parameter(Mandatory=$True,ValueFromPipeline)] [String] $LibraryName,
>> [Parameter(Mandatory=$False)] [String] $Description,
>> [Parameter(Mandatory=$False)] [Switch] $ShowOnQuickLaunch
>> )
>> Try {
>> Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
>>
>> #Get All Existing Lists from the web
>> $Lists = $Web.Lists
>> $Ctx.Load($Lists)
>> $Ctx.ExecuteQuery()
>>
>> #Check if Library name doesn't exist already
>> If(!($Lists.Title -contains $LibraryName))
>> {
>> #Set Quick Launch Option
>> If($ShowOnQuickLaunch.IsPresent)
>> {
>> $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::On
>> }
>> Else
>> {
>> $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::Off
>> }
>>
>> #Create Document Library
>> $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
>> $ListInfo.Title = $LibraryName
>> $ListInfo.Description = $Description
>> $ListInfo.TemplateType = [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary
>> $List = $Web.Lists.Add($ListInfo)
>> $List.OnQuickLaunch = $QuickLaunchOption
>> $List.Update()
>> $Ctx.ExecuteQuery()
>>
>> write-host -f Green "`tNew Document Library '$LibraryName' has been created!"
>> }
>> Else
>> {
>> Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
>> }
>> }
>> Catch {
>> write-host -f Red "`tError:" $_.Exception.Message
>> }
>> }
>>
>> #Set Parameters
>> $CSVFilePath = "C:UserspjonesOneDrive - Advocates for Human PotentialDesktopCCBHC-E Doc LibrariesDocLibs.csv"
>>
>> #Get Credentials to connect
>> Connect-SPOService -Url https://<tenent>-admin.sharepoint.com
>>
>> #Get the CSV file
>> $CSVFile = Import-Csv $CSVFilePath
>>
>> #Read CSV file and create a document library
>> ForEach($Line in $CSVFile)
>> {
>> #Setup the context
>> $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Line.SiteURL)
>> $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
>>
>> #Get the web from URL
>> $Web = $Ctx.web
>> $Ctx.Load($Web)
>> $Ctx.executeQuery()
>>
>> #Get the ShowOnQuickLaunch option
>> $ShowOnQuickLaunch = [System.Convert]::ToBoolean($Line.ShowOnQuickLaunch)
>>
>> #Call the function to create document library
>> If($ShowOnQuickLaunch -eq $True)
>> {
>> CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description -ShowOnQuickLaunch
>> }
>> Else
>> {
>> CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description
>> }
>> }
Phil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.