Skip to main content

Add Items to sharepoint online List using powershell!!

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.
  1.          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  
     }  
    
  2.          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  
     }  
    
  3.           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  
     }  
    
  4.  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  
    
  5.  Use the ADD-PnPListItem command to add items to the list.  The command accepts the following parameters
    a.       List name :- Name of the list, it’s a mandatory parameter
    b.      Content type :- Name of the content type, if not mentioned item will be added using the default content type
    c.       Folder :- Folder relative url if any
    d.      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"}
  6. 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 

Comments

Popular posts from this blog

Sharepoint 2013 REST API limitations with site columns

Recently i attended an interview for one of the company, the interviewer asked me a question on rest api. How to get a publishing image field using rest api? I answered its the normal way how we get the other fields. But later i came to know that its the wrong answer. After some research on Google came to know that there some limitations to rest api in sharepoint 2013. Below is a list of columns available using Rest api. Column Support Notes Hyperlink or Picture Supported Single Line of Text Supported Multiple lines of text :: Plaintext Supported Multiple lines of text :: Richtext Supported Returns unencoded XHTML Multiple lines of text :: Enhanced Richtext Supported Returns unencoded XHTML Choice Supported Column is required in the  $expand  keyword Counter Supported Integer Supported Number Supported Currency Supported Date Supported Returns an ISO 8601 date e.g. 2013-03-08T11:00:00 Yes/No Supported Returns true or false string literals Person or Group Supported Colu

How to upgrade typescript version in a SPFX solution

Why do we need to upgrade typescript version in a SPFX solution? As part of SPFX development we try to install 3 rd party libraries, there is a possibility that we might face type errors. For example, if we try to install ANTD package as part of your solution and use one of its components and try to build the solution. You might be seeing the type errors (TS1005,TS1109) as shown below. Typescript errors How to find out the typescript version? When you build (Gulp build) the SPFX solution you will be able to find out the typescript version from the build log as shown below.  Steps for updating the Typescript version. In SPFX solution typescript version has dependency on the @Microsoft/rush-stack-compiler package version.  In the package.json if you have @microsoft/rush-stack-compiler-2.7 then the typescript version is 2.7.x.    In the SPFX solution deleted the node_modules folder. It will remove all the packages that are installed.  On the package.js

Use of Expression in Microsoft flow through an example

Most of the basic flows can be configured using the templates provided by Microsoft. But in real time we will be developing flows which have complexity. Expression play an important role in fulfilling the requirements. Scenario : -  We have project details list which has the below columns, we need to intimate the project team before 2 days of the end date. (Please note that this scenario is picked up from one of the Microsoft insight demo) The following points needs to be implemented ·          Only title and End time are mandatory fields, Start time if not entered needs to be taken as the current date. ·          Number of days is the difference between the start time and end time.   ·          Email needs to be sent before 2 days of the end date. ·          If any attachments they need to be attached to the mail being send from the flow. Below is the complete flow. If any one needs a copy of the complete flow please drop a comment. Let m