0 Votes

Changes for page Script

Last modified by Ryan C on 2025/05/13 16:08

From version 13.1
edited by Ryan C
on 2025/05/13 16:08
Change comment: Changed the author of the page to fix programming rights
To version 12.1
edited by Ryan C
on 2025/05/13 16:07
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,5 +1,7 @@
1 1  {{groovy}}
2 2  import org.xwiki.model.reference.DocumentReference
3 +import org.xwiki.model.reference.EntityReference
4 +import org.xwiki.model.reference.EntityType
3 3  
4 4  // ------------------------------------------------------------------
5 5  // 1. References
... ... @@ -10,12 +10,12 @@
10 10  // ------------------------------------------------------------------
11 11  // 2. Load (or create) the XWikiAllGroup page
12 12  // ------------------------------------------------------------------
13 -def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper
14 -def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument
15 +def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper
16 +def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument
15 15  
16 16  if (groupDoc.isNew()) {
17 17   xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup')
18 - println " Created XWiki.XWikiAllGroup"
20 + println ' Created XWiki.XWikiAllGroup'
19 19  }
20 20  
21 21  // ------------------------------------------------------------------
... ... @@ -37,14 +37,33 @@
37 37   }
38 38  
39 39   if (!alreadyMember) {
40 - // Try to create XObject using the simplest method first
42 + // Try different methods to create the XObject.
43 + def obj = null
41 41   try {
42 - // Use the string class name instead of the DocumentReference
43 - def obj = groupDoc.newXObject("XWiki.XWikiGroups", xcontext)
45 + // Method 1: Use xcontext
46 + def context = xcontext ?: contextProvider.get()
47 + obj = groupDoc.newXObject(groupClassRef, context)
48 + } catch (Exception e1) {
49 + try {
50 + // Method 2: Use EntityReference
51 + obj = groupDoc.newXObject(
52 + new EntityReference(
53 + 'XWikiGroups',
54 + EntityType.DOCUMENT,
55 + new EntityReference('XWiki', EntityType.SPACE)
56 + )
57 + )
58 + } catch (Exception e2) {
59 + println "Failed to create XObject using either method:"
60 + println "Method 1 Exception: ${e1.getMessage()}"
61 + println "Method 2 Exception: ${e2.getMessage()}"
62 + return // Skip this user if we can't create the object
63 + }
64 + }
65 +
66 + if (obj != null) {
44 44   obj.setStringValue('member', userDocName)
45 45   added << userDocName
46 - } catch (Exception e) {
47 - println "Could not add user ${userDocName}: ${e.message}"
48 48   }
49 49   }
50 50  }
... ... @@ -56,7 +56,7 @@
56 56   xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}")
57 57   println " Added users to main wiki group: ${added.join(', ')}"
58 58  } else {
59 - println " All users are already members of XWikiAllGroup"
80 + println ' All users are already members of XWikiAllGroup'
60 60  }
61 61  {{/groovy}}
62 62