Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2764 from gautamdsheth/fixUpdateRequests
Browse files Browse the repository at this point in the history
Fix: Added If-Match header for HTTP merge requests
  • Loading branch information
erwinvanhunen authored Jul 7, 2020
2 parents 342f705 + 9270c58 commit 50d200e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Changed
- Updated implementation of `Move-PnPFile` to now also support moving of files and folders accross site collections [PR #2749](https://github.com/pnp/PnP-PowerShell/pull/2749)
- Fixed issue where using `Disconnect-PnPOnline -Connection $variable` after having connected using `$variable = Connect-PnPOnline -CertificatePath <path> -ReturnConnection`, it would not clean up the certificate on that connection instance passed in by $variable, but instead try to do it on the current connection context [PR #2755](https://github.com/pnp/PnP-PowerShell/pull/2755)
- Fixed `Invoke-PnPSPRestMethod -Method Merge` not passing in the `If-Match: *` header and thereby causing failed requests [PR #2764](https://github.com/pnp/PnP-PowerShell/pull/2764)
- If a certain PnP PowerShell cmdlet needs access to the SharePoint Admin Center site in order to function correctly, it will now list this in the Synopsis section of the Get-Help for the cmdlet
- Fixed issue where using `Connect-PnPOnline` using `-Thumbnail` would delete the private key on some devices when running `Disconnect-PnPOnline` [PR #2759](https://github.com/pnp/PnP-PowerShell/pull/2759)
- Updated test project structure [PR #2767](https://github.com/pnp/PnP-PowerShell/pull/2767)
Expand Down
24 changes: 11 additions & 13 deletions Commands/Base/InvokeSPRestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ public class InvokeSPRestMethod : PnPSharePointCmdlet
[Parameter(Mandatory = false, Position = 0, HelpMessage = "The Http method to execute. Defaults to GET.")]
public HttpRequestMethod Method = HttpRequestMethod.Get;

[Parameter(Mandatory = true, Position = 0, HelpMessage = "The url to execute.")]
[Parameter(Mandatory = true, Position = 0, HelpMessage = "The url to execute")]
public string Url;

[Parameter(Mandatory = false, HelpMessage = "A string or object to send")]
public object Content;

[Parameter(Mandatory = false, HelpMessage = "The content type of the object to send. Defaults to 'application/json'")]
[Parameter(Mandatory = false, HelpMessage = "The content type of the object to send. Defaults to 'application/json'.")]
public string ContentType = "application/json";

protected override void ExecuteCmdlet()
{

if (Url.StartsWith("/"))
{
// prefix the url with the current web url
Expand All @@ -70,16 +69,14 @@ protected override void ExecuteCmdlet()
var accessToken = this.ClientContext.GetAccessToken();
var method = new HttpMethod(Method.ToString());

//var method = new HttpMethod(Method.ToString().ToUpper());
using (var handler = new System.Net.Http.HttpClientHandler())
using (var handler = new HttpClientHandler())
{
// we're not in app-only or user + app context, so let's fall back to cookie based auth
if (String.IsNullOrEmpty(accessToken))
if (string.IsNullOrEmpty(accessToken))
{
SetAuthenticationCookies(handler, ClientContext);
}


using (var httpClient = new PnPHttpProvider(handler))
{
var requestUrl = Url;
Expand All @@ -94,9 +91,14 @@ protected override void ExecuteCmdlet()
request.Headers.Add("X-HTTP-Method", "MERGE");
}

if (Method == HttpRequestMethod.Merge || Method == HttpRequestMethod.Delete)
{
request.Headers.Add("IF-MATCH", "*");
}

if (!string.IsNullOrEmpty(accessToken))
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
else
{
Expand All @@ -105,7 +107,7 @@ protected override void ExecuteCmdlet()
handler.Credentials = networkCredential;
}
}
request.Headers.Add("X-RequestDigest", (ClientContext as ClientContext).GetRequestDigest().GetAwaiter().GetResult());
request.Headers.Add("X-RequestDigest", ClientContext.GetRequestDigest().GetAwaiter().GetResult());

if (Method == HttpRequestMethod.Post)
{
Expand Down Expand Up @@ -140,7 +142,6 @@ protected override void ExecuteCmdlet()
}
}
}

}

private void SetAuthenticationCookies(HttpClientHandler handler, ClientContext context)
Expand Down Expand Up @@ -175,7 +176,6 @@ private void SetAuthenticationCookies(HttpClientHandler handler, ClientContext c
}
}


//Taken from "Remote Authentication in SharePoint Online Using the Client Object Model"
//https://code.msdn.microsoft.com/Remote-Authentication-in-b7b6f43c

Expand All @@ -202,7 +202,6 @@ internal static class CookieReader
/// <returns>Returns Cookie contents as a string</returns>
public static string GetCookie(string url)
{

int size = 512;
StringBuilder sb = new StringBuilder(size);
if (!NativeMethods.InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
Expand All @@ -222,7 +221,6 @@ public static string GetCookie(string url)

private static class NativeMethods
{

[DllImport("wininet.dll", EntryPoint = "InternetGetCookieEx", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool InternetGetCookieEx(
string url,
Expand Down

0 comments on commit 50d200e

Please sign in to comment.