September 4, 2013

Get User's Properties from Active Directory through profile synchronization.

​User's Property in Active directory are there like City, Job, Birthday, Hire Date etc.
what i have done here is displaying current week's birthday(i.e Sunday to satureday) and hire date with total years of joining from active directory through userprofile synchronization.
Before i write code here, make sure that user profile does contains properties which you need, If it is not there than add that property. Also user must be there, user profile synchronization service must be started

   1:  SPSecurity.RunWithElevatedPrivileges(delegate()
   2:              {
   3:                  using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url))
   4:                  {
   5:                      using (SPWeb web = site.OpenWeb())
   6:                      {
   7:                          try
   8:                          {
   9:   
  10:   
  11:                              SPServiceContext spServiceContext = SPServiceContext.GetContext(site);
  12:                                                                                  
  13:   
  14:   
  15:                              UserProfileManager profileManager = new UserProfileManager(spServiceContext);
  16:                              if (profileManager == null)
  17:                                  return;
  18:                              
  19:                              startDate = DateTime.Now;
  20:                              while (startDate.DayOfWeek != DayOfWeek.Sunday)
  21:                              {
  22:                                  startDate = startDate.AddDays(-1);
  23:                              }
  24:                              endDate = startDate.AddDays(6);                            
  25:                              startdate = startDate.Day;
  26:                              startmonth = startDate.Month;
  27:                              enddate = endDate.Day;
  28:                              endmonth = endDate.Month;
  29:                              IEnumerator profileEnumerator = profileManager.GetEnumerator();
  30:                              while (profileEnumerator.MoveNext())
  31:                             
  32:                              {
  33:                                  UserProfile userProfile = profileEnumerator.Current as UserProfile;
  34:                                  if (SPContext.Current != null)
  35:                                  {
  36:                                      string userLoginName = userProfile["AccountName"].ToString();
  37:                                                                                               
  38:                                  }
  39:                                  if (userProfile == null)
  40:                                      return;
  41:                                  DataRow drbirthday = dtBirthday.NewRow();
  42:                                  DataRow drAnniversary = dtAnniversary.NewRow();
  43:                                  if (userProfile[PropertyConstants.Birthday].Value != null)
  44:                                  {
  45:                                      BirthdayDate = (DateTime)userProfile[PropertyConstants.Birthday].Value;
  46:                                      if ((BirthdayDate.Day >= startdate && BirthdayDate.Month == startmonth) && (BirthdayDate.Day <= enddate && BirthdayDate.Month == endmonth))
  47:                                      {
  48:                                          drbirthday["Name"] = userProfile[PropertyConstants.PreferredName].Value.ToString();
  49:                                          drbirthday["Date"] = BirthdayDate.Month + "/" + BirthdayDate.Day;
  50:                                          drbirthday["fullstring"] = Convert.ToString( drbirthday["Date"]) + "-" + Convert.ToString( drbirthday["Name"]);
  51:                                          dtBirthday.Rows.Add(drbirthday);
  52:                                          birthDayData.Text = birthDayData.Text + (birthDayData.Text == string.Empty? string.Empty: "; ") +Convert.ToString(drbirthday["fullstring"]);
  53:                                      }                                   
  54:                                  }
  55:                                  if (userProfile[PropertyConstants.HireDate].Value != null)
  56:                                  {
  57:                                      AnniversaryDate = (DateTime)userProfile[PropertyConstants.HireDate].Value;
  58:                                      if ((AnniversaryDate.Day == date && AnniversaryDate.Month == month ))
  59:                                      {
  60:                                          int todayyear = DateTime.Today.Year;
  61:                                          int Anniyear = AnniversaryDate.Year;
  62:                                          int totalAnniYear = todayyear - Anniyear;
  63:                                          drAnniversary["Name"] = userProfile[PropertyConstants.PreferredName].Value.ToString();
  64:                                          drAnniversary["Years"] = totalAnniYear;
  65:                                          drAnniversary["fullstring"] = Convert.ToString(drAnniversary["Name"]) + "-" + Convert.ToString(drAnniversary["Years"] + " years");
  66:                                          dtAnniversary.Rows.Add(drAnniversary);
  67:                                          AnniDayData.Text = AnniDayData.Text + (AnniDayData.Text == string.Empty? string.Empty: ", ") + Convert.ToString(drAnniversary["fullstring"]);
  68:                                      }                                    
  69:                                  }
  70:                              }
  71:   
  72:   
  73:                          }
  74:                          catch (Exception ex)
  75:                          {
  76:                              Common.ErrorLog(ex.Message + Environment.NewLine + ex.StackTrace, "GetUserBirthdayAnniversary", Microsoft.SharePoint.Administration.TraceSeverity.Unexpected);
  77:                          }
  78:   
  79:   
  80:                      }
  81:                  }
  82:              });
This will helps to get user's properties from Active Directory.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment