I have a wrote post on updating SharePoint user profile with SharePoint Server Object model (SSOM) which is as below and this sample of code ellaborates on how you can update user profile properties of multi-valued.
Update below link with the latest
https://kushanlahiru.wordpress.com/2017/02/11/update-all-user-profile-properties-in-sharepoint-server-side-object-model-ssom
Update multi valued property
<pre class="brush: csharp">
//Higher permissions requires anyway to access secure data! So elevate
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user to udpate
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// If you need to persist existing values, comment below line
userProfile[property].Clear();
// Add a value to a multi-value user property..and mapping
foreach (var prop in propertyValueCollection)
userProfile[property].Add((object)prop);
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with values of : " + propertyValueCollection + "property :" + property);
});
</pre>
Important!
Multi value properties should be marked as multi valued in the managed user properties.

Check complete code below;
[code language="csharp" collapse="true"]
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SSOM_UserProfileUpdate
{
class Program
{
// Central Admin URL
static string siteUrl = "http://win2012:1412/";
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
Console.WriteLine("Start Working on update user profile poperties . . . ");
Console.WriteLine("");
#region Build profiles to update to Datatable
// This could be implemented with a CSV reading, reading Excel or any method you prefer
// Since Account name is act like primary key, it should the query key for the updates
DataTable dataTable = new DataTable();
dataTable.Columns.Add("AccountName");
dataTable.Columns.Add("SPS-Skills");
dataTable.Columns.Add("CustomProperty");
dataTable.Columns.Add("CustomMultiValued");
dataTable.Rows.Add(new Object[] { @"dev\lahiru", "Developer", "IT;HR;", "IT;Marketing;" });
dataTable.Rows.Add(new Object[] { @"dev\operator", "Developer", "IT;", "HR" });
#endregion Build profiles to update
#region User Profile Upate Logic
//Definitions
string accountName = "";
string propertyValue = "";
string stringProp = "";
//Iterate through data table
foreach (DataRow row in dataTable.Rows)
{
//Account name is the key
accountName = row["AccountName"].ToString();
Console.WriteLine("Updating properties of user : " + accountName);
foreach (var prop in dataTable.Columns)
{
stringProp = prop.ToString();
Console.WriteLine("Updating property => " + stringProp);
if (!string.IsNullOrEmpty(stringProp) && stringProp != "AccountName")
{
propertyValue = row[stringProp].ToString();
Console.WriteLine("Updating property with value => " + propertyValue);
//UpdateUserProfileProperty(@"MYDOMAIN\admin", "Metadata Property", "sample@sample");
if (!string.IsNullOrEmpty(propertyValue))
{
propertyValue = propertyValue.Trim(new char[] { ';' });
//check for multi valued properties
if (propertyValue.IndexOf(';') > 0)
{
string[] propertyValues = propertyValue.Split(';');
UpdateUserProfileMultiValuedProperty(accountName, stringProp, propertyValues);
}
else
{
UpdateUserProfileProperty(accountName, stringProp, propertyValue);
}
}
}
}
}
#endregion User Profile Upate Logic
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("User profile update success . . . !");
Console.ResetColor();
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("General Error: " + ex.ToString());
Console.ResetColor();
}
}
});
Console.ReadKey();
}
/// <summary>
/// Update multi valued properties in the SharePoint user profile
/// </summary>
/// <param name="accountName">Account name of the user</param>
/// <param name="property">Property name to be updated</param>
/// <param name="propertyValueCollection">Values to be updated</param>
private static void UpdateUserProfileMultiValuedProperty(string accountName, string property, string[] propertyValueCollection)
{
if (accountName != "" && property != "" && propertyValueCollection.Length > 0)
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user.
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// I need to push the values exactly I'm pushing.
// If you need to persist existing values, comment below line
userProfile[property].Clear();
// Add a value to a multivalue user property..and mapping
foreach (var prop in propertyValueCollection)
userProfile[property].Add((object)prop);
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with values of : " + propertyValueCollection + "property :" + property);
});
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error when updating account : " + accountName + "with value of : " + propertyValueCollection + "property :" + property);
Console.WriteLine(ex.ToString());
Console.ResetColor();
}
}
}
else
{
Console.WriteLine("Error on property values passed. account =>" + accountName + " value => " + propertyValueCollection + " property => " + property);
}
}
/// <summary>
/// Udpate Propertied with single value
/// </summary>
/// <param name="accountName">Account name of the Sharepoint user Profile</param>
/// <param name="property">Property name to be updated</param>
/// <param name="propertyValue">Value to be updated</param>
private static void UpdateUserProfileProperty(string accountName, string property, string propertyValue)
{
if (accountName != "" && property != "" && propertyValue != "")
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user.
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// Add a value to a multivalue user property.
//userProfile[PropertyConstants.PastProjects].Add((object)"Team Feed App");
//userProfile[PropertyConstants.PastProjects].Add((object)"Social Ratings View Web Part");
//Prop mapping
userProfile[property].Value = propertyValue;
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with value of : " + propertyValue + "property :" + property);
});
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error when updating account : " + accountName + "with value of : " + propertyValue + "property :" + property);
Console.WriteLine(ex.ToString());
Console.ResetColor();
}
}
}
else
{
Console.WriteLine("Error on property values passed. account =>" + accountName + " value => " + propertyValue + " property => " + property);
}
}
}
}
[/code]
References
https://msdn.microsoft.com/en-us/library/office/ms546301(v=office.14).aspx
Update below link with the latest
https://kushanlahiru.wordpress.com/2017/02/11/update-all-user-profile-properties-in-sharepoint-server-side-object-model-ssom
Update multi valued property
<pre class="brush: csharp">
//Higher permissions requires anyway to access secure data! So elevate
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user to udpate
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// If you need to persist existing values, comment below line
userProfile[property].Clear();
// Add a value to a multi-value user property..and mapping
foreach (var prop in propertyValueCollection)
userProfile[property].Add((object)prop);
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with values of : " + propertyValueCollection + "property :" + property);
});
</pre>
Important!
Multi value properties should be marked as multi valued in the managed user properties.

