1

I want to create a code that only generates a link for certain users in a group, but this group name changes across the many subsites that my users have. The group name = [Site Title] + ' Creators' (e.g., "P2X Creators").

I was initially checking for "Creators" in their group collection using SPServices: http://sympmarc.com/2010/01/29/determining-if-a-user-is-in-a-permission-group-with-sharepoints-web-services/ but I realize that if you are in another group with "Creators" in the name on some other subsite within my portal, then the link will show up because this SPService method uses a webservice that checks the groups for the entire site collection.

So what I would like to do is to use something like Web.get_title() (which shows up as an option in the js console but says the property is missing) and just set that value + " Creators" so that I can search for that specific group. Another option would be for me to simply retrieve the names of the groups within the specific subsites so I can grab the name of that group which contains "Creators".

Please let me know if this is worded confusingly or if I can improve on any more specific details.

1
  • For now, I am grabbing the Site Title value that displays in the top lefthand of my library. Hopefully this is consistent enough.
    – Dinerdo
    Commented May 22, 2014 at 18:34

1 Answer 1

2

With every page is associated client context (_spPageContextInfo object), which in turn contains webTitle property. This property stores current web title and the following example demonstrates how to generate fully qualified name of group:

function resolveGroupName(groupName)
{
    return _spPageContextInfo.webTitle + ' ' + groupName;
}

Usage

$().SPServices({
     operation: "GetGroupCollectionFromUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='" + resolveGroupName('Creators') + "']").length == 1)
        {
           //...
        }
      }
});

Option 2. Using CSOM (JavaScript)

An alternative solution would be to utilize CSOM API. SP.User.groups property gets the collection of groups of which the user is a member.

Example:

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var user = web.get_currentUser();
var groups = user.get_groups();

context.load(web,'Title');
context.load(groups);
context.executeQueryAsync(
     function() {

       var groupFQN = web.get_title() + ' Approvers';   //generate group name 
       if(groupExists(groups,groupFQN)){
          //...
       }

     },
     function(sender,args){
        console.log(args.get_message());
     }
  );

function groupExists(groups,name){
   var found = false;
   for (var i = 0; i < groups.get_count() ; i++) {
       var group = groups.getItemAtIndex(i);
       if(group.get_title() == name) {
            found = true;
            break;
       }
   }
   return found;
}
4
  • _spPageContextInfo does not contain that property for me. Is there something else I need to implement in order to use it? I only see things like webLanguage, webUIVersion, webServerRelativeUrl. I am in SP2010 btw.
    – Dinerdo
    Commented May 22, 2014 at 18:34
  • It seems webTitle property of _spPageContextInfo object is available in SharePoint 2013 only. Please check the undated answer, it demonstrates an alternative solution using CSOM API Commented May 22, 2014 at 19:46
  • Thank you for this answer! It would certainly solve my issue, but I receive this error when trying to use web.get_title(); '>>> web.get_title(); Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.'
    – Dinerdo
    Commented May 22, 2014 at 20:21
  • Hmm, in that case, you could try to specify explicitly to load Title property for Web. For that, please replace the line: context.load(web); with this one: context.load(web,'Title'); or check an updated answer. Commented May 22, 2014 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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