SharePoint List data access demo
I need to demonstrate reading data from SharePoint Online lists externally.
Setup the SharePoint tenant
- Create an "events" list - to see this we can get the lists schema using https://gallery.technet.microsoft.com/office/Export-Lists-Schema-as-XML-c472d82c
- I used some PowerShell and ShareGate to migrate a list and its existing content
$srcsite = Connect-Site -url $srcurl -UserName "$AdminAccount" -Password $password
$destsite = Connect-Site -url $desturl -UserName $o365Acct -Password $destpassword
Copy-List -Name $destListName -SourceSite $srcsite -DestinationSite $destsite -InsaneMode
- Check the list design using pnp- i.e check columns and datatypes
Connect-PnPOnline -Url $SiteCollection -Credentials $Credentials
Get-PnPProvisioningTemplate -Out template.xml -Handlers Lists
Configure the App access
Clientid | 00000000-84fb-42f2-9dca-000000000000 |
ClientSecret | XXXXXXXXXXXXXXXXXXXXXXXXXXXX= |
App | MyAppyApp |
App domain | foo.com |
App | https://foo.com |
Then grant permissions to the app using the tenancy URI:
https://{tenant}.sharepoint.com/_layouts/15/appinv.aspx
and supply the clientid
Add the permissions xml, for reading lists..
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read"/>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/>
</AppPermissionRequests>
Re-use some managed code as depicted on the Patterns and Practices site
Whilst running that, use fiddler to get the bearer token.
Copy the bearer token to your the JavaScript Ajax snippet. Run the snippet from web server.
var siteurl = "https://{YOURTENANT}.sharepoint.com";
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('Events')/items",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose",
"authorization": "Bearer *{BEARER TOKEN HERE}* "
},
success: function (data) {
if (data.d.results.length > 0) {
console.log('in doSuccess');
console.log(data.d.results.length);
for (var i = 0; i < data.d.results.length; i++) {
var item = data.d.results[i];
debugger
console.log(item.Title + item.Detail + item.br6c + item.m4eh);
$('#demo').createTable(data.d.results);
}
}
},
error: function (data) {
alert("Error: " + data);
}
});
See https://gist.github.com/tristian2/0d29d5331227ca1f84371c2d023cd112.js
Obviously, this not a viable approach for the final application, but it does demonstrate how the endpoints, oAuth and REST work in order the interact with a SharePoint tenancy and its data. For the production solution, you will need to work with the proscribed mechanisms recommended by Microsoft.