Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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.

Accessing the SiteService

  • You can use Spring Framework to inject the service or use the cover
  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. 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>
      
    2. 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;
      }
      
  2. Using the cover to get the service
    • Note: This is not the recommended method, you should be using Spring to inject the service
    1. Setup a variable to hold the instance from the cover
      private SiteService siteService;
      
    2. 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

  1. 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
  2. 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

Use the ToolManager service to get the current context and then use the SiteService to get the Site

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 membership for a Site

Use the SiteService to get the Site and then get the Members from the site

Site site = null;
try {
   site = siteService.getSite(siteId);
   Set<Member> members = site.getMembers();
   for (Member member : members) {
      // do something here
      log.info("Check it out: " + member.getUserEid());
   }
} catch (Exception e) {
	// assume we are not in a site or something died and do something about it
}

Getting the workspace Site for a user (using the userId)

  1. Use the SiteService to get the siteId based on the userId
    String siteId = siteService.getUserSiteId(userId);
    
  2. Use the SiteService to get the Site based on the siteId
    Site workspaceSite = siteService.getSite(siteId);
    
  • No labels