Lua Requires Chilkat v11.0.0+
Lua
Create Group
See more Microsoft Group Examples
Create a new group as specified in the request body. You can create one of three types of groups:- Office 365 Group (unified group)
- Dynamic group
- Security group
This operation returns by default only a subset of the properties for each group. These default properties are noted in the Properties section.
See https://docs.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0 for more information.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Use your previously obtained access token as shown here:
-- Get Microsoft Graph OAuth2 Access Token with Group.ReadWrite.All scope.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/msGraphGroup.json")
if not success then
print(jsonToken.last_error_text)
return
end
http.auth_token = jsonToken:string_of("access_token")
-- Create a JSON body for the HTTP POST
-- Use this online tool to generate the code from sample JSON:
-- Generate Code to Create JSON
-- {
-- "description": "Self help community for library",
-- "displayName": "Library Assist",
-- "groupTypes": [
-- "Unified"
-- ],
-- "mailEnabled": true,
-- "mailNickname": "library",
-- "securityEnabled": false
-- }
local json = chilkat.JsonObject.new()
json:update_string("description", "Self help community for library")
json:update_string("displayName", "Library Assist")
json:update_string("groupTypes[0]", "Unified")
json:update_bool("mailEnabled", true)
json:update_string("mailNickname", "library")
json:update_bool("securityEnabled", false)
-- POST the JSON to https://graph.microsoft.com/v1.0/groups
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://graph.microsoft.com/v1.0/groups", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
json:load(resp.body_str)
json.emit_compact = false
if resp.status_code ~= 201 then
print(json:emit())
print("Failed, response status code = " .. resp.status_code)
return
end
print(json:emit())
-- A sample response:
-- (See code for parsing this response below..)
-- {
-- "id": "b320ee12-b1cd-4cca-b648-a437be61c5cd",
-- "deletedDateTime": null,
-- "classification": null,
-- "createdDateTime": "2018-12-22T00:51:37Z",
-- "creationOptions": [],
-- "description": "Self help community for library",
-- "displayName": "Library Assist",
-- "groupTypes": [
-- "Unified"
-- ],
-- "mail": "library7423@contoso.com",
-- "mailEnabled": true,
-- "mailNickname": "library",
-- "onPremisesLastSyncDateTime": null,
-- "onPremisesSecurityIdentifier": null,
-- "onPremisesSyncEnabled": null,
-- "preferredDataLocation": "CAN",
-- "proxyAddresses": [
-- "SMTP:library7423@contoso.com"
-- ],
-- "renewedDateTime": "2018-12-22T00:51:37Z",
-- "resourceBehaviorOptions": [],
-- "resourceProvisioningOptions": [],
-- "securityEnabled": false,
-- "visibility": "Public",
-- "onPremisesProvisioningErrors": []
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local strVal = ""
local id = json:string_of("id")
local deletedDateTime = json:string_of("deletedDateTime")
local classification = json:string_of("classification")
local createdDateTime = json:string_of("createdDateTime")
local description = json:string_of("description")
local displayName = json:string_of("displayName")
local mail = json:string_of("mail")
local mailEnabled = json:bool_of("mailEnabled")
local mailNickname = json:string_of("mailNickname")
local onPremisesLastSyncDateTime = json:string_of("onPremisesLastSyncDateTime")
local onPremisesSecurityIdentifier = json:string_of("onPremisesSecurityIdentifier")
local onPremisesSyncEnabled = json:string_of("onPremisesSyncEnabled")
local preferredDataLocation = json:string_of("preferredDataLocation")
local renewedDateTime = json:string_of("renewedDateTime")
local securityEnabled = json:bool_of("securityEnabled")
local visibility = json:string_of("visibility")
local i = 0
local count_i = json:size_of_array("creationOptions")
while i < count_i do
json.i = i
i = i + 1
end
i = 0
count_i = json:size_of_array("groupTypes")
while i < count_i do
json.i = i
strVal = json:string_of("groupTypes[i]")
i = i + 1
end
i = 0
count_i = json:size_of_array("proxyAddresses")
while i < count_i do
json.i = i
strVal = json:string_of("proxyAddresses[i]")
i = i + 1
end
i = 0
count_i = json:size_of_array("resourceBehaviorOptions")
while i < count_i do
json.i = i
-- ...
i = i + 1
end
i = 0
count_i = json:size_of_array("resourceProvisioningOptions")
while i < count_i do
json.i = i
-- ...
i = i + 1
end
i = 0
count_i = json:size_of_array("onPremisesProvisioningErrors")
while i < count_i do
json.i = i
-- ...
i = i + 1
end
print("Success.")