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; }

How to read UserProfile property from Office 365 SharePoint 2013 site using CSOM

Step#1

Create a console application in Visual Studio 2013

Step#2

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.Taxonomy;

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

Step#3

Add references to the following .dll

references

Step#3
 static void Main(string[] args)
        
        {
            ReadUserProfile();
            
           
        }

        public static void ReadUserProfile()
        {
            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);
                PersonProperties oPersonProperties = oPeopleManager.GetPropertiesFor("i:0#.f|membership|youruserid@yoursite.onmicrosoft.com");
                
                oClientContext.Load(oPersonProperties, p => p.AccountName, p => p.UserProfileProperties);
                oClientContext.ExecuteQuery();

                Console.WriteLine(oPersonProperties.UserProfileProperties.Count.ToString());
                
                foreach (var oProperty in oPersonProperties.UserProfileProperties)
                {
                    Console.WriteLine(string.Format("{0}: {1}",
                        oProperty.Key.ToString(), oProperty.Value.ToString()));
                    
                }
                Console.ReadLine();

            }

CSOM userprofile

The account parameter to be passed for the method oPeopleManager.GetPropertiesFor is not straight-forward. For office 365 SharePoint accounts, the parameter has to be passed in this format "i:0#.f|membership|youruserid@yoursite.onmicrosoft.com". Replace the youruserid accordingly.

The complete source code for this article is available in this location

 Subscribe to my blog

Visual Studio 2013 Community Launch @ Chennai–Dec 21, 2013

Date/Time:
Saturday, December 21, 2013 from 9:30 AM to 1:00 PM (PST)

Location:
Chennai

471, 2nd floor, Prestige Polygon, Anna Salai
Rathna Nagar, Teynampet
Chennai, 600018
India

Agenda & Schedule

No Agenda Schedule
1 Registration 9:30 – 10:00 AM
2 Visual Studio Studio 2013 IDE Changes and what’s new in .NET 4.5.1 10:00 AM – 11:00 AM
3 Tea Break 11:00 AM – 11:15 AM
4 Web Development in Visual Studio 2013 11:15 AM – 12:15 PM
5 Build HTML 5 based LOB Apps with Light Switch 12:15 PM – 1:00 PM
6 Q&A 1:00 AM – 1:15 AM

 Subscribe to my blog