private object GetUserInformation(string userPrincipalName)
        {
            var peopleContext    = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(ClientContext);
            var personProperties = peopleContext.GetPropertiesFor(userPrincipalName);

            ClientContext.Load(personProperties);
            ClientContext.ExecuteQuery();
            if (personProperties != null)
            {
                LogVerbose("Tenant PeopleManager");
                var profileProperties = personProperties.UserProfileProperties.ToList();
                LogVerbose("Display Name: {0}", personProperties.DisplayName);
                LogVerbose("Email {0}", personProperties.Email);
                LogVerbose("Latest Post {0}", personProperties.LatestPost);
                LogVerbose("Personal Url {0}", personProperties.PersonalUrl);
                LogVerbose("UserProfile_GUID: {0}", GetPropertyValue(profileProperties, "UserProfile_GUID"));
                LogVerbose("SPS-DistinguisedName: {0}", GetPropertyValue(profileProperties, "SPS-DistinguishedName"));
                LogVerbose("SID: {0}", GetPropertyValue(profileProperties, "SID"));
                LogVerbose("msOnline-ObjectId: {0}", GetPropertyValue(profileProperties, "msOnline-ObjectId"));

                profileProperties.ForEach(f =>
                {
                    LogVerbose("Property:{0} with value:{1}", f.Key, f.Value);
                });
            }

            return(null);
        }
Пример #2
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPUserDefinitionModel>();

            try
            {
                LogVerbose("Querying UserProfiles.PeopleManager");
                var userPrincipalName = string.Format("i:0#.f|membership|{0}", this.UserName);
                var peopleContext     = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(this.ClientContext);
                var personProperties  = peopleContext.GetPropertiesFor(userPrincipalName);
                this.ClientContext.Load(personProperties);
                this.ClientContext.ExecuteQuery();

                if (personProperties != null)
                {
                    var profileProperties = personProperties.UserProfileProperties.ToList();
                    LogVerbose("Display Name: {0}", personProperties.DisplayName);
                    var model = new SPUserDefinitionModel()
                    {
                        UserDisplay          = personProperties.DisplayName,
                        UserEmail            = personProperties.Email,
                        LatestPost           = personProperties.LatestPost,
                        OD4BUrl              = personProperties.PersonalUrl,
                        UserProfileGUID      = profileProperties.GetPropertyValue("UserProfile_GUID"),
                        SPSDistinguishedName = profileProperties.GetPropertyValue("SPS-DistinguishedName"),
                        SPSSid           = profileProperties.GetPropertyValue("SID"),
                        MSOnlineObjectId = profileProperties.GetPropertyValue("msOnline-ObjectId")
                    };
                    models.Add(model);
                }

                models.ForEach(user => WriteObject(user));
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to execute QueryUserProfile in tenant {0}", this.ClientContext.Url);
            }
        }
Пример #3
0
        public static UserProfileInfo GetCurrent()
        {
            try
            {
                var query = ContextFactory.GetContextWeb();

                if (query.IsClient)
                {
                    var context = (ClientContext)query.Context;

                    var peopleManager = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(context);

                    Microsoft.SharePoint.Client.UserProfiles.PersonProperties personDetails =
                        peopleManager.GetMyProperties();

                    context.Load(personDetails,
                                 personsD => personsD.AccountName,
                                 personsD => personsD.Email,
                                 personsD => personsD.Title,
                                 personsD => personsD.PictureUrl,
                                 personsD => personsD.UserProfileProperties,
                                 personsD => personsD.DisplayName);

                    context.ExecuteQuery();

                    return(new UserProfileInfo
                    {
                        Department = personDetails.UserProfileProperties["Department"],
                        DisplayName = personDetails.DisplayName,
                        Email = personDetails.Email,
                        PictureUrl = personDetails.PictureUrl,
                        Title = personDetails.Title,
                        AccountName = personDetails.AccountName,
                        Properties = personDetails.UserProfileProperties
                    });
                }
                else
                {
                    //string currentUser = SPContext.Current.Web.CurrentUser.LoginName;
                    //SPSite spSite = SPContext.Current.Site;

                    var context = (SPWeb)query.Context;

                    SPServiceContext serviceContext = SPServiceContext.GetContext(context.Site);

                    var profileManager = new UserProfileManager(serviceContext);
                    var userProfile    = profileManager.GetUserProfile(context.CurrentUser.LoginName);

                    return(new UserProfileInfo
                    {
                        AccountName = userProfile.AccountName,
                        DisplayName = userProfile.DisplayName,
                        Department = userProfile["Department"].Value.ToString(),
                        Email = userProfile["Email"].Value.ToString(),
                        PictureUrl = userProfile["PictureUrl"].Value.ToString(),
                        Title = userProfile["Title"].Value.ToString(),
                        Properties = userProfile.Properties.ToDictionary(p => p.Name, p => p.Name)
                    });
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }