2
votes

I'm having a play around with the client side object model and apps for SharePoint Online. I can retrieve the information from a Person and Groups field using a FieldUserValue object, however, how can I determine from this if the value entered is simply a user, or a SharePoint group?

As far as I can tell, the FieldUserValue only has a LookupId and LookupValue as its properties, which doesn't specify if it is a group or not. Have I gone the wrong way about this and is there a much better way of querying the field and checking if the value is a user of SharePoint group?

Thanks

1
Maybe something useful: FieldUserValue singleValue = (FieldUserValue)targetItem["Single"]; FieldUserValue[] multValue = targetItem["Multiple"] as FieldUserValue[]; from there. Also looking at this answer, you can guess which field interests you in your case (instead of "Single" and "Multiple") - Kilazur

1 Answers

6
votes

You could determine whether the user field value is User or Group by getting Content Type of List Item in User Information List:

public static string GetUserFieldType(ClientContext ctx,FieldUserValue value)
{
    var userInfoList = ctx.Site.RootWeb.SiteUserInfoList;
    var userInfo = userInfoList.GetItemById(value.LookupId);
    ctx.Load(userInfo,i => i.ContentType);
    ctx.ExecuteQuery();
    return userInfo.ContentType.Name;
}

Usage

Assume a List contains single-valued User Field, then:

using (var ctx = new ClientContext(webUrl))
{
      ctx.Credentials = CreateSPOCredentials(userName, password);

      var list = ctx.Web.Lists.GetByTitle(listTitle);
      var listItem = list.GetItemById(itemId);
      ctx.Load(listItem);
      ctx.ExecuteQuery();

      var userVal = listItem[fieldName] as FieldUserValue;
      var type = GetUserFieldType(ctx,userVal);
      var isUser = type == "Person";
      var isGroup = type == "SharePointGroup";

  }