Information
This explains basic usage of the Sakai SiteService. This service is used to find out things about Sakai Sites and look them up by references or contexts.
- Trunk javadocs:
- Trunk source location: https://source.sakaiproject.org/svn/site/trunk/
Accessing the SiteService
- You can use Spring Framework to inject the service or use the cover
- Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
- Add the SiteService bean to the bean for YourAppClass
<bean id="org.sakaiproject.yourapp.logic.YourAppClass" class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl"> <property name="siteService" ref="org.sakaiproject.site.api.SiteService" /> </bean>
- Add a variable and setter to YourAppClass to use the service in like so:
private SiteService siteService; public void setSiteService(SiteService siteService) { this.siteService = siteService; }
- Add the SiteService bean to the bean for YourAppClass
- Using the cover to get the service
- Note: This is not the recommended method, you should be using Spring to inject the service
- Setup a variable to hold the instance from the cover
private SiteService siteService;
- Get access to the service using the cover
siteService = org.sakaiproject.site.cover.SiteService.getInstance();
Getting the site reference (for security/authz lookups) from the context
- Use the ToolManager service to get the current context
String currentContext = toolManager.getCurrentPlacement().getContext();
- Note: You could also retrieve the context in other ways, this is just the common one
- Use the SiteService to get the site reference from the context
String siteRef = siteService.siteReference(currentContext);
Getting the Site object for the site the current user is currently in
Site site = null; try { site = siteService.getSite(toolManager.getCurrentPlacement().getContext()); } catch (Exception e) { // assume we are not in a site then and do something about it }
Getting the workspace [Site|http://nightly2.sakaiproject.org/javadoc/org/sakaiproject/site/api/Site.html for a user (using the userId)
String siteId = siteService.getUserSiteId(userId); Site workspaceSite = siteService.getSite(siteId);