Get User Profile Picture Programmatically using CSOM Project Online

Get User Profile Picture Programmatically using CSOM Project Online

Get User Profile Picture Programmatically using CSOM Project Online. In this post, you learn to Get User Profile Picture Programmatically using CSOM Project Online. The project has multiple resources called project team. If you need to get project team with profile picture, can find using below code. In this example, I explain to get Project Owner profile picture.

If you don’t know to add references in your solutions then follow the post below

Add  references in Solution

Get User Profile Picture Programmatically using CSOM Project Online:-

[AdSense-A]

 public static void GetProfilePicture(string UserName, string Password, string SiteURL)
        {
            string UserImage_Url = string.Empty;
            using (ProjectContext ProjectCont = new ProjectContext(SiteURL))
            {
                SecureString passWord = new SecureString();
                foreach (char c in Password.ToCharArray()) passWord.AppendChar(c);
                ProjectCont.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(UserName, passWord);
                SharePointOnlineCredentials orgIDCredential = new SharePointOnlineCredentials(UserName, passWord);

                ProjectCont.Load(ProjectCont.Projects,
               c => c.IncludeWithDefaultProperties(pr => pr.Name, pr => pr.Id, pr => pr.StartDate, pr => pr.Owner, pr => pr.FinishDate, pr => pr.PercentComplete));

                ProjectCont.ExecuteQuery();

                foreach (PublishedProject PublishProj in ProjectCont.Projects)
                {
                   
                    string targetuser = PublishProj.Owner.LoginName;
                    PeopleManager peopleManager = new PeopleManager(ProjectCont);
                    PersonProperties personProperties = peopleManager.GetPropertiesFor(targetuser);
                    ProjectCont.Load(personProperties, p => p.PictureUrl, p => p.UserProfileProperties);
                    ProjectCont.ExecuteQuery();
                    var UserProfile = personProperties.UserProfileProperties.Where(x=>x.Key=="PictureURL");
                    foreach (var item in UserProfile)
                    {
                        string url = item.Value;
                                if (!string.IsNullOrEmpty(url))
                                {
                                    Uri filename = new Uri(url);
                                    string serverURL = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
                                    string serverrelativePath = filename.AbsolutePath;

                                    ClientContext clientContext2 = new ClientContext(serverURL);
                                    SecureString passWord2 = new SecureString();
                                    foreach (char c in Password.ToCharArray()) passWord2.AppendChar(c);
                                    clientContext2.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
                                    FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext2, serverrelativePath);
                                    byte[] arr = ReadFully(f.Stream);
                                    UserImage_Url = Convert.ToBase64String(arr, 0, arr.Length);
                                    break;
                                }
                    }
                    
                }
            }
           
        }

[AdSense-A]

 public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

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

error: Content is protected !!