forked from OfficeDev/Project-REST-Basic-Operations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReST.ps1
More file actions
165 lines (144 loc) · 4.89 KB
/
Copy pathReST.ps1
File metadata and controls
165 lines (144 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<#
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
See LICENSE in the project root for license information.
#>
$ErrorActionPreference = "Stop" # http://technet.microsoft.com/en-us/library/dd347731.aspx
Set-StrictMode -Version "Latest" # http://technet.microsoft.com/en-us/library/dd347614.aspx
# PS helper methods to call ReST API methods targeting Project Online tenants
$global:accessHeader = ''
$global:digestValue = ''
[Reflection.Assembly]::LoadFrom("$($PSScriptRoot)\Microsoft.Identity.Client.dll") | Out-Null
function Get-AuthToken([Uri] $Uri)
{
# NOTE: Create an azure app and update $clientId,$tenantId and $redirectUri below
$clientId = ""
$tenantId = ""
$redirectUri = ""
# user's PJO login account
$user = ""
$scopes = New-Object System.Collections.Generic.List[string]
# Project.Write Permission scope for app eg:"https://contoso.sharepoint.com/Project.Write"
$writeScope = ""
$scopes.Add($writeScope)
$pcaConfig = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($clientId).WithTenantId($tenantId).WithRedirectUri($redirectUri);
$authenticationResult = $pcaConfig.Build().AcquireTokenInteractive($scopes).WithPrompt([Microsoft.Identity.Client.Prompt]::NoPrompt).WithLoginHint($user).ExecuteAsync().Result;
return $authenticationResult
}
# Gets Auth Token using MSAL library.
function Set-SPOAuthenticationTicket([string] $siteUrl)
{
$siteUri = New-Object Uri -ArgumentList $siteUrl
$authResult = Get-AuthToken -Uri $siteUri
if ($authResult -ne $null)
{
$global:accessHeader = $authResult.CreateAuthorizationHeader()
}
if ([String]::IsNullOrEmpty($global:accessHeader))
{
throw 'Could not obtain authentication ticket based on provided credentials for specified site'
}
}
function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method, [string]$body = $null)
{
$url = ([string]$siteUrl).TrimEnd("/") + "/_api/" + $endpoint
$req = [System.Net.WebRequest]::Create($url)
$req.Timeout = 120000
$req.Method = $method
[bool]$isReadOnly = (('GET','HEAD') -contains $req.Method)
[bool]$isDigestRequest = $endpoint -contains 'contextinfo'
if ([String]::IsNullOrEmpty($body))
{
$req.ContentLength = 0;
}
else
{
$req.ContentLength = $body.Length
$req.ContentType = "application/json"
}
# set Authorization header
$req.Headers.Add("Authorization", $global:accessHeader)
# handle ETag
$req.Headers.Add("If-Match", "*")
if (-not $isDigestRequest)
{
if (-not $isReadOnly)
{
$req.Headers.Add("X-RequestDigest", $global:digestValue)
}
}
if (-not [String]::IsNullOrEmpty($body))
{
$writer = New-Object System.IO.StreamWriter $req.GetRequestStream()
$writer.Write($body)
$writer.Close()
$writer.Dispose()
}
return $req
}
function Set-DigestValue([string]$siteUrl)
{
$request = Build-ReSTRequest $siteUrl 'contextinfo' 'POST' $null
if ($request -eq $null)
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
try
{
$resp = $request.GetResponse()
$reader = [System.Xml.XmlReader]::Create($resp.GetResponseStream())
if ($reader.ReadToDescendant("d:FormDigestValue"))
{
$global:digestValue = $reader.ReadElementContentAsString()
}
else
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
}
finally
{
if ($reader -ne $null)
{
$reader.Close()
$reader.Dispose()
}
if ($resp -ne $null)
{
$resp.Close()
$resp.Dispose()
}
}
}
function Post-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body = $null)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'POST' $body
$resp = $request.GetResponse()
if ($resp -ne $null)
{
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream()
$reader.ReadToEnd()
$reader.Dispose()
}
}
function Patch-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'PATCH' $body
$resp = $request.GetResponse()
if ($resp -ne $null)
{
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream()
$reader.ReadToEnd()
$reader.Dispose()
}
}
function Get-ReSTRequest([string]$siteUrl, [string]$endpoint)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'GET'
$resp = $request.GetResponse()
if ($resp -ne $null)
{
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream()
$reader.ReadToEnd()
$reader.Dispose()
}
}