Skip to main content

Update multi valued properties in SharePoint User Profile withSharePoint Object Model (SSOM)

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.

untitled

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 =&gt; " + stringProp);
if (!string.IsNullOrEmpty(stringProp) &amp;&amp; stringProp != "AccountName")
{
propertyValue = row[stringProp].ToString();
Console.WriteLine("Updating property with value =&gt; " + 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(';') &gt; 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();

}
/// &lt;summary&gt;
/// Update multi valued properties in the SharePoint user profile
/// &lt;/summary&gt;
/// &lt;param name="accountName"&gt;Account name of the user&lt;/param&gt;
/// &lt;param name="property"&gt;Property name to be updated&lt;/param&gt;
/// &lt;param name="propertyValueCollection"&gt;Values to be updated&lt;/param&gt;
private static void UpdateUserProfileMultiValuedProperty(string accountName, string property, string[] propertyValueCollection)
{
if (accountName != "" &amp;&amp; property != "" &amp;&amp; propertyValueCollection.Length &gt; 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 =&gt;" + accountName + " value =&gt; " + propertyValueCollection + " property =&gt; " + property);
}

}

/// &lt;summary&gt;
/// Udpate Propertied with single value
/// &lt;/summary&gt;
/// &lt;param name="accountName"&gt;Account name of the Sharepoint user Profile&lt;/param&gt;
/// &lt;param name="property"&gt;Property name to be updated&lt;/param&gt;
/// &lt;param name="propertyValue"&gt;Value to be updated&lt;/param&gt;
private static void UpdateUserProfileProperty(string accountName, string property, string propertyValue)
{
if (accountName != "" &amp;&amp; property != "" &amp;&amp; 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 =&gt;" + accountName + " value =&gt; " + propertyValue + " property =&gt; " + property);
}

}

}
}

[/code]

References

https://msdn.microsoft.com/en-us/library/office/ms546301(v=office.14).aspx

Comments

Popular posts from this blog

Turn off/ Hide Details panel/ Information Panel on modern SharePoint lists

Not always we require to show changes done by other which is a mandatory feature in SharePoint online. 
















What is details pane (aka. Information Pane)? Detail pane/ Information shows information regarding the document if you selected a one or its showing recent changes within a list or library.

Follow linkto Microsoft documentation about details pane.

Bad news: Until Microsoft listen to User Voice, there is no straightforward way to enable disable this even you don't want.
Good news: We could write a SharePoint framework extension to hack styles until Microsoft give us a permanent solution.

How? I found this sample project (Inject CSS into modern SharePoint pages with React) which could reuse to our purpose. Thanks to Hugo for saving my time.  Steps to awesomeness: Clone the projectResolve dependencies > npm iBundle > gulp bundle --shipPackage > gulp package-solution --shipUpload package into SharePoint App catalog and deploy. You could find package under SharePoint folder.A…

PowerApps Delegation warning

Warning:Delegation warning. This part "Filter" of this formula might not work correctly on large datasets. The data source might not be able to process the formula and might return an incomplete data set. Your application might not return correct results or behave correctly if the data set is incomplete
"Working with large datasets requires using data sources and formulas that can be delegated" - PowerApps 










Solution!!!500 is the default number of records to pull, but you can change this number for an entire app. 

By altering the number of delegation queries (not recommended), you will be able to pull items up to 2000 (as of 2018). 
On the File tab, select App settings.Find Experimental features, change the Data row limit for non-delegable queries setting from 1 to 2000.










Data sources that will help with delegation:Common Data Service

📢 Update -Microsoft Teams IP Phones and Intune Enrollment

For customers who require desk phones and conference room phones to make and receive audio calls or join meetings, Microsoft Teams provides a growing portfolio of devices that can be purchased from our Teams Marketplace. For Teams phones including the Yealink T56A/T58A/CP960 and the Crestron Flex series IP phones that run on Android 5.x or later, there may be specific configurations that need to be enabled in the customer's tenant for the phones to successfully enroll into Intune. Allowing successful Intune enrollment for Android versions 5.x and upIf all the following conditions below are true, you will need to enable a specific configuration setting in the Intune admin console to allow for a successful enrollment:You are deploying a Teams IP phone with Android OS version 5.x or later.You have connected your Intune tenant with managed Google Play in order to manage Android Enterprise devices.You have configured your enrollment restrictions such that Android work profile enrollmen…