Ejemplo n.º 1
0
        static void MaritalStatus()
        {
            //Code example adds a new property called Marital Status.
            using (SPSite site = new SPSite("http://servername"))
            {
                //SPServiceContext context = SPServiceContext.GetContext(site);
                //ClientContext context = new ClientContext(site);
                //ServerContext context = ServerContext.GetContext(site);
                UserProfileConfigManager upcm = new UserProfileConfigManager();

                try
                {
                    ProfilePropertyManager ppm = upcm.ProfilePropertyManager;

                    // create core property
                    CorePropertyManager cpm = ppm.GetCoreProperties();
                    CoreProperty        cp  = cpm.Create(false);
                    cp.Name        = "MaritalStatus";
                    cp.DisplayName = "Marital Status";
                    cp.Type        = PropertyDataType.StringSingleValue;
                    cp.Length      = 100;

                    cpm.Add(cp);

                    // create profile type property
                    ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User);
                    ProfileTypeProperty        ptp  = ptpm.Create(cp);

                    ptpm.Add(ptp);

                    // create profile subtype property
                    ProfileSubtypeManager         psm  = ProfileSubtypeManager.Get();
                    ProfileSubtype                ps   = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
                    ProfileSubtypePropertyManager pspm = ps.Properties;
                    ProfileSubtypeProperty        psp  = pspm.Create(ptp);

                    psp.PrivacyPolicy  = PrivacyPolicy.OptIn;
                    psp.DefaultPrivacy = Privacy.Organization;

                    pspm.Add(psp);
                }
                catch (DuplicateEntryException e)
                {
                    Console.WriteLine(e.Message);
                    Console.Read();
                }
                catch (System.Exception e2)
                {
                    Console.WriteLine(e2.Message);
                    Console.Read();
                }
            }
        }
