How to set Profile Picture in Office 365 SharePoint 2013 site using CSOM

Step1

Create a console application in Visual Studio 2013.

Step2

Import the following namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;

using System.Net;
using System.Security;
using System.Globalization; 

Step3

Add the following references

Step4

            Uri oUri = new Uri("https://yoursite.sharepoint.com");

            Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(oUri, "youruserid@yoursite.onmicrosoft.com", "password");
            using (ClientContext oClientContext = new ClientContext(oUri))
            {
                oClientContext.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
                PeopleManager oPeopleManager = new PeopleManager(oClientContext);
                
                Stream oStream = new FileStream(@"C:MyPhotoSundar.jpg", FileMode.Open);
                oPeopleManager.SetMyProfilePicture(oStream);
                oClientContext.ExecuteQuery();
                Console.Write("Profile picture is set");



                Console.ReadLine();
            }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

3 thoughts on “How to set Profile Picture in Office 365 SharePoint 2013 site using CSOM

  1. Pingback: How to set Profile Picture in Office 365 SharePoint 2013 site using CSOM - My experiments with SharePoint, Azure and .NET using Visual Studio

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.