1

Is it possible to interact with SharePoint online using PowerShell and CSOM (or any other technology) using regular user credentials (i.e. non-o365 admin credentials). I've searched throught the internets and it looks like it is only possible for administrators. This would mean that everything in a SharePoint online site could be done by hand, click by click. Am I right?

1

You could use CSOM with PowerShell to access SharePoint online.

Here is sample script for your reference:

Add-Type –Path (Resolve-Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type –Path (Resolve-Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

$UserName = "lee@xxx.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter password" -AsSecureString    
$Url = "https://xxx.sharepoint.com/sites/sts"


#$Context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
#$items = Get-ListItems -Context $Context -ListTitle "EmployeeInfo" 

$spoContext = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$listTitle = "EmployeeInfo"

#$password = Read-Host -Prompt "Enter password" -AsSecureString
#$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("M.okoro-ogbobe@shell.com", $password)

$web = $spoContext.Web

$List = $web.Lists.GetByTitle($listTitle)
$a = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery() 
$items = $List.GetItems( $a )

$spoContext.Load($web) 
$spoContext.Load($items)
#execute query 
$spoContext.ExecuteQuery()

Function Get-SPOContext([string]$Url,[string]$UserName,$Password)
{
    write-host Get-SPOContext
    #$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password) #SecurePassword
    return $Context
}

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    write-host Get-ListItems
    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(13,'ID','Created','Modified','Title')
    $items = $list.GetItems($qry)
    $Context.Load($list)
    $Context.Load($items)
    $Context.ExecuteQuery()
    return $items 
}
1

it's possible to use CSOM for SPO, the code should be the same except the credential part, you can just use regular user. For SSOM you need to use SharePoint Online PowerShell, the code is different, and it requires administrator credential.

Here is the sample how to use CSOM for SPO

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
   if([string]::IsNullOrEmpty($Password)) {
      $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString 
   }
   else {
      $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   }
   return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}

$UserName = "xxx@xxx";
$Password = Read-Host -Prompt "Enter the password";
$WebUrl = "https://xxx.sharepoint.com/sites/xxx";

Write-Host "Create Context"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
Write-Host "Initialize credential"
$Context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password

$web = $Context.Web;
$Context.Load($web.AllProperties);
$Context.ExecuteQuery();

//some code here

$Context.Dispose()
  • Ok, so this requires admin credentials, which implies that me, a regular user, can't programmatically interact with SharePoint? – Roberto Feb 22 '18 at 17:31
  • as long as the user account have permission on object you are working on, it should be able to interact – Eric Mar 7 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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