14.1.10

Facebook asp.net

Event Master:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

public partial class EventMaster : System.Web.UI.MasterPage
{
private const string FACEBOOK_API_KEY = "d768a2201d05348eef79b3dfcded6e8c";
private const string FACEBOOK_SECRET = "30efc267bdf2f8675a33d3920129dfbc";
private Api FacebookAPI;
private ConnectSession ConnectSession;

protected void Page_Load(object sender, EventArgs e)
{
ConnectSession = new ConnectSession(FACEBOOK_API_KEY, FACEBOOK_SECRET);
}
protected void lnkEventPermission_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("http://www.facebook.com/authorize.php?api_key={0}&v=1.0&ext_perm={1}", "d768a2201d05348eef79b3dfcded6e8c", Enums.ExtendedPermissions.create_event));
}
}
====================================

create Event:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

public partial class CreateEvent : System.Web.UI.Page
{
private const string FACEBOOK_API_KEY = "d768a2201d05348eef79b3dfcded6e8c";
private const string FACEBOOK_SECRET = "30efc267bdf2f8675a33d3920129dfbc";
private Api FacebookAPI;
private ConnectSession ConnectSession;

protected void Page_Load(object sender, EventArgs e)
{
ConnectSession = new ConnectSession(FACEBOOK_API_KEY, FACEBOOK_SECRET);
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
try
{
DateTime CurrentDate = System.DateTime.Now;
DateTime ChkStartDate = Convert.ToDateTime(txtStartDate.Text);
if (ChkStartDate < CurrentDate)
{
lblMsg.Text = "Start date Should be greater than or eqaual to current date";
return;
}

lblMsg.Text = txtEndDate.Text;
FacebookAPI = new Api(ConnectSession);
DateTime StartDate = new DateTime();
StartDate = Convert.ToDateTime(txtStartDate.Text);
DateTime EndDate = new DateTime();
EndDate = Convert.ToDateTime(txtEndDate.Text);
var NewEvent = new facebookevent
{
name = txtname.Text,
description = txtdescription.Text,
location = txtLocation.Text,
host = txtHost.Text,
event_type = txtType.Text,
event_subtype = txtSubType.Text

};
//AddHours(8) is in below line because of the difference in time between my server and Facebook server you can adjust it as per requirement.
NewEvent.start_time = DateHelper.ConvertDateToFacebookDate(new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, Convert.ToInt32(drpStartSession.SelectedItem.Value) + Convert.ToInt32(drpStartTime.SelectedItem.Value) , Convert.ToInt32(DrpMinuteStart.SelectedItem.Value), 0).AddHours(8));
NewEvent.end_time = DateHelper.ConvertDateToFacebookDate(new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, Convert.ToInt32(drpEndSession.SelectedItem.Value) + Convert.ToInt32(drpEndTime.SelectedItem.Value) , Convert.ToInt32(drpMinuteEnd.SelectedItem.Value), 0).AddHours(8));

long NewEventId = FacebookAPI.Events.Create(NewEvent);
lblMsg.Text = string.Format("Event ({0}) Created Successfully.", NewEventId);
}
catch (Exception err)
{
lblMsg.Text = err.Message;
}

}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
txtEndDate.Text = "";
txtdescription.Text = "";
txtHost.Text = "";
txtLocation.Text = "";
txtname.Text = "";
txtStartDate.Text = "";
txtSubType.Text = "";
txtType.Text = "";
}
protected void imgBack_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Default.aspx");
}
}
=======================================
edit Event:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;
using System.Collections;

public partial class EditEvent : System.Web.UI.Page
{
private const string FACEBOOK_API_KEY = "d768a2201d05348eef79b3dfcded6e8c";
private const string FACEBOOK_SECRET = "30efc267bdf2f8675a33d3920129dfbc";
private Api FacebookAPI;
long EventId;
private ConnectSession ConnectSession;
protected void Page_Load(object sender, EventArgs e)
{


ConnectSession = new ConnectSession(FACEBOOK_API_KEY, FACEBOOK_SECRET);
if (!IsPostBack)
{
//Function to get information about the current information about selected event for editing
GetEventAsperEid();
}
}

private void GetEventAsperEid()
{
try
{
Hashtable EventInfo = new Hashtable();
EventInfo = (Hashtable)(Session["SesEventInfo"]);
lbEid.Text = EventInfo["EventId"].ToString();
lblname.Text = EventInfo["Name"].ToString();
txtStartDate.Text = EventInfo["StartTime"].ToString();
txtEndDate.Text = EventInfo["EndTime"].ToString();
txtdescription.Text = EventInfo["Description"].ToString();
txtLocation.Text = EventInfo["Location"].ToString();
txtHost.Text = EventInfo["Host"].ToString();
}
catch
{
}
}
protected void imgCreate_Click(object sender, ImageClickEventArgs e)
{
try
{
DateTime CurrentDate = System.DateTime.Now;
DateTime ChkStartDate = Convert.ToDateTime(txtStartDate.Text);
if (ChkStartDate < CurrentDate)
{
lblMsg.Text = "Start date Should be greater than or eqaual to current date";
return;
}

Hashtable EventInfo = new Hashtable();
EventInfo = (Hashtable)(Session["SesEventInfo"]);
EventId = Convert.ToInt64(EventInfo["EventId"].ToString());
FacebookAPI = new Api(ConnectSession);
DateTime StartDate = new DateTime();
StartDate = Convert.ToDateTime(txtStartDate.Text);
DateTime EndDate = new DateTime();
EndDate = Convert.ToDateTime(txtEndDate.Text);
var NewEvent = new facebookevent
{
name = lblname.Text,
description = txtdescription.Text,
location = txtLocation.Text,
host = txtHost.Text,
event_type = txtType.Text,
event_subtype = txtSubType.Text
};
//AddHours(8) is in below line because of the difference in time between my server and Facebook server you can adjust it as per requirement.
NewEvent.start_time = DateHelper.ConvertDateToFacebookDate(new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, Convert.ToInt32(drpStartSession.SelectedItem.Value)+ Convert.ToInt32( drpStartTime.SelectedItem.Value), Convert.ToInt32(DrpMinuteStart.SelectedItem.Value), 0).AddHours(8));
NewEvent.end_time = DateHelper.ConvertDateToFacebookDate(new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, Convert.ToInt32(drpEndSession.SelectedItem.Value) + Convert.ToInt32(drpEndTime.SelectedItem.Value), Convert.ToInt32(drpMinuteEnd.SelectedItem.Value), 0).AddHours(8));

bool NewEventId = FacebookAPI.Events.Edit(EventId, NewEvent);
lblMsg.Text = string.Format("Event ({0}) Updated Successfully.", NewEventId);
}
catch (Exception err)
{
lblMsg.Text = err.Message;
}
}
protected void imgRefresh_Click(object sender, ImageClickEventArgs e)
{
lbEid.Text = "";
txtEndDate.Text = "";
txtdescription.Text = "";
txtHost.Text = "";
txtLocation.Text = "";
lblname.Text = "";
txtStartDate.Text = "";
txtSubType.Text = "";
txtType.Text = "";
}
protected void imgBack_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Default.aspx");
}
}

=======================================

Default page:;

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
using Facebook;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

public partial class Default : Facebook.Web.CanvasFBMLBasePage
{

private const string FACEBOOK_API_KEY = "d768a2201d05348eef79b3dfcded6e8c";
private const string FACEBOOK_SECRET = "30efc267bdf2f8675a33d3920129dfbc";
private Api FacebookAPI;
user FaceBookUser;
string AuthToken;
private ConnectSession ConnectSession;
protected void Page_Load(object sender, EventArgs e)
{
ConnectSession = new ConnectSession(FACEBOOK_API_KEY, FACEBOOK_SECRET);
AuthToken = Request.QueryString["auth_token"];
if (String.IsNullOrEmpty(AuthToken))
{
Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + "d768a2201d05348eef79b3dfcded6e8c" + @"&v=0.4\");
}
else
{
try
{
FacebookAPI = new Api(ConnectSession);

if (!IsPostBack)
{
//Functoin to get current user information.
FaceBookCurrentUser();
//Function to get events
FaceBookEvents();
//Function to get friend list
FacebookFriends();
}
}
catch(Exception err)
{
lblMessage.Text = "Please click connect button.";
}
}
}

private void FaceBookCurrentUser()
{
long uid = FacebookAPI.Users.GetLoggedInUser();
FaceBookUser = FacebookAPI.Users.GetInfo(uid);
lblUserFName.Text = FaceBookUser.first_name;
lblUserLName.Text = FaceBookUser.last_name;
lblDob.Text = FaceBookUser.birthday;
}
private void FaceBookEvents()
{
List LstPrpEventObj = new List();
IList EventList = FacebookAPI.Events.Get();
int i = 1;
foreach (facebookevent EventInList in EventList)
{
prpEvents ObjPrpevents = new prpEvents();
ObjPrpevents.EventId = EventInList.eid;
ObjPrpevents.EventNo = i.ToString();
ObjPrpevents.Name = EventInList.name.ToString();
ObjPrpevents.StartDate = EventInList.start_date.ToString();
ObjPrpevents.StartTime = DateHelper.ConvertUnixTimeToDateTime(EventInList.start_time).ToString();
ObjPrpevents.EndTime = DateHelper.ConvertUnixTimeToDateTime(EventInList.end_time).ToString();
ObjPrpevents.EndDate = EventInList.end_date.ToString();
ObjPrpevents.Description = EventInList.description.ToString();
ObjPrpevents.Location = EventInList.location.ToString();
ObjPrpevents.Host = EventInList.host.ToString();
i++;
LstPrpEventObj.Add(ObjPrpevents);
}
GrdEvent.DataSource = LstPrpEventObj;
GrdEvent.DataBind();
}
private void FacebookFriends()
{
IList MyFriendList = FacebookAPI.Friends.GetUserObjects();
List LstPrpFriendsObj = new List();
foreach (var Friend in MyFriendList)
{
prpfriends ObjPrpFriends = new prpfriends();
ObjPrpFriends.Dob = Friend.birthday;
ObjPrpFriends.Name = Friend.name;
ObjPrpFriends.RelationshipStatus = Friend.relationship_status;
ObjPrpFriends.Sex = Friend.sex;
LstPrpFriendsObj.Add(ObjPrpFriends);
}
rptrFriends.DataSource = LstPrpFriendsObj;
rptrFriends.DataBind();
lblrptrfriendsCount.Text = rptrFriends.Items.Count.ToString();
}
protected void GrdEvent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GrdEvent.PageIndex = e.NewPageIndex;
FaceBookEvents();
}
catch (Exception err)
{
lblMessage.Text = err.Message;
}
}
protected void btnCreateEvent_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("CreateEvent.aspx");
}
protected void GrdEvent_SelectedIndexChanged(object sender, EventArgs e)
{
long EventId = Convert.ToInt64(GrdEvent.SelectedDataKey.Value);
try
{
List LstEid = new List();
LstEid.Add(EventId);
IList EventList = FacebookAPI.Events.Get(LstEid);
//selected event Information saved in session "SesEventInfo" to get it on edit page.
foreach (facebookevent EventInList in EventList)
{
string[] StrStartTime = DateHelper.ConvertUnixTimeToDateTime(EventInList.start_time).ToString().Split(' ');
string[] StrEndTime = DateHelper.ConvertUnixTimeToDateTime(EventInList.end_time).ToString().Split(' ');
Hashtable EventInfo = new Hashtable();
EventInfo.Add("EventId", EventId);
EventInfo.Add("Name", EventInList.name);
EventInfo.Add("StartTime", StrStartTime[0].ToString());
EventInfo.Add("EndTime", StrEndTime[0].ToString());
EventInfo.Add("Description", EventInList.description.ToString());
EventInfo.Add("Location", EventInList.location.ToString());
EventInfo.Add("Host", EventInList.host.ToString());
Session["SesEventInfo"] = EventInfo;
}
}
catch (Exception err)
{
lblMessage.Text = err.Message;
}


Response.Redirect("EditEvent.aspx?Eid=" + EventId );
}
protected void GrdEvent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
long EventId = Convert.ToInt64(GrdEvent.DataKeys[e.RowIndex].Value);
FacebookAPI.Events.Cancel(EventId, "Cancel this message");
lblMessage.Text = "Event Canceled";
FaceBookEvents();
}
catch
{
}
}
}
//Property class for "FacebookFriends" function.
public class prpfriends
{
private String prpName, prpDob, prpRelationshipStatus,prpSex;
public string Name
{
get
{
return prpName;
}
set
{
prpName = value;
}
}
public string Dob
{
get
{
return prpDob;
}
set
{
prpDob = value;
}
}

public string RelationshipStatus
{
get
{
return prpRelationshipStatus;
}
set
{
prpRelationshipStatus = value;
}
}
public string Sex
{
get
{
return prpSex;
}
set
{
prpSex = value;
}
}
}
//Property class for "FaceBookEvents" function.
public class prpEvents
{
private String prpName, prpHost, prpStartDate, prpStartTime, prpEndDate, prpEndTime, prpEventNo, prpDescription,prpLocation;
private long prpEventId;
public long EventId
{
get
{
return prpEventId;
}
set
{
prpEventId = value;
}
}
public string EventNo
{
get
{
return prpEventNo;
}
set
{
prpEventNo = value;
}
}
public string Name
{
get
{
return prpName;
}
set
{
prpName = value;
}
}
public string Host
{
get
{
return prpHost;
}
set
{
prpHost = value;
}
}

public string StartDate
{
get
{
return prpStartDate;
}
set
{
prpStartDate = value;
}
}
public string StartTime
{
get
{
return prpStartTime;
}
set
{
prpStartTime = value;
}
}
public string EndDate
{
get
{
return prpEndDate;
}
set
{
prpEndDate = value;
}
}
public string EndTime
{
get
{
return prpEndTime;
}
set
{
prpEndTime = value;
}
}
public string Description
{
get
{
return prpDescription;
}
set
{
prpDescription = value;
}
}
public string Location
{
get
{
return prpLocation;
}
set
{
prpLocation = value;
}
}
}

No comments: