I have a page for each company department.
When creating procedures, users must select the department to which the procedure belongs to.
The field is a DataBase List with this query
select doc.fullName, doc.title from XWikiDocument doc, BaseObject as obj where doc.fullName = obj.name and obj.className = ‘Departamentos.Code.DepartamentosClass’ and doc.name <> ‘DepartamentosTemplate’
However, when going to department page, I can´t see it as a backlink (of course, it’s not a link)… but then it does not accomplishes the objective of selecting the department, that is, for department employees to go to their department page and see all procedures that cite them in back links, but also all procedures that belong to their department.
Is there any way for that, which I am missing?
Hi @rogerpenna,
You need to to a query in the department to list all the procedures that are assigned to the department. If you have a Departamentos.Code.DepartamentosSheet
you will add there the code to get all the procedures for the current department.
Hope it helps,
Alex
My Department Pages are created inside an AWM app. Can I do the query directly in the content field of the app, or what?
Because when I wrote ChatGPT my question and your answer, it gave me a quite complex answer on how to do that… is it really THAT complicated or ChatGPT is making things worse than reality?
You’ve made it clear that you want the department pages to display a list of procedures that belong to each department. Here is a step-by-step guide for creating a new sheet for your Department class that will display a list of procedures associated with a particular department:
- Creating a New Sheet:
- From your wiki’s homepage, navigate to the location where you want to create your new sheet. This could be within the Department app or elsewhere.
- Click the “+ Create” button and select “Page”.
- Give the new page a name (like “DepartmentProceduresSheet”) and click “Create”.
- In the page editor, click “Edit” > “Objects” and then click “Add object”.
- From the list of classes, choose
XWiki.SheetClass
and then click “Add”.
- Click “Save & Continue”.
- Click “Edit” > “WYSIWYG”.
- In the editor, add some code similar to the following (this code is assuming the procedure class is named “ProcedureClass” and the field holding the department reference is “department”):
{{velocity}}
#set($hql = ", BaseObject as obj, StringProperty as prop where doc.fullName = obj.name and obj.className = 'ProcedureClass' and prop.id.id = obj.id and prop.name = 'department' and prop.value = '$doc.fullName'")
#set($results = $services.query.hql($hql).execute())
#if ($results.size() > 0)
= Procedures assigned to this department =
(% class="grid" %)
(((
{{table}}
|=(% colspan="2" %) Procedure
#foreach($result in $results)
#set($procedureDoc = $xwiki.getDocument($result))
|[[$procedureDoc.displayTitle>>$procedureDoc.fullName]]
#end
{{/table}}
)))
#else
= No procedures assigned to this department =
#end
{{/velocity}}
-
- Linking the New Sheet to the Department Class:
- Navigate to your Department class page. It should be located at a URL like “https://yourwikiurl/xwiki/bin/view/Departamentos/Code/DepartamentosClass”.
- Click “Edit” > “Objects”.
- In the list of objects, find the one of class
XWiki.ClassSheetBinding
.
- Click the pencil icon to edit this object.
- In the “Sheet” field, enter the name of the new sheet you created (like “DepartmentProceduresSheet”).
- Click “Save & View”.
The steps above outline how you can create a new sheet that lists all procedures assigned to a department, and then link this sheet to your Department class so that when you view a department page, it uses the new sheet to display the list of procedures.
Please note that you might need to modify the code to match your actual database structure. Be sure to replace “ProcedureClass” with the actual class name of your procedure documents, and replace “department” with the name of the field in the procedure class that stores the department reference.
Please let me know if you need further clarification or help.
If you created your application using the AWM app, then a sheet was already generated for it, no need to create a new one. You should find it and add the code you need to display all the procedures assigned to the current department.
Thanks for helping Acotiuga.
I entered the App page, which shows a livetable with all procedures. Then I edited the page in WYSIWYG and changed to source code.
Added the velocity code and saved. Now below the table with procedures, it shows the message “No related procedures”. So I guess I didn´t find the sheet and edited the wrong place.
How do I find the “sheet” of the app?
If you need to display the related procedures in a department page, then you need the department sheet which should be Departamentos.Code.DepartamentosSheet
if the class is Departamentos.Code.DepartamentosClass
. Or, edit a department page in object mode, check the class name, navigate to the class and you will see there the sheet.
When I clicked to edit the app, at the end of the app editing page, there was a link to edit the sheet There, the middle checkmark

I clicked that link which takes me here. However, I can´t save it unless I give it a title. And if I give it a title, all Departaments pages are renamed to that title!

That is because you have configured your wiki to force title in the pages. You can temporarily disable that to save your sheet.

going into the URL you suggested, I get instead an empty field.
I wrote the code but I still need to add a title.
After saving it, it created a new page with that title in http://localhost:8080/bin/view/Departamentos.Code.DepartamentosSheet/
It’s PARALLEL in the structure to Departamentos, not inside it. And inside each Departamento, no change at all 
Thanks for the patience. I know it’s not infinite however.
with or without title, when editing that I get this structure, parallel to Departamento.

I was able to successfully edit the ‘Departamentos’ Sheet and I tried to implement the desired functionality there. I used an HQL query to fetch the ‘Procedimentos’ whose ‘Departamento Responsável’ field matches the name of the current ‘Departamento’ page.
The query looks like this:
#set ($hql = "select doc.fullName from XWikiDocument as doc, BaseObject as obj where doc.fullName=obj.name and obj.className='Procedimentos.Code.ProcedimentosClass' and obj.databaseList1='${doc.documentReference.name}'")
Here ‘databaseList1’ is the field name of ‘Departamento Responsável’ in ‘ProcedimentosClass’.
However, I encountered an issue where the error “Failed to execute the [velocity] macro. Cause: [could not resolve property: databaseList1 of: com.xpn.xwiki.objects.BaseObject]” was displayed.
After extensive troubleshooting with ChatGPT, we were able to retrieve the list of all ‘Procedimentos’ names and titles using the HQL query, but when we try to filter them by ‘Departamento Responsável’, we encounter the error “BaseStringProperty is not mapped”. It seems that XWiki is having trouble resolving the ‘databaseList1’ field of the ‘ProcedimentosClass’ in the HQL query.
We further observed that the ‘Procedimentos’ associated with a particular ‘Departamento’ are being fetched correctly but they’re not being filtered according to the ‘Departamento Responsável’. Instead, all ‘Procedimentos’ are displayed.
The full HQL query used for filtering is:
#set ($query = $services.query.hql("select str.value from BaseObject as obj, StringProperty as str where obj.className='Procedimentos.Code.ProcedimentosClass' and obj.name=str.id.id and str.name='databaseList1' and str.value='${doc.documentReference.name}'"))
I would greatly appreciate any insights or suggestions from the community on how to resolve these issues and achieve the desired functionality.
If ChatGPT doesn’t help you with the quiery building, you could take a look at the https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module documentation.
Hope it helps!
" The ‘Departamento Responsável’ field is in the format ‘Departamentos..WebHome’, while the full name of the current ‘Departamento’ page is in the format ‘xwiki:Departamentos..WebHome’. The ‘xwiki:’ prefix in the full name of the current ‘Departamento’ page is causing the query to not match.
To resolve this issue, we can modify the query to remove the ‘xwiki:’ prefix from the full name of the current ‘Departamento’ page before comparing it with the ‘Departamento Responsável’ field. We can do this using the replace
method of the String
class in Velocity:"
So I got this query… it worked!!!
#set ($currentDepartamento = $doc.documentReference.toString().replace('xwiki:', ''))
#set ($hql = "select doc.fullName from XWikiDocument as doc, BaseObject as obj, StringProperty as str where doc.fullName=obj.name and obj.className='Procedimentos.Code.ProcedimentosClass' and obj.id=str.id.id and str.name='databaseList1' and str.value='$currentDepartamento'")
#set ($query = $services.query.hql($hql))
#set ($results = $query.execute())
#if ($results.isEmpty())
No matching 'Procedimentos' found.
#else
## Display the title
{{html clean="false"}}<h2>PROCEDIMENTOS ASSOCIADOS</h2>{{/html}}
#foreach ($result in $results)
## Create a link to the 'Procedimento' page
{{html clean="false"}}<a href="$xwiki.getDocument($result).getURL()">$xwiki.getDocument($result).displayTitle</a><br>{{/html}}
#end
#end
