SharePoint – Change User Profile Picture for Multiple

Posted: April 28, 2016 in Uncategorized
Tags: , , , , , , , ,

In this post I will show how to change User Profile Picture for Multiple .

Using Console Application in Visual Studio .Net, I deploy and change picture based on User ID (as custom column in CA from AD):

For Multiple User:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace UserProfilePicURL_v5
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite(“SiteURL”))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(context);
try
{
foreach (UserProfile aUser in myUserProfileManager)
{
string origUrl = (string)aUser[PropertyConstants.PictureUrl].Value;
string displayName = (string)aUser[PropertyConstants.AccountName].Value;
string hUID = (string)aUser[“UserID”].Value;

const int MaxLength = 5;

if (hUID != null)
{
// Case hUID.Length > MaxLength
if (origUrl != null && hUID.Length > MaxLength)
{
string nUID = hUID.Substring(5).ToString();
string newUrl = “PictureURL” + nUID + “.jpg”;
aUser[PropertyConstants.PictureUrl].Value = newUrl.ToString();
aUser.Commit();
Console.WriteLine(“Account Name: ” + displayName.ToString());
Console.WriteLine(“Update Account Image: ” + newUrl.ToString());
}
// Case hUID.Length > MaxLength & no IMG URL
if (String.IsNullOrEmpty(origUrl) && hUID.Length > MaxLength)
{
string nUID = hUID.Substring(5).ToString();
string newUrl = “PictureURL” + nUID + “.jpg”;
aUser[PropertyConstants.PictureUrl].Value = newUrl.ToString();
aUser.Commit();
Console.WriteLine(“Account Name: ” + displayName.ToString());
Console.WriteLine(“New Account Image: ” + newUrl.ToString());
}
//Case hUID.Length == MaxLength
if (hUID.Length == MaxLength)
{
string newUrl2 = “PictureURL”;
aUser[PropertyConstants.PictureUrl].Value = newUrl2.ToString();
aUser.Commit();
Console.WriteLine(“Account Name: ” + displayName.ToString());
Console.WriteLine(“N/A Account Image: ” + newUrl2.ToString());
}
}
else
{
string newUrl2 = “PictureURL”;
aUser[PropertyConstants.PictureUrl].Value = newUrl2.ToString();
aUser.Commit();
Console.WriteLine(“Account Name: ” + displayName.ToString());
Console.WriteLine(“No User ID Account: ” + newUrl2.ToString());
}
Console.Read();
}

}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);}}}}}

Happy SharePoint

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s