SharePoint Online is a wide spread solution for collaboration. Actual versions of Dynamics AX / 365 provide great integration capabilities and for all other purpose there exists the SharePoint Online Client API. However, legacy applications may not support the required libraries or .NET framework versions to use the SharePoint Online Client API. One way to overcome this issue is to utilize the REST API and communicate via HTTP. Here you can find a step by step guide how to provide an external application access to SharePoint Online via an App: http://www.ktskumar.com/2017/01/access-sharepoint-online-using-postman/
To interact with SharePoint Online you need to generate a token.This is done by sending an HTTP POST requiest with client ID, secret, and desired resource to SharePoint Online. Here is the Code in X++ written in Ax 2009.
str clientID = "<YOUR_CLIENT_APP_ID>";
str secret = "<YOUR_SECRET>";
str tenant = "<YOUR_TENANT>";
str ctx = "00000003-0000-0ff1-ce00-000000000000"; //i.e. SharePoint
str sp = "<YOUR_SITE>.sharepoint.com";
str listName = "<NAME_OF_YOUR_LIST>"; // e.g. Customers
str listUrl = .sharepoint.com/_api/web/lists/GetByTitle(‚"+listName+"‘)/items">.sharepoint.com/">.sharepoint.com/_api/web/lists/GetByTitle(‚"+listName+"‘)/items">https://<YOUR_SITE>.sharepoint.com/
_api/web/lists/GetByTitle(‚"+listName+"‘)/items;
str url = "https://accounts.accesscontrol.windows.net/"
+tenant+"/tokens/OAuth/2";
System.Net.WebClient cl = new System.Net.WebClient();
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
System.Collections.Specialized.NameValueCollection body = new System.Collections.Specialized.NameValueCollection();
System.Byte[] response;
System.Text.Encoding enc = System.Text.Encoding::get_UTF8();
System.String responseText;
str xppResponse;
int indexOf;
int start;
int totalLength;
int length;
str token;
;
headers.Add("Content-Type","application/x-www-form-urlencoded");
body.Add("grant_type","client_credentials");
body.Add("client_id",clientID+"@"+tenant);
body.Add("client_secret",secret);
body.Add("resource",ctx+"/"+sp+"@"+tenant);
cl.set_Headers(headers);
response = cl.UploadValues(url,"POST",body);
responseText = enc.GetString(response);
// cut token out of response text
indexOf = responseText.IndexOf("access_token");
start = indexOf + 15;
totalLength = responseText.get_Length();
length = totalLength – start – 2;
xppResponse = responseText;
token = responseText.Substring(start,length);
Query List Elements
The token has a valid timestamp measured in Unix Ticks. So it might be good idea to store the token in a parameter table and only request a new one if it is or will expire soon. However, in this example I go on and query the Customer List.
// .. Code from above
cl = new System.Net.WebClient();
headers = new System.Net.WebHeaderCollection();
headers.Add("Accept","application/atom+xml");
headers.Add("Authorization","Bearer "+token);
cl.set_Headers(headers);
body.Clear();
url = listUrl;
responseText = cl.DownloadString(listUrl);
xppResponse = responseText;
info(xppResponse);
In this case the response type is an XML. You may parse the XML and only view e.g. the Title Tag or any other fields your are interested in. Here is the Infolog of the complete XML response from the SharePoint Online List.