Check complete code below;
[code language="csharp" collapse="true"]
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SSOM_UserProfileUpdate
{
class Program
{
// Central Admin URL
static string siteUrl = "http://win2012:1412/";
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
Console.WriteLine("Start Working on update user profile poperties . . . ");
Console.WriteLine("");
#region Build profiles to update to Datatable
// This could be implemented with a CSV reading, reading Excel or any method you prefer
// Since Account name is act like primary key, it should the query key for the updates
DataTable dataTable = new DataTable();
dataTable.Columns.Add("AccountName");
dataTable.Columns.Add("SPS-Skills");
dataTable.Columns.Add("CustomProperty");
dataTable.Columns.Add("CustomMultiValued");
dataTable.Rows.Add(new Object[] { @"dev\lahiru", "Developer", "IT;HR;", "IT;Marketing;" });
dataTable.Rows.Add(new Object[] { @"dev\operator", "Developer", "IT;", "HR" });
#endregion Build profiles to update
#region User Profile Upate Logic
//Definitions
string accountName = "";
string propertyValue = "";
string stringProp = "";
//Iterate through data table
foreach (DataRow row in dataTable.Rows)
{
//Account name is the key
accountName = row["AccountName"].ToString();
Console.WriteLine("Updating properties of user : " + accountName);
foreach (var prop in dataTable.Columns)
{
stringProp = prop.ToString();
Console.WriteLine("Updating property => " + stringProp);
if (!string.IsNullOrEmpty(stringProp) && stringProp != "AccountName")
{
propertyValue = row[stringProp].ToString();
Console.WriteLine("Updating property with value => " + propertyValue);
//UpdateUserProfileProperty(@"MYDOMAIN\admin", "Metadata Property", "sample@sample");
if (!string.IsNullOrEmpty(propertyValue))
{
propertyValue = propertyValue.Trim(new char[] { ';' });
//check for multi valued properties
if (propertyValue.IndexOf(';') > 0)
{
string[] propertyValues = propertyValue.Split(';');
UpdateUserProfileMultiValuedProperty(accountName, stringProp, propertyValues);
}
else
{
UpdateUserProfileProperty(accountName, stringProp, propertyValue);
}
}
}
}
}
#endregion User Profile Upate Logic
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("User profile update success . . . !");
Console.ResetColor();
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("General Error: " + ex.ToString());
Console.ResetColor();
}
}
});
Console.ReadKey();
}
/// <summary>
/// Update multi valued properties in the SharePoint user profile
/// </summary>
/// <param name="accountName">Account name of the user</param>
/// <param name="property">Property name to be updated</param>
/// <param name="propertyValueCollection">Values to be updated</param>
private static void UpdateUserProfileMultiValuedProperty(string accountName, string property, string[] propertyValueCollection)
{
if (accountName != "" && property != "" && propertyValueCollection.Length > 0)
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user.
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// I need to push the values exactly I'm pushing.
// If you need to persist existing values, comment below line
userProfile[property].Clear();
// Add a value to a multivalue user property..and mapping
foreach (var prop in propertyValueCollection)
userProfile[property].Add((object)prop);
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with values of : " + propertyValueCollection + "property :" + property);
});
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error when updating account : " + accountName + "with value of : " + propertyValueCollection + "property :" + property);
Console.WriteLine(ex.ToString());
Console.ResetColor();
}
}
}
else
{
Console.WriteLine("Error on property values passed. account =>" + accountName + " value => " + propertyValueCollection + " property => " + property);
}
}
/// <summary>
/// Udpate Propertied with single value
/// </summary>
/// <param name="accountName">Account name of the Sharepoint user Profile</param>
/// <param name="property">Property name to be updated</param>
/// <param name="propertyValue">Value to be updated</param>
private static void UpdateUserProfileProperty(string accountName, string property, string propertyValue)
{
if (accountName != "" && property != "" && propertyValue != "")
{
using (SPSite site = new SPSite(siteUrl))
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
// Retrieve all properties for the "UserProfile" profile subtype,
// and retrieve the property values for a specific user.
ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
// Add a value to a multivalue user property.
//userProfile[PropertyConstants.PastProjects].Add((object)"Team Feed App");
//userProfile[PropertyConstants.PastProjects].Add((object)"Social Ratings View Web Part");
//Prop mapping
userProfile[property].Value = propertyValue;
// Save the changes to the server.
userProfile.Commit();
Console.WriteLine("Success on updating account : " + accountName + "with value of : " + propertyValue + "property :" + property);
});
}
//Catch exceptions here, ex. NullReferenceException
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error when updating account : " + accountName + "with value of : " + propertyValue + "property :" + property);
Console.WriteLine(ex.ToString());
Console.ResetColor();
}
}
}
else
{
Console.WriteLine("Error on property values passed. account =>" + accountName + " value => " + propertyValue + " property => " + property);
}
}
}
}
[/code]
References
https://msdn.microsoft.com/en-us/library/office/ms546301(v=office.14).aspx
Comments
Post a Comment