Thursday, April 13, 2023

How to access sharepoint items using powershell


We can access sharepoint items\sites using following powershell modules.
 
 1. SPO (SharepointPnpPowershellOnline) Module 
 
 2. PnP.Powershell module 
 
SPO (sharepointPnpPowershellOnlin) Module Vs PnP.Powershell module

1. PnP PowerShell Cmdlets works in the context of the current user, where as SPO (sharepointPnpPowershellOnline command) runs with the Tenant Admin rights. 

2. PnP PowerShell Cmdlets connects to the SiteCollection level, where as SPO has the commands to target Tenant level.

3. SPO module works only in SharePoint Online where as pnp. powershell is a cross platform library.

4. SPO is legecy version of PnP Powershell

5. What are the advantages of PnP in SharePoint?

6. The main advantage of using SharePoint PnP is, It reduced the code to get the required information by using only a single line of code so you can retrieve the object, once the client context is set. 
  
7. By using PnP in SharePoint Online development, developers will be more efficient and productive.

Upgrade SPO module to PnP Powershell module

```sh
$SPOModule=Get-InstalledModule  sharepointPnpPowershellOnline  -ErrorAction SilentlyContinue 

if($SPOModule  -ne $null){ Uninstall-Module  sharepointPnpPowershellOnline -Force -ErrorAction SilentlyContinue}

$PnPModule= Get-InstalledModule PnP.Powershell -ErrorAction SilentlyContinue

if($PnPModule -eq $null) { Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber }
```

Note: Dont install latest PnP.Powershell with version 2.1.1. there is problem with this version
Problem:
  		Import-Module : Could not load file or assembly 'System.Management.Automation] Issue Symptom: unable to run PnP PowerShell commands using PowerShell ISE in administrative mode. Getting following error
    
    	Exception: Import-Module : Could not load file or assembly 'System.Management.Automation, Version=7.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Solution:

1. Uninstall all versions and then install correct 1.12.0 version because Newer version (2.1.1) of PnP PowerShell  has issues #1. This issue happened with newer version PnP PowerShell 2.1.1.
  
```sh 
  Uninstall-Module PnP.PowerShell 
```

2. Install PnP.PowerShell with version 1.12.0

```sh 
   Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber
```


Following is the powershell script to upload files to sharepoint from local drive


```sh

Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber 

#Ex:- $URL="https://{tenant-name}.sharepoint.com/sites/{site-name}"

$URL="https://contso.sharepoint.com/sites/Pre-Screening"

$SourcePath="D:\Pre-Screening-DocLib"
$DestinationPath="Shared Documents/General/Pre-Screening"

#Connect to sharepoint

Connect-PnPOnline -Url $URL -Interactive -UseWeblogin:$true

## upload local files Sharepoint  

gci $SourcePath |ForEach-Object  -Process {Add-PnPFile -Path $_.FullName -Folder $DestinationPath -Values @{Modified=$_.LastWriteTime}}

```

No comments:

Post a Comment