cancel
Showing results for 
Search instead for 
Did you mean: 

Get instance of the siteService object

gilleslesire
Champ in-the-making
Champ in-the-making
I have no experience with Alfresco, I do know how to program however, so no problem there.

I am trying to do a minor edit to the /share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/share/imports/share-header.lib.js file to add a few menu elements to the share menu.

I want to list all the sites a user is a member of.

After doing some searching I came across the siteService object which can do what I want to achieve so I added the following to the generateAppItems function.

<javascript>
function generateAppItems() {
…..
var sites = siteService.listUserSites(user.name);
   for (var i=0; i<sites.length; i++)
   {
       var targetUrl = "/share/page/site/" + sites.shortName + "/" + dashboard;
       appItems.push({
          id: "HEADER_SITE_" + sites.shortName.toUpperCase(),
          name: "alfresco/menus/AlfMenuBarItem",
          config: {
             id: "HEADER_SITE_" + sites.shortName.toUpperCase(),
             label: (sites.title) ? sites.title : sites.shortName,
             targetUrl: targetUrl
          }
       });
   }
…..
}
</javascript>

I try to get the sites the user is member of by using siteService.listUserSites(user.name). But now I get the following error

ReferenceError: "siteService" is not defined.


I have googled around but can't find anywhere how I should instantiate this elusive siteService object.
3 REPLIES 3

mrogers
Star Contributor
Star Contributor
The site service is not available in share.

You will have to call a webscript to return the list of users sites.

gilleslesire
Champ in-the-making
Champ in-the-making
Please delete this comment. I moved this reply to a reply on this thread instead of on this comment. I do not have the authorization to delete my comment apparantly.

gilleslesire
Champ in-the-making
Champ in-the-making
Some example code would have been nice. Anyway thanks for pointing me in the right direction, I didn't really have an understanding of the Alfresco architecture just yet.

So for future generations Smiley Wink I'll add the code of my solution.

So a mrogers said, you can't access the siteService through the share tier.

Instead you have to use the REST APIs of Alfresco.

I ended up adding the following function to the file

<javascript>
function getUserSites()
{
   // Call the repository for the site profile
   var json = remote.call("/api/people/" + encodeURIComponent(user.name) + "/sites");
   var userSites = null;
   if (json.status == 200)
   {
      // Create javascript objects from the repo response
      var obj = eval('(' + json + ')');
      if (obj)
      {
         userSites = obj;
      }
   }

   return userSites;
}
</javascript>

and changing the generateAppItems to

<javascript>
function generateAppItems() {
        var appItems = [
        {
            id: "HEADER_HOME",
            name: "alfresco/menus/AlfMenuBarItem",
            config: {
                id: "HEADER_HOME",
                label: "header.menu.home.label",
                targetUrl: "user/" + encodeURIComponent(user.name) + "/dashboard"
            }
        }
        ];

        var sites = getUserSites();

        if(sites != null) {
         for (var i=0; i<sites.length; i++)
         {
             var targetUrl = "site/" + sites.shortName + "/dashboard";
             appItems.push({
                id: "HEADER_SITE_" + sites.shortName.toUpperCase(),
                name: "alfresco/menus/AlfMenuBarItem",
                config: {
                   id: "HEADER_SITE_" + sites.shortName.toUpperCase(),
                   label: (sites.title) ? sites.title : sites.shortName,
                   targetUrl: targetUrl
                }
             });
         }
     }

   appItems.push({
         id: "HEADER_SITES_MENU",
         name: "alfresco/header/AlfSitesMenu",
         config: {
            id: "HEADER_SITES_MENU",
            label: "header.menu.sites.label",
            currentSite: page.url.templateArgs.site,
            currentUser: user.name
         }
      },
      {
       id: "HEADER_TASKS",
       name: "alfresco/header/AlfMenuBarPopup",
       config: {
          id: "HEADER_TASKS",
          label: "header.menu.tasks.label",
          widgets: [
             {  
                name: "alfresco/menus/AlfMenuGroup",
                config: {
                   widgets: [
                      {
                         id: "HEADER_MY_TASKS",
                         name: "alfresco/header/AlfMenuItem",
                         config:
                         {
                            id: "HEADER_MY_TASKS",
                            label: "header.menu.mytasks.label",
                            iconClass: "alf-mytasks-icon",
                            targetUrl: "my-tasks#filter=workflows|active"
                         }
                      },
                      {
                         id: "HEADER_MY_WORKFLOWS",
                         name: "alfresco/header/AlfMenuItem",
                         config:
                         {
                            id: "HEADER_MY_WORKFLOWS",
                            label: "header.menu.myworkflows.label",
                            iconClass: "alf-myworkflows-icon",
                            targetUrl: "my-workflows#filter=workflows|active"
                         }
                      }
                   ]
                }
             }
          ]
       }
   });

   if (user.isAdmin || showRepositoryLink == "true")
   {
      appItems.push({
         id: "HEADER_REPOSITORY",
         name: "alfresco/menus/AlfMenuBarItem",
         config: {
            id: "HEADER_REPOSITORY",
            label: "header.menu.repository.label",
            targetUrl: "repository"
         }
      });
   }
   if (user.isAdmin)
   {
      appItems.push({
         id: "HEADER_ADMIN_CONSOLE",
         name: "alfresco/menus/AlfMenuBarItem",
         config: {
            id: "HEADER_ADMIN_CONSOLE",
            label: "header.menu.admin.label",
            targetUrl: "console/admin-console/application"
         }
      });
   }
   return appItems;
}
</javascript>

This will result in removing the MY FILES and SHARED FILES from the share header menu and adding all the sites the user is a member of as menu items for faster navigation as I wanted.

Thanks for the help anyway.