Jump to content

Can't update secondaryGroups using the API


Go to solution Solved by Richard Kiessig,

Recommended Posts

Posted

I've struggled trying many different format permutations and nothing works to specify the secondaryGroups parameter in the Edit Member API call:

https://invisioncommunity.com/developers/rest-api?endpoint=core/members/POSTitem

Specifying [n,n] does NOT work. 

Does anyone have a working example to set it? Does it need to be in some particular physical format?

Posted

By poking around, I found that a member's secondary groups is stored in the 'mgroup_others' field as a comma delimited list of group ids in the core_members table. So if Invision finds it too much trouble to properly document their API or to fix bugs, I'll have to use database access to update that list, which is atrocious. 

Posted (edited)

Looks like the API input need to be an array.

if( isset( \IPS\Request::i()->secondaryGroups ) AND \is_array( \IPS\Request::i()->secondaryGroups ) )
		{
			foreach( \IPS\Request::i()->secondaryGroups as $groupId )
			{
				....

 

 

$endpoint = '/core/members/1';
$curl = curl_init( $communityUrl . 'api' . $endpoint . '?key=' . $apiKey );

$postData = [
	'secondaryGroups' => [ 7, 8 ]
];
curl_setopt_array( $curl, array(
	CURLOPT_RETURNTRANSFER	=> TRUE,
	CURLOPT_POST => TRUE,
	CURLOPT_POSTFIELDS => http_build_query( $postData ),
) );

$response = curl_exec( $curl );

image.png.f9ed23eb56cce6fa1247ee99fde4bdce.png

Edited by Martin A.
  • Solution
Posted

Thanks, Nathan and Martin. Your replies helped. I am not using PHP, I'm using C#/.NET, and Postman for testing. I think it's a deficiency of the documentation that it assumes that the API will be called from PHP.

By reading about how PHP is resolving the arrays to POST fields, I found the problem. The actual POST format of the fields is NOT actually (for example):

Field Name            Value
secondaryGroups   [7,8]

it's:
Field Name                   Value
secondaryGroups[0]      7
secondaryGroups[1]      8

This works.

I'd add that it's an annoyance that this sets and replaces any existing secondary groups. This requires reading existing ones and taking them into account, for adding new groups. Since this is an update operation, a more common use case would be to add new groups (or delete old ones).   A suggestion, which would be backwards compatible, would be to add a new field, e.g. secondaryGroupsOp, with values 'replace', 'add', 'remove'.  'replace' would be current behavior, 'add' would add the group (doing nothing if the user were already in that group), 'remove' would remove the group.

Another approach occurs to me, also (I think) backwards compatible: if a '+' is prepended to the id, then add it to the existing groups;  '-', remove it. 

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...