SharePoint online provides only a small set of basic commands
for writing powershell scripts. IF we have to do any operation using powershell
we need to use CSOM or REST API.
Sharepoint patterns and Practises (PnP) team came with a powershell
module for doing operation on the sharepoint online. Its very help full while
writing scripts. Similar modules exists
for Sharepoint 2013 and Sharepoint 2016. Link to the Github
respositry.
In the below article I will try to add items to a list
picking the data from the JSON file.
- Try to check whether SharepointPNPPowershellOnline module was installed or not. If not install it.
# This script adds items to the list picking up the data from the json file. if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline)) { Install-Module SharePointPnPPowerShellOnline }
- Get the data from the json file and store it in a variable. We can read the data from a json file similar to how we read from a CSV file. In the JSON file I am storing all the tenant, sharepoint details and the data to be updated to the list.
# Gets or Sets the JSON data. $jsonconfig = $null # Imports the json configuration template. try { $jsonconfig = Get-Content .\PollData.json|ConvertFrom-Json } catch { # Prompts for json configuration template path. $jsonconfigPath = Read-Host -Prompt "Please enter the json configuration template full path" $jsonconfig = Get-Content -Path $jsonconfigPath|ConvertFrom-Json }
- Get the credentials for the sharepoint online either from the windows credential manager or ask the user to enter the credentials. If the windows credential manager was configured then provide the label name.
# Gets or Sets the tenant admin credentials. $credentials = $null # Windows Credentials Manager credential label. $winCredentialsManagerLabel = "ShareOnline" # Gets stored credentials from the Windows Credential Manager or show prompt. # How to use windows credential manager: # https://github.com/SharePoint/PnP-PowerShell/wiki/How-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell if((Get-PnPStoredCredential -Name $winCredentialsManagerLabel) -ne $null) { $credentials = $winCredentialsManagerLabel } else { # Prompts for credentials, if not found in the Windows Credential Manager. $email = Read-Host -Prompt "Please enter tenant admin email" $pass = Read-host -AsSecureString "Please enter tenant admin password" $credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $email, $pass } if($credentials -eq $null -or $jsonconfig -eq $null) { Write-Host "Error: Not enough details." -ForegroundColor DarkRed exit 1 }
- Connect to the Sharepoint online tenant using
the command Connect-PnpOnline, pass
the tenant id parameter from the JSON file. Pass the credential value also as shown below.
Connect-PnPOnline $jsonconfig.TenantUrl -Credentials $credentials
- Use the ADD-PnPListItem command to add
items to the list. The command accepts
the following parametersa. List name :- Name of the list, it’s a mandatory parameterb. Content type :- Name of the content type, if not mentioned item will be added using the default content typec. Folder :- Folder relative url if anyd. Web :- it’s a optionla parameter if the list is part of a sub web, if no value is mentioned it takes the current web.e. Value :- Form an array with the internal names and the values. @{"Title" = "Title New"}
- Pass the list name and the value array as shown below. Run the script.
$listItems=$jsonconfig.PollQuestions foreach($item in $listItems){ $options=$null $item.Options|%{if($options -ne $null){$options=$options+"`n"+$_}else{$options=$options+$_}} $valuestr=@{"Title"=$item.Question;"Question"=$item.Question;"Options"=$options;"Published_x0020_Date"=$item.PublishedDate;"Expiry_x0020_Date"=$item.ExpiryDate} Add-PnPListItem -List $jsonconfig.ListName -Values $valuestr }
The format of the JSON file I have used for my script is at the below link.
https://github.com/dineshR86/PowershellScriptsRefrence/blob/master/AddItemsToList/PollData.json
The script can be downloaded from the Git link.
https://github.com/dineshR86/PowershellScriptsRefrence/tree/master/AddItemsToList
https://github.com/dineshR86/PowershellScriptsRefrence/blob/master/AddItemsToList/PollData.json
The script can be downloaded from the Git link.
https://github.com/dineshR86/PowershellScriptsRefrence/tree/master/AddItemsToList
Comments
Post a Comment