Example #1
0
        /// <summary>
        /// Get the localized label of User Profile Properties
        /// Option 1 => Get the specific property to find the label (much better performance)
        /// </summary>
        /// <param name="userProfilePropertyName"></param>
        /// <reference>
        ///       1. http://nikpatel.net/2013/12/26/code-snippet-programmatically-retrieve-localized-user-profile-properties-label/
        /// </reference>
        /// <returns></returns>
        public string getLocalizedUserProfilePropertiesLabel(string userProfilePropertyName, SPWeb currentWeb)
        {
            string localizedLabel = string.Empty;

            try
            {
                //Get the handle of User Profile Service Application for current site (web application)
                SPSite                   site    = SPContext.Current.Site;
                SPServiceContext         context = SPServiceContext.GetContext(site);
                UserProfileConfigManager upcm    = new UserProfileConfigManager(context);

                //Access the User Profile Property manager core properties
                ProfilePropertyManager ppm = upcm.ProfilePropertyManager;
                CorePropertyManager    cpm = ppm.GetCoreProperties();

                //Get the core property for user profile property and get the localized value
                CoreProperty cp = cpm.GetPropertyByName(userProfilePropertyName);
                if (cp != null)
                {
                    localizedLabel = cp.DisplayNameLocalized[System.Globalization.CultureInfo.CurrentUICulture.LCID];
                }
            }
            catch (Exception err)
            {
                LogErrorHelper objErr = new LogErrorHelper(LIST_SETTING_NAME, currentWeb);
                objErr.logSysErrorEmail(APP_NAME, err, "Error at getLocalizedUserProfilePropertiesLabel function");
                objErr = null;

                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(APP_NAME, TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, err.Message.ToString(), err.StackTrace);
                return(string.Empty);
            }
            return(localizedLabel);
        }
Example #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();
                }
            }
        }
Example #3
0
        public ProfileManager()
        {
            var farm = SPFarm.Local;

            if (farm == null)
            {
                throw new Exception("Provided SPFarm is null");
            }

            _farm = farm;
            var config = Configuration;

            if (config == null)
            {
                throw new Exception("Configuration is null");
            }

            var proxyGroup = farm.ServiceApplicationProxyGroups[config.ProxyGroup];

            if (proxyGroup == null)
            {
                var msg = "Failed to get Service Application Proxy Group with name '" + config.ProxyGroup + "'";
                throw new Exception(msg);
            }

            _context = SPServiceContext.GetContext(proxyGroup, SPSiteSubscriptionIdentifier.Default);

            if (_context == null)
            {
                var msg = "Failed to get Service Context";
                throw new Exception(msg);
            }

            _manager = new UserProfileManager(_context);

            if (_manager == null)
            {
                var msg = "Failed to get UserProfileManager";
                throw new Exception(msg);
            }

            _propertyManager = new UserProfileConfigManager(_context).ProfilePropertyManager;

            if (_propertyManager == null)
            {
                var msg = "Failed to get UserProfileConfigManager";
                throw new Exception(msg);
            }
        }
Example #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string html      = "";
            string userValue = "";
            UserProfileValueCollection valueCollection = null;
            object valueObject = null;

            _site                     = SPContext.Current.Site;
            _serviceContext           = SPServiceContext.GetContext(_site);
            _userProfileConfigManager = new UserProfileConfigManager(_serviceContext);
            _profilePropertyManager   = _userProfileConfigManager.ProfilePropertyManager;

            _profileManager = new UserProfileManager(_serviceContext);
            _profileSubtypePropertyManager = _profileManager.DefaultProfileSubtypeProperties;

            // if you need another profile subtype
            //_profileSubtypePropertyManager = _profilePropertyManager.GetProfileSubtypeProperties("ProfileSubtypeName");

            _corePropertyManager        = _profilePropertyManager.GetCoreProperties();
            _profileTypePropertyManager = _profilePropertyManager.GetProfileTypeProperties(ProfileType.User);

            UserProfile profile = _profileManager.GetUserProfile(true);

            html += "<h1>First listing out all of the property types themselves</h1>";

            html += "<h2>ProfileSubtypeProperty list</h2>";

            html += "<table cellspacing='2' border='1'>";

            html += "<tr><th>Section/Property</th><th>Name</th><th>Display Name</th><th>Type Property Name</th><th>Core Property Name</th><th>Display Order</th><th>Current User's Value</th></tr>";

            foreach (ProfileSubtypeProperty profileSubtypeProperty in _profileSubtypePropertyManager.PropertiesWithSection)
            {
                userValue = "";
                if (!profileSubtypeProperty.IsSection)
                {
                    userValue       = "<i>(none)</i>";
                    valueCollection = profile[profileSubtypeProperty.Name];
                    if (valueCollection != null)
                    {
                        valueObject = valueCollection.Value;
                        if (valueObject != null)
                        {
                            userValue = valueObject.ToString();
                        }
                    }
                }


                html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>",
                                      profileSubtypeProperty.IsSection ? "Section" : "Property",
                                      profileSubtypeProperty.Name,
                                      profileSubtypeProperty.DisplayName,
                                      profileSubtypeProperty.TypeProperty.Name,
                                      profileSubtypeProperty.CoreProperty.Name,
                                      profileSubtypeProperty.DisplayOrder,
                                      userValue);
            }

            html += "</table>";

            html += "<h2>ProfileTypeProperty list</h2>";

            html += "<table cellspacing='2' border='1'>";

            html += "<tr><th>Section/Property</th><th>Name</th><th>Core Property Name</th></tr>";

            foreach (ProfileTypeProperty profileTypeProperty in _profileTypePropertyManager.PropertiesWithSection)
            {
                html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",
                                      profileTypeProperty.IsSection ? "Section" : "Property",
                                      profileTypeProperty.Name,
                                      profileTypeProperty.CoreProperty.Name);
            }

            html += "</table>";

            html += "<h2>CoreProperty list</h2>";
            html += "<table cellspacing='2' border='1'>";

            html += "<tr><th>Section/Property</th><th>(Core Property) Name</th><th>Display Name</th><th>Type</th></tr>";

            foreach (CoreProperty coreProperty in _corePropertyManager.PropertiesWithSection)
            {
                html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                                      coreProperty.IsSection ? "Section" : "Property",
                                      coreProperty.Name,
                                      coreProperty.DisplayName,
                                      coreProperty.Type); //,
                //coreProperty.UseCount
                //"BUG!" );
            }

            html += "</table>";

            UserProfileValueCollection values = profile[PropertyConstants.PictureUrl];

            if (values.Count > 0)
            {
                // Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F}

                SPFieldUrlValue urlValue = new SPFieldUrlValue(values.Value.ToString());

                html += "<p><b>PictureUrl = " + urlValue.Url + "</b></p>";
            }

            html += "<p><b>User account = " + profile[PropertyConstants.AccountName].Value.ToString() + "</b></p>";


            Content.Text = html;
        }