[SharePoint 2010; SharePoint 2013]
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using System.Linq;
using System.Collections;
namespace GetProfileProperty
{
class Program
{
//Declare SKU keys of each SharePoint version
const string SHAREPOINT2010FOUNDATION = "BEED1F75-C398-4447-AEF1-E66E1F0DF91E";
const string SHAREPOINT2010STANDARD = "3FDFBCC8-B3E4-4482-91FA-122C6432805C";
const string SHAREPOINT2010ENTERPRISE = "D5595F62-449B-4061-B0B2-0CBAD410BB51";
const string SHAREPOINT2013STANDARD = "C5D855EE-F32B-4A1C-97A8-F0A28CE02F9C";
const string SHAREPOINT2013ENTERPRISE = "B7D84C2B-0754-49E4-B7BE-7EE321DCE0A9";
const string SHAREPOINT2013FOUNDATION = "9FF54EBC-8C12-47D7-854F-3865D4BE8118";
static void Main(string[] args)
{
string _skuID = string.Empty;
string CurrentSPVersion = string.Empty;
bool IsUserProfileServiceEnabled = false;
string result = string.Empty;
string PropertyName = "Unit System"; //User profile property name to get the value
string siteURL = "http://siteURL";
SPSite site = new SPSite(siteURL);
SPFarm farm = site.WebApplication.Farm;
//Get the SharePoint Products installed in current Farm
IEnumerable<Guid> _guid = farm.Products;
//Iterate each product to find the version installed
foreach (var item in _guid)
{
_skuID = item.ToString();
//Condition true if the version installed is Enterprise/Standard
if (_skuID.Equals(SHAREPOINT2010ENTERPRISE, StringComparison.CurrentCultureIgnoreCase) || _skuID.Equals(SHAREPOINT2010STANDARD, StringComparison.CurrentCultureIgnoreCase) || _skuID.Equals(SHAREPOINT2013ENTERPRISE, StringComparison.CurrentCultureIgnoreCase) || _skuID.Equals(SHAREPOINT2013STANDARD, StringComparison.CurrentCultureIgnoreCase))
{
CurrentSPVersion = "SPServer";
break;
}
//Condition true if the version installed is Foundation
if (_skuID.Equals(SHAREPOINT2010FOUNDATION, StringComparison.CurrentCultureIgnoreCase) || _skuID.Equals(SHAREPOINT2013FOUNDATION, StringComparison.CurrentCultureIgnoreCase))
{
CurrentSPVersion = "SPFoundation";
}
}
if (CurrentSPVersion == "SPServer")
{
//Get the service applications installed in the web application
Microsoft.SharePoint.SPServiceContext spServiceContext = Microsoft.SharePoint.SPServiceContext.GetContext(site);
IEnumerable<SPServiceApplicationProxy> proxies = site.WebApplication.ServiceApplicationProxyGroup.Proxies;
foreach (var services in proxies)
{
//Check if the User Profile Service is enabled
if (services.TypeName == "User Profile Service Application Proxy")
{
IsUserProfileServiceEnabled = true;
}
}
if (IsUserProfileServiceEnabled)
{
Console.WriteLine("User profile for this web application is enabled");
Console.WriteLine(GetUserProfilePropertyValue(siteURL, PropertyName, true));
}
else
{
Console.WriteLine("User profile for this web application is NOT enabled");
}
}
if (CurrentSPVersion == "SPFoundation")
{
Console.WriteLine("You have SP Foundation");
}
}
/// <summary>
/// Get the User Profile Property value
/// </summary>
/// <param name="property">User Profile Propery Name</param>
/// <param name="isUserProfileEnabled">boolean</param>
/// <returns></returns>
public static string GetUserProfilePropertyValue(string siteURL, string property, bool isUserProfileEnabled)
{
string result = "";
string CurrentWeb =siteURL;
try
{
//Get the value from My Settings/User Information List if User Profile Service is not enabled
if (!isUserProfileEnabled)
{
using (SPSite site = new SPSite(CurrentWeb))
{
using (SPWeb web = site.OpenWeb())
{
SPList userInfoList = web.SiteUserInfoList;
SPUser user = web.EnsureUser(@"domainname\username");
SPListItem userItem = userInfoList.Items.GetItemById(user.ID);
result = userItem[property].ToString();
}
}
}
else
{
//Read the user profile property value if user profile service application is enabled. Reads the value using User Profile Manager
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(CurrentWeb))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
string sAccount = @"domainname\username";
UserProfile userProfile = profileManager.GetUserProfile(sAccount);
ProfileSubtypePropertyManager psmanager = userProfile.Properties;
//Get the internal name from the display name
IEnumerable<string> PropertyInternalName = psmanager.Cast<ProfileSubtypeProperty>()
.Where(r => r.DisplayName.ToLower() == property.ToLower())
.Select(r => r.Name);
//Get the value of property
if(PropertyInternalName.Count()==1)
result = userProfile[PropertyInternalName.First().ToString()].Value.ToString();
}
});
}
}
catch (Exception)
{
}
return result;
}
}
}
PS: Above is a console application and some of the values are hard-coded, you need to change the code accordingly to your requirement/environment.
Tags:
- programmatically retrieve user profile property value using property display name
- Check SharePoint version/edition programmatically