Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

JSON

Explore SharePoint REST API with Chrome F12 cURL (bash) to Postman

Wanted to create demo for how to leverage F12 tools to capture SharePoint Online REST api calls and replay into REST Postman.   Allows adjustment of HTTP headers, inspecting output, and execute test API calls.  Postman provides benefits of storing history, export data, and fine tuning the API headers.

Leveraging Chrome F12 enables us to quickly create a valid HTTP POST with correct authentication HTTP  headers, cookies, and endpoint URL.  Cheers.

shades_smile

Video

Screenshot

image
image

Reference

VIDEO – PowerApps – Super Gallery

Working with Gallery control in PowerApps can be a limiting way to navigate records.   Many PowerApps include multiple screens with dedicated Gallery screens for navigation.   Helping users locate records (sort, filter, scroll, and search) would be better accomplished with SharePoint Views.   SharePoint List Views are more mature with full features for ad-hoc sort, filter, search and ability to save query (shared/personal) and provide hyperlinks to view ASPX pages.

Why not use SharePoint Views instead of Gallery control?

Turns out we can.   On SharePoint modern pages:

  1. Create new custom column with simple formula “=[ID]”
  2. Apply JSON custom formatting to generate link <A HREF=”…power app URL..ID=1”> which opens PowerApps, giving ID number for a single record to Edit
  3. Share View URL with end users to navigate records (sort/filter/search/paging)

This gives a robust and familiar navigation experience.  Use PowerApps for it’s strength (input validation) and use SharePoint list for it’s strength (navigate records).  Cheers. 

shades_smile

Video

PowerApps – Super Gallery from Jeff Jones on Vimeo.

Screenshots

image
image
image

References

Code – GitHub Repo

Code – SuperGallery-Modern-VIEW-FORM-column

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "style": {
        "color": "white",
        "display": "inline-block",
        "padding": "4px",
        "background-color": "purple"
      },
      "attributes": {
        "target": "_blank",
        "href": "='https://apps.powerapps.com/play/015ba39a-db14-4d5f-80a9-c23881784383?tenantId=19e61d8c-a318-488b-8d4e-33bc232b3530&source=portal&screenColor=rgba(0%2C%20176%2C%20240%2C%201)&ID=' + [$ID]"
      },
      "children": [
        {
          "elmType": "span",
          "txtContent": "VIEW FORM"
        }
      ]
    }
  ]
}

FIXED – Cannot convert the JSON string … contains the duplicated keys ‘Id’ and ‘ID’

Recently I came across the error message below when download a SharePoint 2013 List item REST api call to PowerShell.  The cmdlet ConvertFrom-JSON does an excellent job.  However, it is case sensitive with property names.

The resolution I found was to first string replace (case sensitive) to rename one of the source property names.  Cheers! 

shades_smile

Screenshot – Before

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys ‘Id’ and ‘ID’.

image

Screenshot – After

image

Code

cls

# Config
$web = "http://portal/sites/team"
$list = "Test"

# Digest
$urlView = "$web/_api/contextinfo"
$urlView
$req = Invoke-WebRequest $urlView -UseDefaultCredentials -UseBasicParsing -Method Post
$ctx = $req.Content
$digest = $ctx.GetContextWebInformation.FormDigestValue
$digest

# List Items
$urlItems = "$web/_api/web/lists/GetByTitle('$list')/getitems"
$json = "application/json; odata=verbose"
$headers = @{"Accept" = $json; "Content-Type" = $json; "X-RequestDigest" = $digest}
$body = '{"query":{}}'
$req = Invoke-WebRequest $urlItems -UseDefaultCredentials -UseBasicParsing -Method Post -Body $body -Headers $headers
$resp = $req.Content

### FIX - Case Sensitive Property Name Replacement ###
$resp = $resp.Replace("Id","Idd")

$json = $resp  | ConvertFrom-Json
$json

References

VIDEO – HTTP Header Client Secret protected Web API

Watch the video below to see a demo of protecting WebAPI with HTTP header and a Client Secret.    By default, new Web API projects lack any security mechanism and are open to any anonymous user.    Protecting Dot Net methods with an IF() statement condition provides a simple security mechanism to ensure only users who know the Client Secret are able to run the API and execute the method.

NOTE – Check out https://www.spjeff.com/2017/10/05/video-azure-ad-protected-web-api-in-an-angularjs-spa/ for more complete WebAPI security with Azure AD.

Cheers!

shades_smile

Video

Screenshots

image

Code

public bool keyMatch()
{
	// security HTTP header
	string key = "12345678901234567890123456789012345678901234567890";
	IEnumerable headerValues;
	var keyFilter = string.Empty;
	if (Request.Headers.TryGetValues("key", out headerValues))
	{
		// ALLOW - match key
		keyFilter = headerValues.FirstOrDefault();
	}
	if (keyFilter == key)
	{
		return true;
	}
	else
	{
		return false;
	}
}

References

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