Tuesday, 20 July 2010

Modify multi value user profile properties in SharePoint with PowerShell

Following on from my previous article on how to modify single value user profile properties in SharePoint 2010 using PowerShell, this script shows how you can modify multi value properties, which are stored in the Keywords term set in the Managed Metadata service application. There is actually not a lot of difference between the two scripts – the only change being the part of the script that modifies the property. The key difference is that this script adds a term to the list of terms already present in the property rather than replacing the value as before.

The example I have used here is to add the term “SharePoint” to the list of skills for the user DOMAIN\phil.childs. If the term is already present in the Keywords term set, then it will retrieve it and add it to the user profile property. If it is not currently in the term set, then it will add it for you before populating the user profile property:

#Set up default variables
$mySiteUrl = "http://mysite"
$adAccount = "DOMAIN\phil.childs"
$upAttribute = "SPS-Skills"
$upAttributeValue = "SharePoint"

#Get site objects and connect to User Profile Manager service
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

#Check to see if user profile exists
if ($profileManager.UserExists($adAccount))
{
    #Get user profile
    $up = $profileManager.GetUserProfile($adAccount)
    $up[$upAttribute].Add($upAttributeValue)
    $up.Commit()
}
else
{
    write-host "Profile for user"$adAccount "cannot be found"
}

#Dispose of site object
$site.Dispose()

A few things to note with this script:

  • Make sure you have the correct permissions configured on the User Profile Service Application (see previous article for details)
  • As before, you will need to specify the My Site host site collection URL for the $mySiteUrl variable
  • You have to use the internal name of the user profile property when assigning the $upAttribute variable – e.g., “SPS-Skills” for the Skills property – this is also the same for single value properties, too (forgot to mention this in my previous article). You can find the internal name of a property by going to the Manage User Properties page of the User Profile Service Application and editing a property

Manage User Properties 

Along with adding terms to a multi value user profile property, you may also want to remove them. You can do this by using the same script above and replacing the $up[$upAttribute].Add($upAttributeValue) line with the following, which cycles through all the terms assigned to the user profile property and removes the term you have specified once found:

for ( [int]$i = 0; $i -lt $up[$upAttribute].Count; $i++ )
{
    if ($up[$upAttribute][$i] -eq $upAttributeValue) { $up[$upAttribute].RemoveAt($i) }
}

Finally, if you want to remove ALL terms from the user profile property (e.g., delete all terms from the Skills property for that user), then replace the $up[$upAttribute].Add($upAttributeValue) line with the following:

$up[$upAttribute].Clear()

29 comments:

  1. Thanks for the post.

    I used some of it as inspiration.

    One note: The line
    $up[$upAttribute].Add($upAttributeValue)
    Will fail if you execute it twice (i.e. for an existing term set value).

    ReplyDelete
  2. A crap, my bad.

    My comment above is only valid if you have bound the multi-valued field to a term set.

    ReplyDelete
  3. Thanks for this it's really useful. One thing that I am struggling with is being able to add multiple values into the multi-value field - e.g. someone has more than one skill (e.g. SharePoint 2007 and SharePoint 2010" how do you pass both through to the UPS via Powershell?

    ReplyDelete
  4. $myStringFromCSV = "SharePoint 2007; SharePoint 2010; SharePoint 2013"

    $values = $myStringFromCSV.Split(";")

    foreach ($value in $values)
    {
    $up["SPS-Responsibility"].Add($value)
    }

    $up.Commit()

    ReplyDelete
  5. Thank you - finally got a step in the right way :-)

    If the script should loop through all the users MySites and delete e.g. 3 tags - how should the script be extended? (I'm a NOOB to powershell)

    ReplyDelete
  6. With a range of available activities like sailing, swimming, snorkeling, water-sports, trekking, rafting, skiing, climbing, cycling, etc every moment can be a unique experience.have a peek at this web-site

    ReplyDelete
  7. Our digital marketing course in Chennai is targeted at those who are desirous of taking advantage of career opportunities in digital marketing. Join the very best Digital Marketing Course in Chennai. Get trained by an expert who will enrich you with the latest digital trends.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai
    Online Digital Marketing Training
    SEO Training in Chennai
    Digital Marketing Course
    Digital Marketing Training
    Digital Marketing Courses

    ReplyDelete
  8. Thank you for sharing an interesting and very useful article. And let me share an article about health here I believe this is useful. Thank you :)

    Cara Mengobati Kencing Nanah (Gonore) dengan Daun

    Sirih

    Obat Penghancur Kista Ovarium
    Obat Nyeri Tumit Tradisional

    ReplyDelete
  9. يلزم ان تهتم جيدا بغسل السجاد وقبل أن تحدث عملية نقله، نقوم بلف السجاد داخل بعضه لأجل أن لا يأخذ منطقة عظيمة ومن الأجود أن يلف السجاد بورق شفاف لأجل أن يتم حفظه من تراكم الأتربة عليه.
    شركة نقل عفش
    شركة نقل اثاث من الرياض الى جدة
    شركة نقل عفش من الرياض الى جدة
    شركة نقل عفش بجدة

    ReplyDelete
  10. نحن لدينا مجموعة عمل على أعلي مستوي من الخبرة والمهنية فى ميدان إبادة ومكافحة الحشرات المنزلية ، ادارة المؤسسة ادارة قوية حكيمة جعلت من شركتنا المؤسسة الاولي فى جازان فى مكافحة الحشرات والقوارض المنزلية ، بالاضافة الي المهندسيين الذين يجتهدون للوصول الي افضل المبيدات التي تسخدمها مؤسسة بخ الحشرات بجازان للقضاء على الحشرات المنزلية ، كما يبقى لدينا متخصصون بخ الحشرات بالمملكة الفنيين المختصين لرش وابادة الحشرات وخبراء اصتياد الفئران بالبيت والمندوبين الذين يأتون لمعاينة الموضع وفريق استقبال المكالمات جاهو للاستجابة إلى اسئلتكم واستفساراتكم فى اى وقت – لا تترددو بالاتصال بنا هذه اللّحظة .
    شركة مكافحة حشرات
    شركة مكافحة النمل الابيض بجازان
    شركة مكافحة حشرات بجازان
    شركة رش مبيدات بجازان

    ReplyDelete
  11. No matter what type of Michael Kors Bags Outlet each individual got, they all had one thing in common. We all know that getting something on sale is like winning a mini lottery, but to find a name brand handbag like Michael Kors Black Friday Sale for instance, discounted is like a slice of heaven on earth. I have personally attacked my husband with hugs and kisses when finding Official Coach Outlet Online at discounted prices.

    Michael Kors Factory Outlet stores can be found in malls all over. MK Outlet and other Coach merchandise can also be found in some boutiques. Inspired by the same material baseball gloves are made from, these Coach Bags On Sale Online are versatile and stylish. Since it's conception, the Michael Kors Handbags Outlet has been produced into a variety of designs, colors, shapes, and styles that have won top pick of millions of women the world over.

    New Michael Kors Bags are the most sought after handbag. Since Coach Outlet Online is one of the most recognized name brands in the world of fashion, you will find A-list celebrities down to small girls wearing them.

    tags:Coach Outlet|Coach Bags Factory Outlet|Coach Purses Outlet

    ReplyDelete