0

I am to run following script on SharePoint Online Management Shell, to bulk upload documents with metadata, but getting error(below screen shots)

Add-PSSnapin Microsoft.SharePoint.PowerShell  -ErrorAction SilentlyContinue 
#Configuration Variables
$SiteURL="https://tenant.sharepoint.com/sites/CMS/"
$LibraryName="TestLibrary"
$FilesPath="C:\Users\userX\Desktop\Upload Test 4 sug\NaUpload"
$CSVFilePath="C:\Users\userX\Desktop\Upload Test 4 sug\Nature.csv"

#Get the Web
$spWeb = Get-SPWeb $SiteURL
#Get the Target Document Library to upload
$Library = $Web.GetFolder($LibraryName)

#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath

#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{
    write-host "Uploading File:"$Row.FileName

    #Get the File from local disk 
    $SourcePath= Join-Path $FilesPath -ChildPath $Row.FileName
    $SourceFile = (Get-ChildItem $SourcePath).OpenRead()

    #Set Metadata Hashtable For the file
    $Metadata = @{"Document No" = $Row.Document_x0020_No; "Title" = $Row.Title}

    #upload the file              
    $uploaded = $Library.Files.Add($Row.FileName, $SourceFile, $Metadata, $TRUE)
}

Error:

enter image description here


After, input from @M.Qassas, I made some changes in my script and now I am getting Null-Valued Expression Error

Add-PSSnapin Microsoft.Online.SharePoint.PowerShell  -ErrorAction SilentlyContinue 

#Configuration Variables
$SiteURL= "https://tenant.sharepoint.com/sites/CMS"
$LibraryName= "TestLibrary"
$FilesPath= "C:\Users\userX\Desktop\Upload Test 4 sug\NaUpload"
$CSVFilePath= "C:\Users\userX\Desktop\Upload Test 4 sug\Nature.csv"

#Get the SpoWeb
$Web = Get-SPOSite $SiteURL
#Get the Target Document Library to upload
$Library = $Web.web.list.GetByTitle($LibraryName)

#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath

#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{
    write-host "Uploading File:"$Row.Name

    #Get the File from local disk 
    $SourcePath= Join-Path $FilesPath -ChildPath $Row.Name
    $SourceFile = (Get-ChildItem $SourcePath).OpenRead()

    #Set Metadata Hashtable For the file
    $Metadata = @{"Document No" = $Row.Document_x0020_No; "Title" = $Row.Title}

    #upload the file              
    $uploaded = $Library.Files.Add($Row.Name, $SourceFile, $Metadata, $TRUE)
}

Error:

enter image description here

2 Answers 2

3

For the first error, in SharePoint online there is no direct command to Get-SPWeb , you should use SharePoint Client Side model as shown below:

function Get-SPOWebs($url){

#fill metadata information to the client context variable

$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$context.Credentials = $SPOcredentials

$web = $context.Web

$context.Load($web)

$context.Load($web.Webs)

$context.load($web.lists)

try{

$context.ExecuteQuery()

#loop through all lists in the web

foreach($list in $web.lists){

add-content -value "<tr><td><span style='margin-left:$($pixelslist)px'>$($list.title)</td><td>List/library</td><td></td><td>$($list.itemcount)</td></tr>" -path $filePath

}

#loop through all webs in the web and start again to find more subwebs

$pixelsweb = $pixelsweb + 15

$pixelslist = $pixelslist + 15

foreach($web in $web.Webs) {

add-content -value "<tr style='background-color:yellow'><td><span style='margin-left:$($pixelsweb)px'>$($web.url)</td><td>Web</td><td>$($web.webtemplate)</td><td></td></tr>" -path $filePath

write-host "Info: Found $($web.url)" -foregroundcolor green

Get-SPOWebs($web.url)

}

}

catch{

write-host "Could not find web" -foregroundcolor red

}

For more details check

For the second error, based on your script, $Web does not exist and not set before, So make sure it's $spWeb


Note: try to setup and run your script using SharePoint Online Management Shell

4
  • 'PS C:\Windows> $Web = Get-SPOWeb $SiteURL' Get-SPOWeb : The term 'Get-SPOWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:8 + $Web = Get-SPOWeb $SiteURL + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-SPOWeb:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    – cjs
    Commented Aug 4, 2017 at 10:04
  • getting above, when running with Get-SPOWeb
    – cjs
    Commented Aug 4, 2017 at 10:07
  • Try to Set up the SharePoint Online Management Shell environment support.office.com/en-us/article/… Commented Aug 4, 2017 at 10:08
  • 1
    Tried but same result...
    – cjs
    Commented Aug 4, 2017 at 10:21
2

First you need to refer SharePoint client dlls

Add-Type -Path "\\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "\Microsoft.SharePoint.Client.Runtime.dll"

#next pass the credentials and connect to web    
$siteURL = ""     
$userName = ""   
$PlainPassword = ""  
$password = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force

# set SharePoint Online credentials
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)  

#Creating client context object
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$clientContext.credentials = $SPOCredentials  

$web = $clientContext.Web  
$clientContext.load($web)  
$clientContext.executeQuery()  
4
  • Do I need to add this, before my code !
    – cjs
    Commented Aug 4, 2017 at 11:25
  • your code is in SSOM , you need to refer dlls to use CSOM, powershell up to web ,remaining need to change to csom
    – kumar
    Commented Aug 4, 2017 at 11:33
  • Thanks for your input, but I do not have any coding background. Made some changes in my script. Please check my edited question...
    – cjs
    Commented Aug 4, 2017 at 11:39
  • Both Add-Type lines throw Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path ... because it does not exist." for me.
    – Nae
    Commented Oct 16, 2018 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.