Saturday, October 27, 2012

Current User's Group Owner details Using Client object model

Current User's Group Owner details Using Client object model

Here i am trying to get the Group owners Email Id. Similar way we can get all details of the group owner.



                //COM For Current User
                ClientContext COMforCurrentUser = new ClientContext("http://htschdev01:8000/sites/hr");
                COMforCurrentUser.Load(COMforCurrentUser.Web.CurrentUser);
                COMforCurrentUser.ExecuteQuery();
                User CurrentUser=COMforCurrentUser.Web.CurrentUser;

                // Smtp Server Details
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                //COM For Owner Details
                ClientContext clientContext = new ClientContext("http://htschdev01:8000/sites/hr");
                Web web = clientContext.Web;
                GroupCollection groupColl = web.SiteGroups;
                clientContext.Load(groupColl,
                groups=>groups.Include(
                group=>group.Title,
                group=>group.Id));
                clientContext.ExecuteQuery();
                int Ownerid = 0;
                string OwnerMailID = "";
                foreach (Group siteGroup in groupColl)
                  {
                clientContext.Load(siteGroup);
                clientContext.ExecuteQuery();
                clientContext.Load(siteGroup.Users);
                clientContext.ExecuteQuery();
                UserCollection Tolalusers = siteGroup.Users;
                int count = siteGroup.Users.Count;
                string Groupname = siteGroup.Title;            
                User SiteOWner;
                foreach (User singleuser in Tolalusers)
                {
                    if (singleuser.LoginName == CurrentUser.LoginName)
                    {
                        clientContext.Load(siteGroup);
                        clientContext.ExecuteQuery();
                        clientContext.Load(siteGroup.Owner);
                        clientContext.ExecuteQuery();
                        Ownerid = siteGroup.Owner.Id;
                        string Ownername = siteGroup.Owner.LoginName;
                        foreach (User Owner in Tolalusers)
                        {
                            if (Owner.Id == Ownerid)
                            {
                                OwnerMailID = Owner.Email;
                                break;
                            }
                        }                                            
                        break;
                    }                  
                }              
            }
                mail.From = new MailAddress(OwnerMailID);               
                mail.To.Add(CurrentUser.Email);              
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("XYZ@gmail.com", "Password");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");

No comments:

Post a Comment