cancel
Showing results for 
Search instead for 
Did you mean: 

Share API: Add Member

ofrxnz
Champ in-the-making
Champ in-the-making
So, I am trying to utilize the following script found here

http://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference#Memberships_2

Memberships

Adds a new membership to the site.

POST /alfresco/service/api/sites/{shortname}/memberships

Requirements:

    * Default Format: json
    * Authentication: user
    * Transaction: required
    * Format Style: any

Definition:

    * Id: org/alfresco/repository/site/membership/memberships.post
    * Description: classpath:alfresco/templates/webscripts/org/alfresco/repository/site/membership/memberships.post.desc.xml

I think my problem is, I am not passing it the proper json value and was wondering if anyone had a skeleton of what this script wants.

I am passing it something like the following   I have tried several variations. 

'{"site" : "test","role" : "SiteManager","person" : {"userName" : "fName.lName","firstName" : "fName","lastName" : "lName"}}'

I based this off the response i received from /alfresco/service/api/sites/test/memberships/SomeValidUser and have since tweaked and mangled it.

I come from a PHP background so JS is a bit foreign to me.  Here is the method I'm using since i couldn't find a way to just POST data in JS without a form. My other concern with this method is it may be taking "params" and placing it under the wrong post variable say POST[0[params]] or something similar. 


      method = "post";
      path = "/alfresco/service/api/sites/test/memberships";
      params = '{"site" : "test","role" : "SiteManager","person" : {"userName" : "fName.lName","firstName" : "fName","lastName" : "lName"}}'
      
      var formA = document.createElement("form");
      formA.setAttribute("method", method);
      formA.setAttribute("action", path);
      
   
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
       // hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params);

        formA.appendChild(hiddenField);
   
    document.body.appendChild(formA);    // Not entirely sure if this is necessary
    formA.submit();


Any suggestions are always greatly appreciated. 

The goal of this code is to automagically add several managers to every site created without doing it by hand in alfresco or through invites.  If you know a better way I am all ears

Thanks in advance

Adam
4 REPLIES 4

ofrxnz
Champ in-the-making
Champ in-the-making
Sorry, I just realized I posted this in the wrong forum.

[size=85][MikeH: Moved for you][/size]

mikeh
Star Contributor
Star Contributor
I doubt you'll be able to use an HTML form for this, as the POSTed content-type needs to be set to "application/json". You'll need to use the XHR object directly, or a JavaScript library that wraps it (like YUI, jQuery, et al)

The body should look like:
{"person":{"userName":"first.last"},"role":"SiteContributor"}

Thanks,
Mike

ofrxnz
Champ in-the-making
Champ in-the-making
Mike, 

As always YOU ROCK!!!

That did the trick!!!

for future reference of the JS lame here is the code that worked

var url = "/alfresco/service/api/sites/test/memberships";
var params = '{"person":{"userName":"someValidUserName"},"role":"SiteContributor"}';

var http = new XMLHttpRequest();
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
   if(http.readyState == 4 && http.status == 200) {
      alert(http.responseText);
   }
}
http.send(params);

Thanks,

Adam

mikeh
Star Contributor
Star Contributor
Thanks for the feedback - glad you got it working!  Smiley Happy

Mike