2

I am able to get a user field and its value from a list using CSOM/JSOM. I cannot figure out how to get if the value is a user or a group.

ExecuteOrDelayUntilScriptLoaded(function()
{
    var context = new SP.ClientContext.get_current()
    var list = context.get_web().get_lists().getByTitle("test");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);

    context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
    {
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

        var items = list.getItems(query);

        context.load(items);

        context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
        {
            var listEnumerator = items.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var listItem = listEnumerator.get_current();

                // this field is a "Person or Group" field with a group as the value
                var userField = listItem.get_item("n09_02");

                // i can get the value
                alert(userField.get_lookupValue());

                // how can i check if the value is a user or group?
            }
        }), Function.createDelegate(this, function(){}));
    }), Function.createDelegate(this, function(){}));
}, "sp.js");
2

1 Answer 1

0

Did that in SP2016 using the SP Services library. First checked whether the field value is a group, using the GetGroupInfo operation. If it is NOT a group, you get an error (I was not interested in that scenario) - that's the drawback. If it IS a group, you get response with < Group > tag.

$().SPServices({
   operation: "GetGroupInfo",
   async: true,
   groupName: [your group's name],
   completefunc: function (xData, Status) {
      if (Status === 'success') {                                            
         $(xData.responseXML).SPFilterNode("Group").each(function () {
            groupId =  $(this).attr("ID");
         });
      } else {
         console.log([your group's name] + ' is not a group!');
      }
   }
});

Depending on your needs, you can later use the SP Services` GetUserInfo operation as well to check whether the user is actually a Domain Group - you get response with < User > tag and IsDomainGroup property.

Your Answer

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

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