コード例 #1
0
        /// <summary>
        /// Enumerates list of imported from 1C profile records and updates SP profiles
        /// </summary>
        /// <returns>true on success, false on failed</returns>
        bool UpdateProfiles()
        {
            int nAdded    = 0;
            int nModified = 0;

            try
            {
                string strSiteURL = Properties.Settings.Default.sharePointUserProfilesUrl;
                using (SPSite site = new SPSite(strSiteURL))
                {
                    SPWeb                 web     = site.OpenWeb();
                    SPServiceContext      context = SPServiceContext.GetContext(site);
                    UserProfileManager    upm     = new UserProfileManager(context);
                    ProfileSubtypeManager psm     = ProfileSubtypeManager.Get(context);

                    web.AllowUnsafeUpdates = true;

                    // choose default user profile subtype as the subtype
                    string         subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User);
                    ProfileSubtype subType     = psm.GetProfileSubtype(subtypeName);

                    try
                    {
                        imageUploader = new SPProfilePhotoUploader(web);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0}\nImages will not be updated", ex.ToString());
                        imageUploader = null;
                    }

                    double total   = users1C.Count;
                    double rec     = 100.0 / total;
                    double current = 0.0;

                    Console.Write("Updated 0%");
                    foreach (RecordUserProfile profile in users1C.GetProfiles())
                    {
                        RecordUserProfile profileFound       = null;
                        string            profileAccountName = "";
                        bool profileExists = false;

                        // If LDAP is refer to account name then verify if such account is existing
                        if (profile.AccountName != null &&
                            profile.AccountName.Trim().Length > 0 &&
                            Properties.Settings.Default.accountNameIsAKey == true)
                        {
                            if (upm.UserExists(profile.AccountName.Trim()))
                            {
                                profileAccountName = profile.AccountName.Trim();
                                profileExists      = true;
                            }
                        }
                        // If innIsAKey option is true and account is not found then try to find
                        // profile by INN
                        if (profileExists == false &&
                            profile.INN != null &&
                            profile.INN.Trim().Length > 0 &&
                            Properties.Settings.Default.innIsAKey)
                        {
                            profileFound = GetProfileByINN(profile);
                            if (profileFound != null)
                            {
                                profileAccountName = profileFound.AccountName;
                                profileExists      = true;
                            }
                        }
                        // Find by Last name, First name, Middle name (FIO) and Birthday
                        if (profileExists == false)
                        {
                            profileFound = GetProfileByPersonName(profile);
                            if (profileFound != null)
                            {
                                profileAccountName = profileFound.AccountName;
                                profileExists      = true;
                            }
                        }

                        if (profileExists)
                        {
                            // get existing profile
                            UserProfile p = upm.GetUserProfile(profileAccountName);

                            if (UpdateUserProfile(site, p, profile))
                            {
                                nModified++;
                            }
                        }
                        else
                        {
                            // create a user profile and set properties
                            if (CreateUserProfile(site, upm, profile, subType))
                            {
                                nAdded++;
                            }
                        }
                        current += rec;
                        Console.Write("\rUpdated {0:F}%", current);
                    }
                    Console.Write("\r");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("Updated {0} records.", nModified);
            Console.WriteLine("Added {0} new records.", nAdded);
            return(true);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: sergiygladkyy/Mriya
        /// <summary>
        /// Enumerates list of imported from 1C profile records and updates SP profiles
        /// </summary>
        /// <returns>true on success, false on failed</returns>
        bool UpdateProfiles()
        {
            int nAdded = 0;
            int nModified = 0;

            try
            {
                string strSiteURL = Properties.Settings.Default.sharePointUserProfilesUrl;
                using (SPSite site = new SPSite(strSiteURL))
                {
                    SPWeb web = site.OpenWeb();
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager upm = new UserProfileManager(context);
                    ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);

                    web.AllowUnsafeUpdates = true;

                    // choose default user profile subtype as the subtype
                    string subtypeName = ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User);
                    ProfileSubtype subType = psm.GetProfileSubtype(subtypeName);

                    try
                    {
                        imageUploader = new SPProfilePhotoUploader(web);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("{0}\nImages will not be updated", ex.ToString());
                        imageUploader = null;
                    }

                    double total = users1C.Count;
                    double rec = 100.0 / total;
                    double current = 0.0;

                    Console.Write("Updated 0%");
                    foreach (RecordUserProfile profile in users1C.GetProfiles())
                    {
                        RecordUserProfile profileFound = null;
                        string profileAccountName = "";
                        bool profileExists = false;

                        // If LDAP is refer to account name then verify if such account is existing
                        if (profile.AccountName != null &&
                            profile.AccountName.Trim().Length > 0 &&
                            Properties.Settings.Default.accountNameIsAKey == true)
                        {
                            if (upm.UserExists(profile.AccountName.Trim()))
                            {
                                profileAccountName = profile.AccountName.Trim();
                                profileExists = true;
                            }

                        }
                        // If innIsAKey option is true and account is not found then try to find
                        // profile by INN
                        if (profileExists == false &&
                            profile.INN != null &&
                            profile.INN.Trim().Length > 0 &&
                            Properties.Settings.Default.innIsAKey)
                        {
                            profileFound = GetProfileByINN(profile);
                            if (profileFound != null)
                            {
                                profileAccountName = profileFound.AccountName;
                                profileExists = true;
                            }
                        }
                        // Find by Last name, First name, Middle name (FIO) and Birthday
                        if (profileExists == false)
                        {
                            profileFound = GetProfileByPersonName(profile);
                            if (profileFound != null)
                            {
                                profileAccountName = profileFound.AccountName;
                                profileExists = true;
                            }
                        }

                        if (profileExists)
                        {
                            // get existing profile
                            UserProfile p = upm.GetUserProfile(profileAccountName);

                            if (UpdateUserProfile(site, p, profile))
                                nModified++;
                        }
                        else
                        {
                            // create a user profile and set properties
                            if (CreateUserProfile(site, upm, profile, subType))
                                nAdded++;
                        }
                        current += rec;
                        Console.Write("\rUpdated {0:F}%", current);
                    }
                    Console.Write("\r");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("Updated {0} records.", nModified);
            Console.WriteLine("Added {0} new records.", nAdded);
            return true;
        }