public UserProfileService(ClientContext context, string siteUrl = "") : this()
        {
            if (string.IsNullOrEmpty(siteUrl))
            {
                if (!context.Site.IsPropertyAvailable(sctx => sctx.Url))
                {
                    context.Site.EnsureProperties(sctx => sctx.Url);
                    siteUrl = context.Site.Url;
                }
            }

            var trailingSiteUrl = siteUrl.EnsureTrailingSlashLowered();

            OWService = new OfficeDevPnP.Core.UPAWebService.UserProfileService
            {
                Url                   = $"{trailingSiteUrl}_vti_bin/userprofileservice.asmx",
                Credentials           = context.Credentials,
                UseDefaultCredentials = false
            };


            if (context.Credentials is SharePointOnlineCredentials)
            {
                var spourl     = new Uri(siteUrl);
                var newcreds   = (SharePointOnlineCredentials)context.Credentials;
                var spocookies = newcreds.GetAuthenticationCookie(spourl);

                var cookieContainer = new System.Net.CookieContainer();
                cookieContainer.SetCookies(spourl, spocookies);
                OWService.CookieContainer = cookieContainer;
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets the UserProfileService proxy to enable calls to the UPA web service.
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns>UserProfileService web service client</returns>
        public static OfficeDevPnP.Core.UPAWebService.UserProfileService GetUserProfileServiceClient(this Tenant tenant)
        {
            var client = new OfficeDevPnP.Core.UPAWebService.UserProfileService();

            client.Url = tenant.Context.Url + "/_vti_bin/UserProfileService.asmx";
            client.UseDefaultCredentials = false;
            client.Credentials           = tenant.Context.Credentials;

            if (tenant.Context.Credentials is SharePointOnlineCredentials)
            {
                var creds           = (SharePointOnlineCredentials)tenant.Context.Credentials;
                var authCookie      = creds.GetAuthenticationCookie(new Uri(tenant.Context.Url));
                var cookieContainer = new CookieContainer();

                cookieContainer.SetCookies(new Uri(tenant.Context.Url), authCookie);
                client.CookieContainer = cookieContainer;
            }
            return(client);
        }
Exemple #3
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPUserDefinitionModel>();

            try
            {
                var creds      = SPIaCConnection.CurrentConnection.GetActiveCredentials();
                var newcreds   = new System.Net.NetworkCredential(creds.UserName, creds.Password);
                var spourl     = new Uri(this.ClientContext.Url);
                var spocreds   = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(creds.UserName, creds.Password);
                var spocookies = spocreds.GetAuthenticationCookie(spourl);

                var spocontainer = new System.Net.CookieContainer();
                spocontainer.SetCookies(spourl, spocookies);

                var ows = new OfficeDevPnP.Core.UPAWebService.UserProfileService();
                ows.Url             = string.Format("{0}/_vti_bin/userprofileservice.asmx", spourl.AbsoluteUri);
                ows.Credentials     = newcreds;
                ows.CookieContainer = spocontainer;
                var UserProfileResult = ows.GetUserProfileByIndex(-1);
                var NumProfiles       = ows.GetUserProfileCount();
                var i              = 1;
                var tmpCount       = 0;
                var nextValue      = UserProfileResult.NextValue;
                var nextValueIndex = int.Parse(nextValue);

                // As long as the next User profile is NOT the one we started with (at -1)...
                while (nextValueIndex != -1)
                {
                    LogVerbose("Examining profile {0} of {1}", i, NumProfiles);

                    // Look for the Personal Space object in the User Profile and retrieve it
                    // (PersonalSpace is the name of the path to a user's OneDrive for Business site. Users who have not yet created a
                    // OneDrive for Business site might not have this property set.)

                    tmpCount++;

                    var PersonalSpaceUrl = UserProfileResult.RetrieveUserProperty("PersonalSpace");
                    var UserName         = UserProfileResult.RetrieveUserProperty("UserName");
                    if (!string.IsNullOrEmpty(UserName))
                    {
                        UserName = UserName.ToString().Replace(";", ",");
                    }

                    var userObject = new SPUserDefinitionModel()
                    {
                        UserName  = UserName,
                        OD4BUrl   = PersonalSpaceUrl,
                        UserIndex = nextValueIndex
                    };
                    models.Add(userObject);

                    // And now we check the next profile the same way...
                    UserProfileResult = ows.GetUserProfileByIndex(nextValueIndex);
                    nextValue         = UserProfileResult.NextValue;
                    nextValueIndex    = int.Parse(nextValue);
                    i++;
                }

                models.ForEach(user => WriteObject(user));
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to retreive user profiles");
            }
        }
        /// <summary>
        /// Process the request
        /// </summary>
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (this.ClientContext == null)
            {
                LogWarning("Invalid client context, configure the service to run again");
                return;
            }



            // obtain CSOM object for host web
            var vweb = this.ClientContext.Web;

            this.ClientContext.Load(vweb, hw => hw.SiteGroups, hw => hw.Title, hw => hw.ContentTypes);
            this.ClientContext.ExecuteQuery();


            GroupCollection groups = vweb.SiteGroups;

            this.ClientContext.Load(groups, g => g.Include(inc => inc.Id, inc => inc.Title, igg => igg.Users));
            this.ClientContext.ExecuteQuery();


            var creds      = SPIaCConnection.CurrentConnection.GetActiveCredentials();
            var newcreds   = new System.Net.NetworkCredential(creds.UserName, creds.Password);
            var spourl     = new Uri(this.ClientContext.Url);
            var spocreds   = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(creds.UserName, creds.Password);
            var spocookies = spocreds.GetAuthenticationCookie(spourl);

            var spocontainer = new System.Net.CookieContainer();

            spocontainer.SetCookies(spourl, spocookies);

            var ows = new OfficeDevPnP.Core.UPAWebService.UserProfileService();

            ows.Url             = string.Format("{0}/_vti_bin/userprofileservice.asmx", spourl.AbsoluteUri);
            ows.Credentials     = newcreds;
            ows.CookieContainer = spocontainer;

            var siteGroupUsers = new List <SPUserDefinitionModel>();

            var filteredGroups = groups.Where(w => SiteGroups.Any(a => w.Title.Equals(a, StringComparison.CurrentCultureIgnoreCase)));

            foreach (var group in filteredGroups)
            {
                foreach (var user in group.Users)
                {
                    if (!siteGroupUsers.Any(a => a.UserName == user.LoginName))
                    {
                        var userProfile = ows.GetUserProfileByName(user.LoginName);
                        //var userOrgs = ows.GetUserOrganizations(user.LoginName);
                        var UserName = userProfile.RetrieveUserProperty("UserName");
                        var office   = userProfile.RetrieveUserProperty("Department");
                        if (string.IsNullOrEmpty(office))
                        {
                            office = userProfile.RetrieveUserProperty("SPS-Department");
                        }

                        var userManager = userProfile.RetrieveUserProperty("Manager");

                        office = office.Replace(new char[] { '/', '\\', '-' }, ",");
                        office = office.Replace(" ", "");
                        var officeSplit   = office.Split(new string[] { "," }, StringSplitOptions.None);
                        var officeAcronym = (officeSplit.Length > 0) ? officeSplit[0] : string.Empty;

                        siteGroupUsers.Add(new SPUserDefinitionModel()
                        {
                            Manager             = userManager,
                            Organization        = office,
                            OrganizationAcronym = officeAcronym,
                            UserName            = user.LoginName,
                            UserEmail           = user.Email,
                            UserDisplay         = user.Title
                        });
                    }
                }



                WriteObject(siteGroupUsers);
            }
        }
        /// <summary>
        /// Gets the UserProfileService proxy to enable calls to the UPA web service.
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns>UserProfileService web service client</returns>
        public static OfficeDevPnP.Core.UPAWebService.UserProfileService GetUserProfileServiceClient(this Tenant tenant)
        {
            var client = new OfficeDevPnP.Core.UPAWebService.UserProfileService();

            client.Url = tenant.Context.Url + "/_vti_bin/UserProfileService.asmx";
            client.UseDefaultCredentials = false;
            client.Credentials = tenant.Context.Credentials;

            if (tenant.Context.Credentials is SharePointOnlineCredentials)
            {
                var creds = (SharePointOnlineCredentials)tenant.Context.Credentials;
                var authCookie = creds.GetAuthenticationCookie(new Uri(tenant.Context.Url));
                var cookieContainer = new CookieContainer();

                cookieContainer.SetCookies(new Uri(tenant.Context.Url), authCookie);
                client.CookieContainer = cookieContainer;
            }
            return client;
        }