using System;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Administration;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
namespace GlobasIntranet.UpCommingBirthdayWSP
{
public partial class UpCommingBirthdayWSPUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// GetUserProfile();
//System.Diagnostics.Debugger.Launch();
//UserProfileManager pm = new UserProfileManager(SPServiceContext.Current);
//UserProfile UserProfile = pm.GetUserProfile("MOSS02\\adom");
GetUserProfile();
// System.Diagnostics.Debugger.Launch();
//int dd = GetDaysBeforeBirthday(Convert.ToDateTime("06/12/2012"));
}
}
private int GetDaysBeforeBirthday(DateTime birthdate)
{
DateTime nextBday = new DateTime(DateTime.Now.Year, birthdate.Month, birthdate.Day);
if (DateTime.Today > nextBday)
nextBday = nextBday.AddYears(1);
return (nextBday - DateTime.Today).Days;
}
private void GetUserProfile()
{
//System.Diagnostics.Debugger.Launch();
int index=0;
List<string> RemoveDuplicateUser = new List<string>();
DateTime Bday = new DateTime();
int indexChecker = 0;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
SPWebApplication webApp = site.WebApplication;
foreach (SPSite sitecol in webApp.Sites)
{
SPWebCollection webs = sitecol.AllWebs;
foreach (SPWeb web in webs)
{
if (!Equals(indexChecker, 0))
continue;
SPUserCollection users = web.SiteUsers;
foreach (SPUser user in users)
{
RemoveDuplicateUser.Add(user.LoginName);
}
// RemoveDuplicateUser = RemoveDuplicateUser.Distinct().ToList();
foreach (SPUser user in users)
{
//if (RemoveDuplicateUser.Contains(user.LoginName))
//{
// RemoveDuplicateUser.RemoveAll(e => e == user.LoginName);
//}
//else
//{
// continue;
//}
try
{
UserProfileManager pm = new UserProfileManager(SPServiceContext.Current);
UserProfile UserProfile = pm.GetUserProfile(user.ToString());
if (!string.IsNullOrEmpty(Convert.ToString(UserProfile[PropertyConstants.Birthday].Value)))
{
Bday=Convert.ToDateTime(Convert.ToString(UserProfile[PropertyConstants.Birthday].Value));
int dayCount = GetDaysBeforeBirthday(Convert.ToDateTime(Bday.Month+"/"+Bday.Day+"/"+DateTime.Now.Year));
if (dayCount < 5)
{
//Image image = new Image();
//image.ImageUrl = SPContext.Current.Site.Url + "/Style%20Library/Images/bdayface.jpg";
//image.ID = "img" + index;
//PlaceHolder.Controls.Add(image);
if (Equals(dayCount, 0))
{
Label lbl = new Label();
lbl.ID = "Lbl" + index;
lbl.Text = "Happy Birthday to "+ Convert.ToString(Convert.ToString(UserProfile[PropertyConstants.FirstName].Value));
PlaceHolder.Controls.Add(lbl);
PlaceHolder.Controls.Add(new LiteralControl("<br>"));
}
else
{
Label lbl = new Label();
lbl.ID = "Lbl" + index;
lbl.Text = Convert.ToString(Convert.ToString(UserProfile[PropertyConstants.FirstName].Value) + " " + Convert.ToString(Convert.ToDateTime(UserProfile[PropertyConstants.Birthday].Value).ToString("MMMM dd")));
PlaceHolder.Controls.Add(lbl);
PlaceHolder.Controls.Add(new LiteralControl("<br>"));
PlaceHolder.Controls.Add(new LiteralControl("<br>"));
}
index = index + 1;
}
}
}
catch { }
}
indexChecker = indexChecker + 1;
}
}
}
}
}
}
No comments:
Post a Comment