Blogroll
A great source of information for Lotus Domino administrators
Jeff Potts' excellent ECM/Portal blog
Lots of very useful Alfresco articles
| Adding a User to an Alfresco Share Site via the Javascript API |
|
|
|
| Written by Nathan McMinn | ||||||||||
| Sunday, 14 August 2011 14:46 | ||||||||||
|
As much as I love working with Alfresco (and I do, really), there are some things about it that still make me scratch my head and say "huh?". One of these things is the way Alfresco Share handles adding users to sites. Typically, a site manager simply invites a user to a Share site via the invite mechanism. 95% of the time this is exactly how I want it to work. Sometimes, however, I need to add a user directly without going through the invite process. Strangely, Alfresco admin users cannot simply add users to an arbitrary site. Under older versions of Alfresco (3.1), there was a simple workaround. A user could be added directly to a site group, and thus the site, through the Alfresco Explorer admin interface. In newer versions of Alfresco (3.3.3, in my case), this no longer works. The site manager, collaborator, contributor and consumer groups no longer show up in the admin interface's group search. Aaaaarrrrgh!!!
Thankfully, there is another way. It takes a little more work. I have written a few articles before about the Alfresco Javascript API, and we're going to dive into it again to accomplish the task at hand.
So, let's get started! To add a user directly to a Share site using the Javascript API, two things are required:
The first step is to fire up your REST client or a browser and get an Alfresco authentication ticket. This is done via a simple HTTP GET operation. Just hit the following URL:
http://<your_alfresco_host>/alfresco/service/api/login?u=username&pw=password
This will return an authentication ticket that can be appended to all of your future requests. The exact response will look something like this:
The part of the ticket that should be appended to the URL is the content of the <ticket> element. Make a note of the ticket, it will be required in a moment. Now, fire up your REST client if you haven't already. The client will be used to send a POST to the site membership web service. This web service is used by Alfresco Share internally to (duh) add a user to a site with a specified role. You can browse all of the available web scripts on your system at this URL:
http://<your_alfresco_host>/alfresco/service/index
The script we're looking for is in the package /org/alfresco/repository/site/membership, and is called the "Add Site Membership" script. A full description of the script and its parameters can be found here:
http://<your_alfresco_host>
The script itself is pretty simple. Take a moment to read the description, go over the associated Javascript and get a feel for what it is going to do. I'll wait. Done? Good. If you look at the URL that will accept the POST, you'll see it has a pretty simple signature. Only one part of the URL needs to be changed. You'll need the short name of the site to which you want to add a user. The short name is the "URL name" that you used when you created the site. It's a part of every URL in the site. For this example we'll assume I have created a site called My Test Site with a URL name (short name) of "mytestsite". The URL specified by the script looks like this:
http://<your_alfresco_host>/alfresco/service/api/sites/{shortname}/memberships
Looking at the URL template above, it's pretty clear what to do. Substitute the shortname of your site for {shortname}. The last part of the URL is the Alfresco authentication ticket created earlier. Simply append it as a URL parameter named "alf_ticket".
http://<your_alfresco_host>/alfresco/service/api/sites/mytestsite/memberships?alf_ticket=TICKET_dc233f7e8212825ef8fa8f945762ee24df53bcd7b
When I first started working on this I tried to use the authentication built into my REST client, but Alfresco constantly complained about HTTP authentication. Appending the ticket in this manner seems to work nicely with every web script I have tried. The last step in this process is to create a JSON object that will go into the body of the POST request. A quick look at the Javascript for the memberships script shows that it expects a JSON object that contains a role (SiteManager, SiteCollaborator, SiteContributor or SiteConsumer, corresponding to the Share site roles), and a person object with a username property. Here's what such a request looks like:
Note that this isn't everything that Alfresco includes in a person object, but it's enough to get the job done in this case. Once you have entered this JSON into the body of your POST request, there is one more task and we're ready to send the request. Alfresco is expecting a certain content type for JSON requests. A proper REST client will let you set any additional HTTP headers that the request requires. Add a header called "Content-type" and set its value to "application/json". Once the header is set and your request body is ready go ahead and send the request. You should get back an HTTP OK response, with a body that contains another JSON object with a bit of detail about what was done. For the request and test site in this example, the response should look something like this:
It's entirely possible that there was an easier way to do this that I completely missed. Even if there is, it never hurts to get more familiar with the Javascript API. The Alfresco Javascript API is a powerful tool for Alfresco users. It doesn't matter if you are a developer or admin, learning the Javascript API is essential.
Happy coding! |
||||||||||
| Last Updated on Tuesday, 16 August 2011 11:47 | ||||||||||
About Me

My name is Nathan McMinn. I'm a software engineer, beer geek, wannabe adventurer and genuinely curious guy. Find me on Facebook, Linkedin or Twitter
Latest Comments
- Belgian Stout Recipe
Chris, I'd love to try it! I live in NJ, about 30 ... - Belgian Stout Recipe
I finally brewed it. I used the Belgian Ardennes 5... - iPad + Alfresco = Mobile Aweso...
Hi Nathan, Any update on the offline caching and s... - Calling Web Services from the ...
Hi, This is just want I am looking to do and thank... - WordPress CMIS Plugin - New Pr...
forex indonesia broker provides historical stock d...



Comments
I suppose you are right, this is Alfresco's REST API. If I am not mistaken, these API calls hit an Alfresco web script, which is probably in Javascript
Glad it was helpful either way!
Just wondering though - Isn't this technically the 'REST' api? There is a javascript api to work with sites (using siteService) but it doesnt allow this sort of functionality. (Or at least I dont think it does!)
I added a new user but put my email address as their email. I did this for a few users. When I got emailed the opt-in - I opted in, then changed those users to their actual emails. Its a pain but it worked.
RSS feed for comments to this post.