예제 #1
0
        private CoreProperty EnsureCoreProperty(
            SPSite site,
            UserProfilePropertyInfo userProfilePropertyInfo,
            CorePropertyManager userProfileCoreProperties)
        {
            // If property is null, create it. Else, update it
            var property = userProfileCoreProperties.GetPropertyByName(userProfilePropertyInfo.Name);

            if (property == null)
            {
                this.logger.Info("Creating user profile property '{0}'", userProfilePropertyInfo.Name);

                // Create property (false here means it's not a section)
                property               = userProfileCoreProperties.Create(false);
                property.Name          = userProfilePropertyInfo.Name;
                property.Type          = userProfilePropertyInfo.PropertyDataType;
                property.Length        = userProfilePropertyInfo.Length;
                property.IsMultivalued = userProfilePropertyInfo.IsMultivalued;
            }
            else
            {
                this.logger.Info("Updating user profile property '{0}'", userProfilePropertyInfo.Name);
            }

            property.DisplayName  = userProfilePropertyInfo.DisplayName;
            property.Description  = userProfilePropertyInfo.Description;
            property.IsAlias      = userProfilePropertyInfo.IsAlias;
            property.IsSearchable = userProfilePropertyInfo.IsSearchable;

            // Setup localized display name
            if (userProfilePropertyInfo.DisplayNameLocalized.Count > 0)
            {
                foreach (var displayName in userProfilePropertyInfo.DisplayNameLocalized)
                {
                    property.DisplayNameLocalized[displayName.Key] = displayName.Value;
                }
            }

            // Setup localized description
            if (userProfilePropertyInfo.DescriptionLocalized.Count > 0)
            {
                foreach (var description in userProfilePropertyInfo.DescriptionLocalized)
                {
                    property.DescriptionLocalized[description.Key] = description.Value;
                }
            }

            // Setup taxonomy mappings if configured
            if (userProfilePropertyInfo.TermSetInfo != null)
            {
                var taxonomyCache = this.siteTaxonomyCacheManager.GetSiteTaxonomyCache(site, null, this.taxonomyHelper);
                var termStore     = userProfilePropertyInfo.TermSetInfo.ResolveParentTermStore(taxonomyCache.TaxonomySession);
                property.TermSet = termStore.GetTermSet(userProfilePropertyInfo.TermSetInfo.Id);
            }

            property.Commit();
            return(property);
        }
예제 #2
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();
                }
            }
        }
        private CoreProperty EnsureCoreProperty(
            SPSite site,
            UserProfilePropertyInfo userProfilePropertyInfo,
            CorePropertyManager userProfileCoreProperties)
        {
            // If property is null, create it. Else, update it
            var property = userProfileCoreProperties.GetPropertyByName(userProfilePropertyInfo.Name);
            if (property == null)
            {
                this.logger.Info("Creating user profile property '{0}'", userProfilePropertyInfo.Name);

                // Create property (false here means it's not a section)
                property = userProfileCoreProperties.Create(false);
                property.Name = userProfilePropertyInfo.Name;
                property.Type = userProfilePropertyInfo.PropertyDataType;
                property.Length = userProfilePropertyInfo.Length;
                property.IsMultivalued = userProfilePropertyInfo.IsMultivalued;
            }
            else
            {
                this.logger.Info("Updating user profile property '{0}'", userProfilePropertyInfo.Name);
            }

            property.DisplayName = userProfilePropertyInfo.DisplayName;
            property.Description = userProfilePropertyInfo.Description;
            property.IsAlias = userProfilePropertyInfo.IsAlias;
            property.IsSearchable = userProfilePropertyInfo.IsSearchable;

            // Setup localized display name
            if (userProfilePropertyInfo.DisplayNameLocalized.Count > 0)
            {
                foreach (var displayName in userProfilePropertyInfo.DisplayNameLocalized)
                {
                    property.DisplayNameLocalized[displayName.Key] = displayName.Value;
                }
            }

            // Setup localized description
            if (userProfilePropertyInfo.DescriptionLocalized.Count > 0)
            {
                foreach (var description in userProfilePropertyInfo.DescriptionLocalized)
                {
                    property.DescriptionLocalized[description.Key] = description.Value;
                }
            }

            // Setup taxonomy mappings if configured
            if (userProfilePropertyInfo.TermSetInfo != null)
            {
                var taxonomyCache = this.siteTaxonomyCacheManager.GetSiteTaxonomyCache(site, null, this.taxonomyHelper);
                var termStore = userProfilePropertyInfo.TermSetInfo.ResolveParentTermStore(taxonomyCache.TaxonomySession);
                property.TermSet = termStore.GetTermSet(userProfilePropertyInfo.TermSetInfo.Id);
            }

            property.Commit();
            return property;
        }