protected void BtnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsPostBack && SPContext.Current != null)
                {
                    if (Fuprofileimage.HasFile)
                    {

                        var web = SPContext.Current.Site.RootWeb;

                        // uploading image to document library
                        var list = web.Lists.TryGetList(ProfilePictureLibrary);
                        if(list == null)
                        {
                            web.AllowUnsafeUpdates = true;
                            var listUID = web.Lists.Add(ProfilePictureLibrary, string.Empty, SPListTemplateType.PictureLibrary);
                            web.Update();
                            web.AllowUnsafeUpdates = false;
                            list = web.Lists[listUID];
                        }
                        if (list != null)
                        {
                            // now upload the file to document library
                            var file = list.RootFolder.Files.Add(list.RootFolder.Url + "/" + Fuprofileimage.FileName,Fuprofileimage.FileBytes,true);
                            file.Update();
                            var userProfileService = new UserProfileService.UserProfileService
                                                         {
                                                             UseDefaultCredentials = true,
                                                             Url = web.Site.Url + "/_vti_bin/userprofileservice.asmx",
                                                             AllowAutoRedirect = true
                                                         };
                            foreach (var account in PEaccount.Accounts)
                            {
                                var newdata = new PropertyData[1];
                                newdata[0] = new PropertyData { Name = "PictureURL", Values = new ValueData[1] };
                                newdata[0].Values[0] = new ValueData { Value = web.Url + "/" + file.Url };
                                newdata[0].IsValueChanged = true;
                                userProfileService.ModifyUserPropertyByAccountName(account.ToString(), newdata);
                            }
                            LblError.Text = "Profile pictures updated successfully.";
                        }
                        else
                            LblError.Text = "Profile pictures list not available";
                    }
                    else
                        LblError.Text = "Please select the profile picture and then update!";
                }
            }
            catch (Exception ex)
            {
                LblError.Text = "Error - " + ex.Message;
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("SkillLabTestScheduling", TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable, ex.Message, new object[] { ex.StackTrace });
            }
        }
コード例 #2
0
        /// <summary>
        /// Executes the business logic
        /// </summary>
        /// <param name="logger">The logger.</param>
        public override void Run(BaseAction parentAction, DateTime CurrentTime, LogHelper logger)
        {
            if (parentAction != null)
            {
                this.Properties = parentAction.Properties;
            }

            CsvProcessor csvProcessor = new CsvProcessor();

            string[] csvFiles = Directory.GetFiles(this.CSVDirectoryLocation, "*.csv", SearchOption.TopDirectoryOnly);

            logger.LogVerbose(string.Format("Attempting to get files from directory 'location' {0}. Number of files found {1}", this.CSVDirectoryLocation, csvFiles.Length));

            foreach (string csvFile in csvFiles)
            {
                logger.LogVerbose(string.Format("Attempting to read CSV file '{0}' from location {1}", csvFile, this.CSVDirectoryLocation));

                logger.LogVerbose(string.Format("Pausing the utility for '{0}' seconds so ASMX service is not overloaded", this.SleepPeriod));

                Thread.Sleep(this.SleepPeriod * 1000);

                using (StreamReader reader = new StreamReader(csvFile))
                {
                    logger.LogVerbose(string.Format("Establishing connection with tenant at '{0}'", this.TenantSiteUrl));

                    using (ClientContext context = new ClientContext(this.TenantSiteUrl))
                    {
                        Uri site = new Uri(this.TenantSiteUrl);

                        try
                        {
                            UserProfileService.UserProfileService profileService = new UserProfileService.UserProfileService(site.ToString() + ProfileService);
                            this.profileService = profileService;
                            profileService.UseDefaultCredentials = false;

                            using (SecureString password = new SecureString())
                            {
                                foreach (char c in this.TenantAdminPassword.ToCharArray())
                                {
                                    password.AppendChar(c);
                                }

                                logger.LogVerbose(string.Format("Attempting to authenticate against tenant with user name '{1}'", this.TenantSiteUrl, this.TenantAdminUserName));

                                var crudentials = new SharePointOnlineCredentials(this.TenantAdminUserName, password);

                                string cookie = crudentials.GetAuthenticationCookie(site);

                                profileService.CookieContainer = new CookieContainer();
                                profileService.CookieContainer.Add(new Cookie(FedAuthCookieName, cookie.TrimStart(SPOIDCookieValue.ToCharArray()), string.Empty, site.Authority));
                                csvProcessor.Execute(reader, (entries, y) => { IterateCollection(context, entries, logger); }, logger);
                            }
                        }
                        finally
                        {
                            if (this.profileService != null)
                            {
                                this.profileService.Dispose();
                            }
                        }
                    }
                }

                // Clean up current CSV file
                System.IO.File.Delete(csvFile);
            }
        }