Ejemplo n.º 2
0
 static OrganizationProfile CreateOrganization(string displayName, long id, OrganizationProfile parentProfile, ProfileSubtype subtype, OrganizationProfileManager manager, SPSite site) {
     OrganizationProfile profile = manager.CreateOrganizationProfile(subtype, parentProfile);
     profile.DisplayName = displayName;
     using (var db = new CMSMIPEntities()) {
         IEnumerable<string> accountNames = db.CMS_SA_USER_INFO_V.Where(u => u.所属部门ID == id).Select(u => u.SP账号);
         foreach (var accountName in accountNames) {
             profile.AddMember(accountName, OrganizationMembershipType.Member);
         }
         profile.Commit();
         CreateSiteGroup(profile, accountNames, site);
         var subOrganizations = db.CMS_BA_IN_DEPT_INFO_V.Where(o => o.组织机构上级ID == id);
         foreach (var subOrganization in subOrganizations) {
             CreateOrganization(subOrganization.组织机构名称, subOrganization.机构ID, profile, subtype, manager, site);
         }
         return profile;
     }
     Console.WriteLine(displayName + id);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates new SharePoint user profile record
        /// </summary>
        /// <param name="site">SPSite object</param>
        /// <param name="upm">UserProfileManager object</param>
        /// <param name="profile">Profile record (imported from 1C) to be added</param>
        /// <param name="subType">Type of UserProfile</param>
        /// <returns>true on success, false on failed</returns>
        bool CreateUserProfile(SPSite site, UserProfileManager upm, RecordUserProfile profile, ProfileSubtype subType)
        {
            UserProfile p = null;
            string      strAccountName = "";
            string      strDisplayName = "";

            try
            {
                // create a user profile and set properties
                if (profile.AccountName.Trim().Length > 0)
                {
                    strAccountName = profile.AccountName;
                }
                else
                {
                    strAccountName = string.Format("SqlMembershipProvider:imported1c{0}", ++nAccount);
                }

                strDisplayName = string.Format("{0} {1}{2}",
                                               profile.FirstName.Trim(), (profile.MiddleName.Trim().Length > 0) ? (profile.MiddleName.Trim() + " ") : (""),
                                               profile.LastName);

                p = upm.CreateUserProfile(strAccountName);

                p.DisplayName = strDisplayName;
                WriteSPProfileField(ref p, "FirstName", profile.FirstName);
                WriteSPProfileField(ref p, "LastName", profile.LastName);
                WriteSPProfileField(ref p, "SPS-Birthday", profile.Birthday);

                WriteSPProfileField(ref p, "WorkPhone", profile.PhoneWork);
                WriteSPProfileField(ref p, "HomePhone", profile.PhoneHome);
                WriteSPProfileField(ref p, "WorkEmail", profile.EmailWork);
                WriteSPProfileField(ref p, "Office", profile.SeparateDivision);
                WriteSPProfileField(ref p, "Department", profile.SubDivision);
                WriteSPProfileField(ref p, "Title", profile.Position);
                WriteSPProfileField(ref p, "SPS-JobTitle", profile.Position);
                WriteSPProfileField(ref p, "SPS-HireDate", profile.DateOfEmployment);

                // Custom fields
                WriteSPProfileField(ref p, "Mr-MiddleName", profile.MiddleName);
                WriteSPProfileField(ref p, "Mr-Inn", profile.INN);
                WriteSPProfileField(ref p, "Mr-Ssn", profile.SSN);
                WriteSPProfileField(ref p, "Mr-Organization", profile.Organization);

                // Photos
                if (profile.Photo != null)
                {
                    if (imageUploader != null)
                    {
                        imageUploader.UploadPhoto(p, profile.Photo);
                        string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", site.Url,
                                                          imageUploader.GetSubfolderName(), imageUploader.GetFileNameFromAccount(p));
                        p["PictureUrl"].Value = pictureUrl;
                    }
                }
                else
                {
                }

                p.ProfileSubtype = subType;

                p.Commit();
            }
            catch (Exception ex)
            {
                string sError = string.Format("CreateUserProfile() The error was occured while updating profile:\nAccount name: {0}\nDisplayName: {1}\nError:\n\n{2}\n---\n",
                                              strAccountName, strDisplayName, ex.ToString());

                TextWriter errorWriter = Console.Error;
                Console.WriteLine(sError);
                errorWriter.WriteLine(sError);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates new SharePoint user profile record
        /// </summary>
        /// <param name="site">SPSite object</param>
        /// <param name="upm">UserProfileManager object</param>
        /// <param name="profile">Profile record (imported from 1C) to be added</param>
        /// <param name="subType">Type of UserProfile</param>
        /// <returns>true on success, false on failed</returns>
        bool CreateUserProfile(SPSite site, UserProfileManager upm, RecordUserProfile profile, ProfileSubtype subType)
        {
            UserProfile p = null;
            string strAccountName = "";
            string strDisplayName = "";

            try
            {
                // create a user profile and set properties
                if (profile.AccountName.Trim().Length > 0)
                    strAccountName = profile.AccountName;
                else
                    strAccountName = string.Format("SqlMembershipProvider:imported1c{0}", ++nAccount);

                strDisplayName = string.Format("{0} {1}{2}",
                    profile.FirstName.Trim(), (profile.MiddleName.Trim().Length > 0) ? (profile.MiddleName.Trim() + " ") : (""),
                    profile.LastName);

                p = upm.CreateUserProfile(strAccountName);

                p.DisplayName = strDisplayName;
                WriteSPProfileField(ref p, "FirstName", profile.FirstName);
                WriteSPProfileField(ref p, "LastName", profile.LastName);
                WriteSPProfileField(ref p, "SPS-Birthday", profile.Birthday);

                WriteSPProfileField(ref p, "WorkPhone", profile.PhoneWork);
                WriteSPProfileField(ref p, "HomePhone", profile.PhoneHome);
                WriteSPProfileField(ref p, "WorkEmail", profile.EmailWork);
                WriteSPProfileField(ref p, "Office", profile.SeparateDivision);
                WriteSPProfileField(ref p, "Department", profile.SubDivision);
                WriteSPProfileField(ref p, "Title", profile.Position);
                WriteSPProfileField(ref p, "SPS-JobTitle", profile.Position);
                WriteSPProfileField(ref p, "SPS-HireDate", profile.DateOfEmployment);

                // Custom fields
                WriteSPProfileField(ref p, "Mr-MiddleName", profile.MiddleName);
                WriteSPProfileField(ref p, "Mr-Inn", profile.INN);
                WriteSPProfileField(ref p, "Mr-Ssn", profile.SSN);
                WriteSPProfileField(ref p, "Mr-Organization", profile.Organization);

                // Photos
                if (profile.Photo != null)
                {
                    if (imageUploader != null)
                    {
                        imageUploader.UploadPhoto(p, profile.Photo);
                        string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", site.Url,
                            imageUploader.GetSubfolderName(), imageUploader.GetFileNameFromAccount(p));
                        p["PictureUrl"].Value = pictureUrl;
                    }
                }
                else
                {
                }

                p.ProfileSubtype = subType;

                p.Commit();
            }
            catch (Exception ex)
            {

                string sError = string.Format("CreateUserProfile() The error was occured while updating profile:\nAccount name: {0}\nDisplayName: {1}\nError:\n\n{2}\n---\n",
                    strAccountName, strDisplayName, ex.ToString());

                TextWriter errorWriter = Console.Error;
                Console.WriteLine(sError);
                errorWriter.WriteLine(sError);
                return false;
            }
            return true;
        }