/// <summary> /// Internal private method to process an HTTP request toward the ThemeManager REST APIs /// </summary> /// <param name="context">The current ClientContext of CSOM</param> /// <param name="action">The action to perform</param> /// <param name="postObject">The body of the request</param> /// <param name="accessToken">An optional Access Token for OAuth authorization</param> /// <returns>A boolean declaring whether the operation was successful</returns> private static async Task <bool> BaseRequest(ClientContext context, ThemeAction action, Object postObject, String accessToken = null) { var result = false; context.Web.EnsureProperty(w => w.Url); #pragma warning disable CA2000 // Dispose objects before losing scope var httpClient = PnPHttpClient.Instance.GetHttpClient(context); #pragma warning restore CA2000 // Dispose objects before losing scope // Reference here: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-theming/sharepoint-site-theming-rest-api string requestUrl = $"{context.Web.Url}/_api/thememanager/{action}"; // Always make a POST request using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl)) { request.Headers.Add("ACCEPT", "application/json; odata.metadata=minimal"); request.Headers.Add("ODATA-VERSION", "4.0"); await PnPHttpClient.AuthenticateRequestAsync(request, context).ConfigureAwait(false); if (postObject != null) { var jsonBody = JsonConvert.SerializeObject(postObject); var requestBody = new StringContent(jsonBody); MediaTypeHeaderValue sharePointJsonMediaType; MediaTypeHeaderValue.TryParse("application/json;charset=utf-8", out sharePointJsonMediaType); requestBody.Headers.ContentType = sharePointJsonMediaType; request.Content = requestBody; } // Perform actual post operation HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken()); if (response.IsSuccessStatusCode) { // If value empty, URL is taken var responseString = await response.Content.ReadAsStringAsync(); if (responseString != null) { try { var responseJson = JObject.Parse(responseString); result = true; } catch { } } } else { // Something went wrong... throw new Exception(await response.Content.ReadAsStringAsync()); } } return(await Task.Run(() => result)); }
/// <summary> /// Internal private method to process an HTTP request toward the ThemeManager REST APIs /// </summary> /// <param name="context">The current ClientContext of CSOM</param> /// <param name="action">The action to perform</param> /// <param name="postObject">The body of the request</param> /// <param name="accessToken">An optional Access Token for OAuth authorization</param> /// <returns>A boolean declaring whether the operation was successful</returns> private static async Task <bool> BaseRequest(ClientContext context, ThemeAction action, Object postObject, String accessToken = null) { var result = false; // If we don't have the access token if (String.IsNullOrEmpty(accessToken)) { // Try to get one from the current context accessToken = context.GetAccessToken(); } using (var handler = new HttpClientHandler()) { context.Web.EnsureProperty(w => w.Url); // We're not in app-only or user + app context, so let's fall back to cookie based auth if (String.IsNullOrEmpty(accessToken)) { handler.SetAuthenticationCookies(context); } using (var httpClient = new PnPHttpProvider(handler)) { // Reference here: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-theming/sharepoint-site-theming-rest-api string requestUrl = $"{context.Web.Url}/_api/thememanager/{action.ToString()}"; // Always make a POST request HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl); request.Headers.Add("ACCEPT", "application/json; odata.metadata=minimal"); if (!string.IsNullOrEmpty(accessToken)) { request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); } request.Headers.Add("X-RequestDigest", await context.GetRequestDigestAsync()); request.Headers.Add("ODATA-VERSION", "4.0"); if (postObject != null) { var jsonBody = JsonConvert.SerializeObject(postObject); var requestBody = new StringContent(jsonBody); MediaTypeHeaderValue sharePointJsonMediaType; MediaTypeHeaderValue.TryParse("application/json;charset=utf-8", out sharePointJsonMediaType); requestBody.Headers.ContentType = sharePointJsonMediaType; request.Content = requestBody; } // Perform actual post operation HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken()); if (response.IsSuccessStatusCode) { // If value empty, URL is taken var responseString = await response.Content.ReadAsStringAsync(); if (responseString != null) { try { var responseJson = JObject.Parse(responseString); result = true; } catch { } } } else { // Something went wrong... throw new Exception(await response.Content.ReadAsStringAsync()); } } } return(await Task.Run(() => result)); }