다음을 통해 공유


Installing App in SharePoint Online Using PowerShell - CSOM

Installing App in SharePoint Online Using PowerShell - CSOM

Summary

This TechNet Wiki is to demo PowerShell code for installing Apps in SharePoint Online.

Code

Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' -Verbose
$site = 'https://spsite.sharepoint.com'
$admin = 'ChenV@domain.onmicrosoft.com'
$password = Read-Host 'Enter Password' -AsSecureString
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials
$context.Web.LoadAndInstallApp([System.IO.File]::OpenRead('C:\Solutions\File.Apps.app'))
$context.ExecuteQuery()

Explanation

We shared the code with team and received lot of complaints.

DLL Failed to Load

Import-Module: could not load files or assemblies. The reason because team executed the code in SharePoint 2010 Management shell. Indeed that will load DLL of SharePoint 2010. So, executing this on ISE or console will do. All we need to do is pointing the correct hive. While creating this PowerShell script we used Windows 7 mahine and found 15 hive not 16.

SSL/TLS error

The Underlying connection was closed: Could not establish trust relationship for SSL/TLS secure channel. Well, this is not again PowerShell issue. The above error occured because the server is failing to validate the certificate call back. This can be fixed using below piece of code

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Reference

For more details about CSOM refer the below link
CSOM SharePoint Online Reference