0

I have a group on my site - I want users to be able to add and remove themselves from this group via a button on a page. What is the URL I should be using as a parameter to retrieve the group and the group membership? All the examples I've seen involve the 2013 interface, which returns a 404 when I pass it the parameter.

/_api/web/sitegroups/getbyname(xxxx) 

gives me a 404.

Unfortunately I can't wait to do this until we finish migrating to SharePoint Online.

1

The endpoint is different for SP2010. The URL below will return all SharePoint Groups on a site collection.

<SiteCollectionUrl>/_vti_bin/Listdata.svc/UserInformationList?$filter=substringof('SharePointGroup',ContentType) eq true

There's another way to achieve what you are trying to do with SPServices.

$().SPServices({
  operation: 'AddUserToGroup',
  groupName: '<Group Name Here>',
  userLoginName: '<User Login Name Here'>,
  completefunc: function(data,status){
    //Do something here like alert user that they've been added
  }
});

Likewise

$().SPServices({
  operation: 'RemoveUserFromoGroup',
  groupName: '<Group Name Here>',
  userLoginName: '<User Login Name Here'>,
  completefunc: function(data,status){
    //Do something here like alert user that they've been removed
  }
});

EDIT: JSOM Add User (Reference)

function addUserToSharePointGroup(){
  var ctx = new SP.ClientContext.get_current();
  var groupCollection = ctx.get_web().get_siteGroups();
  var group = groupCollection.getById(<group ID number>); //Not sure if getByTitle or getByName is available.  Worth testing
  var userCreationInformation = new SP.UserCreationInformation();

  userCreationInformation.set_email('<email address>');
  userCreationInformation.set_loginName('DOMAIN\alias');
  userCreationInformation.set_title('<title here');

  var user = group.get_users().add(userCreationInformation);
  ctx.load(user);
  ctx.executeQueryAsync(
    function(){console.log(user.get_title() + " added.");},
    function(sender, args){console.error(args.get_message());}
  );
}

NOTE, if this is a single page application, you have to reference the following JS files before JSOM will work: /_layouts/1033/init.js /_layouts/MicrosoftAjax.js /_layouts/sp.core.js /_layouts/sp.runtime.js /_layouts/sp.js

  • I already know the specific group I want to return - I don't want all groups on the site. And for (annoying) policy reasons, we're not using SPServices. – Scott Davis Jun 20 at 18:39
  • I'm guessing the policy is something like "No External Libraries." Let me see if I can find you a better URL for specific groups. – BigRaj Jun 20 at 18:41
  • It would depend on the users rights to add themselves to a group. A better solution would be to use JSOM in a function to pull the user into the group. no extra libraries will need to be loaded unless it's a single page app. – BigRaj Jun 20 at 18:53
  • What's a better approach? The group that users will be adding themselves to has All Authenticated Users as an owner, (long story). The group they can add themselves to is to be used for email purposes only. – Scott Davis Jun 20 at 18:54
  • Using JSOM. I'll add a snippet to my answer – BigRaj Jun 20 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.