0 Votes

Changes for page Script

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

From version 7.1
edited by Ryan C
on 2025/05/13 16:01
Change comment: There is no comment for this version
To version 10.1
edited by Ryan C
on 2025/05/13 16:03
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,20 +1,20 @@
1 1  {{groovy}}
2 2  import org.xwiki.model.reference.DocumentReference
3 3  
4 +// References
4 4  def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup")
5 -def groupClassName = "XWiki.XWikiGroups"
6 +def groupClassName = "XWiki.XWikiGroups" // ⚠️ Must be a STRING
6 6  
7 -// 1. Ensure the group document exists
8 +// Load or create the group page
8 8  def groupDocAPI = xwiki.getDocument(groupDocRef)
9 9  def groupDoc = groupDocAPI.getDocument()
10 10  
11 11  if (groupDoc.isNew()) {
12 - def apiDoc = xwiki.getDocument(groupDoc.documentReference)
13 - xwiki.saveDocument(apiDoc, "Created XWikiAllGroup page")
14 - println "🆕 Created XWiki.XWikiAllGroup page"
13 + xwiki.saveDocument(groupDocAPI, "Created XWikiAllGroup")
14 + println "Created XWiki.XWikiAllGroup"
15 15  }
16 16  
17 -// 2. Proceed as before
17 +// Add missing users to group
18 18  def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'")
19 19  def added = []
20 20  
... ... @@ -24,18 +24,19 @@
24 24   }
25 25  
26 26   if (!alreadyMember) {
27 - def newObj = groupDoc.newXObject(groupClassName)
28 - newObj.setStringValue("member", userDocName)
27 + def obj = groupDoc.newXObject(groupClassName) // ✅ String only
28 + obj.setStringValue("member", userDocName)
29 29   added << userDocName
30 30   }
31 31  }
32 32  
33 +// Save the document
33 33  if (added) {
34 34   def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
35 - xwiki.saveDocument(wrappedDoc, "Added users to XWikiAllGroup: ${added.join(', ')}")
36 - println "Added users to main wiki: ${added.join(', ')}"
36 + xwiki.saveDocument(wrappedDoc, "Added users to group: ${added.join(', ')}")
37 + println "Added users: ${added.join(', ')}"
37 37  } else {
38 - println "All users are already members of the main wiki."
39 + println "All users are already members of the main wiki."
39 39  }
40 40  {{/groovy}}
41 41