Question

I have a sharepoint field in a list that can be either a user or a group. Using the Server Object Model, I can identify easily whether the user is a group or not.

However, I cannot find a way to achieve this using the Managed Client Object model. Is there a way to know.

I only managed to make it work by looping the list of groups and checking if the there is a group with the name. Howver, this is not exactly correct or efficient. Maybe there is a way to find out using the ListItem of the user. But I did not see any fields that show that user is administrator. I have also tried EnsureUser. This crashes if the user is not a group. So I could work out by using a try/catch but this would be very bad programming.

Thanks,

Joseph

Was it helpful?

Solution

To do this get the list of users from ClientContext.Current.Web.SiteUserInfoList and then check the ContentType of each item that is returned to determine what it is.

Checking the content type is not very direct though, because all you actually get back from each item is a ContentTypeID, which you then have to look-up against the content types of the user list at ClientContext.Current.Web.SiteUserInfoList.ContentTypes. That look-up will return a ContentType object, and you can read from the Name property of that object to see what the list item is.

So an over simplified chunk of code to do this would be:

using Microsoft.SharePoint.Client;

...

ClientContext context = ClientContext.Current;

var q = from i in context.Web.SiteUserInfoList.GetItems(new CamlQuery()) select i;
IEnumerable<ListItem> Items = context.LoadQuery(q);
context.ExecuteQueryAsync((s, e) => {
    foreach (ListItem i in Items) {
        //This is the important bit:
        ContentType contenttype = context.Web.SiteUserInfoList.ContentTypes.GetById(i["ContentTypeId"].ToString());
        context.Load(contenttype); //It's another query so we have to load it too
        switch (contenttype.Name) {
            case "SharePointGroup":
                //It's a SharePoint group
                break;
            case "Person":
                //It's a user
                break;
            case "DomainGroup":
                //It's an Active Directory Group or Membership Role
                break;
            default:
                //It's a mystery;
                break;
        }
    }
},
    (s, e) => { /* Query failed */ }
);

You didn't specify your platform, but I did all of this in Silverlight using the SharePoint client object model. It stands to reason that the same would be possible in JavaScript as well.

OTHER TIPS

Try Microsoft.SharePoint.Client.Utilities.Utility.SearchPrincipals(...):

var resultPrincipals = Utility.SearchPrincipals(clientContext, clientContext.Web, searchString, PrincipalType.All, PrincipalSource.All, null, maxResults);

The return type, PrincipalInfo, conveniently has a PrincipalType property which you can check for Group.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top