Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

This explains basic usage of the Sakai SiteService. This service is used to find out things about Sakai Sites (courses) and look them up by references or contexts. The Site provides access to memberships in the site and allows retrieval and manipulation.

...

  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. Add the SiteService bean to the bean for YourAppClass
      Code Block
      xml
      xml
      <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:
      Code Block
      java
      java
      private SiteService siteService;
      public void setSiteService(SiteService siteService) {
      	this.siteService = siteService;
      }
      
  2. Using the cover Component Manager 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 Use the CM cover to get the service
      Code Block
      java
      java
      import org.sakaiproject.component.cover.ComponentManager;
      import org.sakaiproject.site.api.SiteService;
      ...
        private SiteService siteService;
      
      Get access to the service using the cover
      Code Block
      javajava
      ...
          siteService = (SiteService) org.sakaiproject.site.cover.ComponentManager.get(SiteService.getInstance(class);
      

Getting the site reference (for security/authz lookups) from the context

...

Code Block
java
java
Site site = null;
try {
 	  site = siteService.getSite(toolManager.getCurrentPlacement().getContextsiteId);
   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 thendied and do something about it
}

Adding memberships to a Site

Use the SiteService to get the Site and then use the methods on Site to add a membership (it can also update and remove memberships)

Code Block
java
java

String userId = getCurrentUserId(); // or some other way of getting the userId
Site site;
try {
    site = siteService.getSite(siteId);
} catch (IdUnusedException e) {
    throw new IllegalArgumentException("Cannot find site by siteId: " + siteId +":"+e, e);
}
String roleId = site.getJoinerRole(); // or some other role
site.addMember(userId, roleId, true, false);
try {
    siteService.saveSiteMembership(site);
} catch (IdUnusedException e) {
    throw new IllegalArgumentException("Invalid site: " + site.getId() + ":" + e, e);
} catch (PermissionException e) {
    throw new SecurityException("Current user not allowed to update site memberships in site: " + site.getId() + " :"+e, e);
}

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

...