Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

So I'm attempting to write something to mimic sharepoint's people picker. I was originally using Utility.ResolvePrincipal with some success, but it was only returning me a single user (obviously). So, I tried to use Utility.SearchPrincipal. Only that's not returning me anything. Here's what I have:

var user = Utility.ResolvePrincipal(_clientContext, _clientContext.Web, nameStart, PrincipalType.user, PrincipalSource.All, null, false);
var users = Utility.SearchPrincipals(_clientContext, _clientContext.Web, nameStart, PrincipalType.user, PrincipalSource.All, null, 10);
_clientContext.ExecuteQuery();

At this point, user.Value has a single user, and users is an empty list. What am I doing wrong?

share|improve this question
    
Try below correction in your code : var users = Utility.SearchPrincipals(_clientContext, _clientCOntext.Web, nameStart, PrincipalType.user, PrincipalSource.All, null, 10); _clientContext.ExecuteQuery(); –  Amit Kumawat Apr 22 '13 at 9:39
    
Good catch, but it was just a transcription error. –  Colin DeClue Apr 22 '13 at 13:53
    
Grab a copy of iLspy and take a look how it works internally in SharePoint you can pretty much use identical code. This is always my first go to when I don't know how to do something but it already exists in SharePoint. You can consider to use msdn.microsoft.com/en-us/library/… if you want more profile information as well. –  Hugh Wood Apr 23 '13 at 17:41
    
UserProfileManager looks like it only works with an SPServiceContext and not a ClientContext. –  Colin DeClue Apr 23 '13 at 17:53
1  
@Wartickler: That was it! I switched the PrincipalSource to UIL, and I got 2 results! (Both different than ResolveUser got, and they're both SecurityGruoups for some reason, but hey! Results!) –  Colin DeClue Apr 26 '13 at 16:40
show 5 more comments

4 Answers 4

up vote 1 down vote accepted

I'm thinking you are running into an issue when peeking into all of those PrincipalSource.All sources. Perhaps a permissions issue?

Are you really able to get into a RoleProvider, MembershipProvider, Active Directory and the UIL between the client and the 2013 Provider Host? ResolvePrincipal(...) stops looking when it finds a single user. The first enum in PrincipalSource is the UIL. But, SearchPrincipals(...) has to go through all of its other possible sources (when using .All) and may be unhappy with the walls it's hitting.

Good luck (:

share|improve this answer
add comment

I did the same a couple of weeks ago. In my case i used SPServices to search for persons and it worked quite well. In my case i used the web service to search persons.

$().SPServices({
  operation: "SearchPrincipals",
  webURL: "/",
  searchText: "bauer",
  maxResults: 100,
  SPPrincipalType: "SPPrincipalType.User",
  completefunc: function (xData, Status) {
    alert(xData);
  }
});

Do you get any error back?

share|improve this answer
    
That's giving me a 401 unauthorized because this is a SharePoint 2013 Provider Hosted app that lives inside a different domain than the actual SharePoint site. –  Colin DeClue Apr 22 '13 at 14:00
add comment

Retrieve a Sharepoint principal by Active Directory user or group name

Now this one took me a very long time. For some reason there is a static Utility.SearchPrincipals method that gets you a PrincipalInfo object, but you can never get to get a Principal object that you can use for setting permissions. I spent a long time scratching my head how to get around this before I found there is another way:

public Principal GetPrincipal(string name)
{
  if (web != null)
  {
    try
    {
      var principal = web.EnsureUser(name);
      clientContext.Load(principal);
      clientContext.ExecuteQuery();
      if (principal != null)
      {
        return principal;
      }
    }
    catch (ServerException){}
  }
  return null;
}

using the method like so:

var g = sh.GetPrincipal("MyUserGroup");
var u = sh.GetPrincipal("MyUser");

http://dotnetbyexample.blogspot.co.uk/2011/03/sharepoint-client-object-model-sites.html

share|improve this answer
    
This doesn't really help. I already know how to get the actual principal after I get the Principal Info through (well, hopefully SearchPrincipals) but right now through ResolvePrincipal. –  Colin DeClue Apr 24 '13 at 13:14
    
Solved my problem! I actually start banging my head into Microsoft.SharePoint.Client.UserProfiles, now I can stop! Thanks! –  Mzn Jan 19 at 19:53
add comment

My example from project, correctly works with users and groups.

    public Principal ResolvePrincipal(string name)
    {
        if (String.IsNullOrEmpty(name))
            throw new ArgumentException("Principal name is empty");

        Principal result = null;

        var info = Utility.ResolvePrincipal(Context, Context.Web, name, PrincipalType.All, PrincipalSource.All, null, false);
        Context.ExecuteQuery();

        if (info.Value.PrincipalType == PrincipalType.User)
            result = Context.Web.EnsureUser(info.Value.LoginName);

        if (info.Value.PrincipalType == PrincipalType.SharePointGroup || info.Value.PrincipalType == PrincipalType.SecurityGroup)
            result = Context.Web.SiteGroups.GetById(info.Value.PrincipalId);

        Context.Load(result);
        Context.ExecuteQuery();

        return result;
    }

And tests

rep.ResolvePrincipal("i:0#.f|membership|admin@helpdesk02.onmicrosoft.com");  //return user
rep.ResolvePrincipal("admin@helpdesk02.onmicrosoft.com"); //return user
rep.ResolvePrincipal("Help Desk Owners"); // return group Help Desk Owners
rep.ResolvePrincipal("1"); //return User with Id = 1
rep.ResolvePrincipal("Roman Rylov"); //return User
rep.ResolvePrincipal(""); //throw Exception
share|improve this answer
    
This doesn't really apply to my question at all (which I have resolved, and which has an accepted answer). My question was around how to get a full list of users for searching. Your code does look useful, just not for this. –  Colin DeClue Mar 20 at 15:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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