Skip to content

Commit 2e0e1d5

Browse files
committed
Updating REST examples to use OAuth.
1 parent 992a94e commit 2e0e1d5

File tree

4 files changed

+75
-33
lines changed

4 files changed

+75
-33
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
---
2-
page_type: sample
3-
products:
4-
- office-project
5-
- office-365
6-
languages:
7-
- powershell
8-
extensions:
9-
contentType: samples
10-
platforms:
11-
- REST API
12-
createdDate: 11/23/2015 8:07:26 PM
13-
---
1+
---
2+
page_type: sample
3+
products:
4+
- office-project
5+
- office-365
6+
languages:
7+
- powershell
8+
extensions:
9+
contentType: samples
10+
platforms:
11+
- REST API
12+
createdDate: 11/23/2015 8:07:26 PM
13+
---
1414
# Project REST Basic Operations
1515

1616
The Project REST Basic Operations demonstrates how to create and update a project using REST API
1717

1818
###Prerequisites
1919
To use this Project Online REST code sample, you need the following:
2020
* An Office 365 tenant with a Project license
21-
* Project CSOM client library. It is available as a Nuget Package from [here](https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/)
2221
* PowerShell v4.0
22+
* Latest ADAL.NET library. It is available as a Nuget Package from [here](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/)
23+
* For the files downloaded, please run "Unblock-File *" to unblock accessing the file.
2324

2425
###Modules
2526
* [CreateProject](/createproject.ps1)

ReST.ps1

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,43 @@
33
See LICENSE in the project root for license information.
44
#>
55

6+
$ErrorActionPreference = "Stop" # http://technet.microsoft.com/en-us/library/dd347731.aspx
7+
Set-StrictMode -Version "Latest" # http://technet.microsoft.com/en-us/library/dd347614.aspx
8+
69
# PS helper methods to call ReST API methods targeting Project Online tenants
7-
$global:fedAuthTicket = ''
10+
$global:accessHeader = ''
811
$global:digestValue = ''
912

13+
[Reflection.Assembly]::LoadFrom("$($PSScriptRoot)\Microsoft.IdentityModel.Clients.ActiveDirectory.dll") | Out-Null
14+
15+
function Get-AADAuthToken([Uri] $Uri)
16+
{
17+
# NOTE: Create an azure app and update $clientId and $redirectUri below
18+
$clientId = ""
19+
$redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
20+
21+
$authority = "https://login.microsoftonline.com/common"
22+
$resource = $Uri.GetLeftPart([System.UriPartial]::Authority);
23+
24+
$promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always
25+
$platformParam = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList $promptBehavior
26+
$authenticationContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority, $False
27+
$authenticationResult = $authenticationContext.AcquireTokenAsync($resource, $clientId, $redirectUri, $platformParam).Result
28+
29+
return $authenticationResult
30+
}
31+
1032
function Set-SPOAuthenticationTicket([string] $siteUrl)
1133
{
12-
$username = "[email protected]"
13-
Write-Host 'Enter password for user' $username 'on site' $siteUrl
14-
$password = Read-Host -AsSecureString
15-
16-
# load the SP client runtime code
17-
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
18-
$onlineCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
19-
if ($onlineCredentials -ne $null)
34+
$siteUri = New-Object Uri -ArgumentList $siteUrl
35+
36+
$authResult = Get-AADAuthToken -Uri $siteUri
37+
if ($authResult -ne $null)
2038
{
21-
$global:fedAuthTicket = $onlineCredentials.GetAuthenticationCookie($SiteUrl, $true).TrimStart('SPOIDCRL=')
39+
$global:accessHeader = $authResult.AccessTokenType + " " + $authResult.AccessToken
2240
}
2341

24-
if ([String]::IsNullOrEmpty($global:fedAuthTicket))
42+
if ([String]::IsNullOrEmpty($global:accessHeader))
2543
{
2644
throw 'Could not obtain authentication ticket based on provided credentials for specified site'
2745
}
@@ -45,13 +63,9 @@ function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method
4563
$req.ContentLength = $body.Length
4664
$req.ContentType = "application/json"
4765
}
48-
49-
$domain = (New-Object System.Uri($url)).Authority
50-
$cookies = New-Object System.Net.CookieContainer
51-
$fedCookie = New-Object System.Net.Cookie 'SPOIDCRL', $global:fedAuthTicket, "", $domain
52-
$cookies.Add($fedCookie)
53-
54-
$req.CookieContainer = $cookies
66+
67+
# set Authorization header
68+
$req.Headers.Add("Authorization", $global:accessHeader)
5569

5670
if (-not $isDigestRequest)
5771
{

getprojects.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<#
2+
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3+
See LICENSE in the project root for license information.
4+
#>
5+
6+
7+
# Get list of projects using OData/CSOM ReST API
8+
param
9+
(
10+
# SharepointOnline project site collection URL
11+
$SiteUrl = $(throw "SiteUrl parameter is required")
12+
)
13+
# Load ReST helper methods
14+
. .\ReST.ps1
15+
16+
# Set up the request authentication
17+
Set-SPOAuthenticationTicket $siteUrl
18+
Set-DigestValue $siteUrl
19+
20+
# Get list of projects using OData ReST API
21+
"OData"
22+
Get-ReSTRequest $SiteUrl "ProjectData/Projects"
23+
24+
"CSOM"
25+
# Get list of projects using CSOM ReST API
26+
Get-ReSTRequest $SiteUrl "ProjectServer/Projects"

updateprojectcustomfieldvalues.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
See LICENSE in the project root for license information.
44
#>
55

6-
# Updates a Project Custom field value using ReST API
6+
7+
# Updates a Project Custom field value using ReST API
78
param
89
(
910
# SharepointOnline project site collection URL

0 commit comments

Comments
 (0)