Create a group folder in Nextcloud
One script reply has been approved by the moderators Verified

Create a group folder in a Nextcloud. Needs the Group folders app installed. This is only for creating, not changing, it will create a new folder with every use

Created by nextcloud 426 days ago Picked 1 time
Submitted by nextcloud Bun
Verified 21 days ago
1
/*
2
 * We expect that the group comes from a Nextcloud Table where you can
3
 * define a datatype "Users and groups". The result is a list of groups,
4
 * each list item consist of the fields "id", "type" and "displayName". 
5
 * This script uses the "id" value of each list item.
6
 */
7
import createClient, { type Middleware } from "openapi-fetch";
8

9
export async function main(
10
  nextcloud: RT.Nextcloud,
11
  folderName: string,
12
  groups: Array<{
13
    id: string,
14
    key: string,
15
    type: number,
16
    displayName: string,
17
  }>,
18
  quota: number,
19
) {
20

21
  const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
22
  const authMiddleware: Middleware = {
23
    async onRequest({ request, options }) {
24
      // fetch token, if it doesn’t exist
25
      // add Authorization header to every request
26
      request.headers.set("Authorization", `Basic ${btoa(nextcloud.userId + ':' + nextcloud.token)}`);
27
      return request;
28
    },
29
  };
30
  client.use(authMiddleware);
31

32

33
  try {
34
    const resp = await client.POST("/index.php/apps/groupfolders/folders", {
35
      params: {
36
        header: {
37
          "OCS-APIRequest": true,
38
        },
39
        query: {
40
          format: "json",
41
        },
42
      },
43
      body: {
44
        mountpoint: folderName
45
      },
46
    });
47

48
    const tableId = resp.data.ocs.data.id;
49

50
    for (const group of groups) {
51
        await client.POST("/index.php/apps/groupfolders/folders/{id}/groups", {
52
          params: {
53
            header: {
54
              "OCS-APIRequest": true,
55
            },
56
            query: {
57
              format: "json",
58
            },
59
            path: {
60
              id: tableId,
61
            }
62
          },
63
          body: {
64
            group: group.id
65
          },
66
        });
67
    }
68

69
    await client.POST("/index.php/apps/groupfolders/folders/{id}/quota", {
70
      params: {
71
        header: {
72
          "OCS-APIRequest": true,
73
        },
74
        query: {
75
          format: "json",
76
        },
77
        path: {
78
          id: tableId,
79
        }
80
      },
81
      body: {
82
        quota: quota
83
      },
84
    });
85

86
  } catch (e) {
87
    console.debug('error', e)
88
  }
89
}
90