Click here to Skip to main content
11,428,825 members (62,612 online)
Click here to Skip to main content

Using Facebook SDK 3.01 with C#.NET

, 10 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article shall demonstrate how to use Facebook SDK 3.01 functionalities with C#.NET

Introduction

The interest in integrating desktop applications with social networks such as Facebook is growing day after day. That's because the ability of developing a software that is not just enclosed among a group of people, but can be shared exponentially among millions of users within days is remarkable.

This article shall demonstrate how to take Facebook applications to another level beyond web applications, but to desktop applications.

It's interesting to mention that due to implementing this integration into one of my applications, on the first day of UNOFFICIAL publishing - just informing around 5 friends about it - 75 people were registered into the application database!! This is mainly because of the wall publishing thing which I consider free publicity.

Background

To use this project, three things must be available:

  1. The Facebook Developer Toolkit : http://facebooktoolkit.codeplex.com/
  2. A .dll file called shdocvw.dll used to control Internet Explorer: http://www.dll-files.com/dllindex/dll-files.shtml?shdocvw
  3. A Facebook application whose application key shall be used within the project. One could get one by simply going to http://www.facebook.com/#!/developers/ and creating an application.

It is also assumed that the reader is oriented with basic Facebook features.

Using the Code

The project consists mainly of two classes, the Form1 class and is the main class, and ScreenShotFormatter class and is just called by Form1 when a screen shot is required using the corresponding button.

This article is mainly concerned with the main Facebook features that are:

  • Current user wall publishing
  • Current user's friends wall publishing
  • Photo upload
  • Album creation
  • Friendship request
  • Automatic photo tagging

Each of the above functionalities is represented using its own function within the code with a corresponding GUI interface.

Main_Form.png

To begin using Facebook APIs, one should start a FacebookService giving it the application API (refer to Background section). Also, according to the application, some user permissions may be needed to be acquired. To get a list of permissions, you can go to http://developers.facebook.com/docs/authentication/permissions.

 // The application key of the Facebook application used
 fbService.ApplicationKey = "";

 // Add all needed permissions
 List<Enums.ExtendedPermissions> perms = new List<Enums.ExtendedPermissions>
                                           {
                                                 Enums.ExtendedPermissions.none
                                           };
 fbService.ConnectToFacebook(perms); 

Important note: Some permissions are not yet listed in the current SDK, so I sometimes need to add them in the Enums.ExtendedPermissions class and re-compile the source.

After a successful initialization of the FacebookService, running the application now shall result in the following dialog that is used to start a session between a user and Facebook servers.

FB_Login.png

Now for the fun stuff. I used the Facebook APIs asynchronously due to the latency that may be noticed due to network issues. It goes like that...

For publishing, the main function used here is Stream.PublishAsync. All I have done is write a neat interface to use this function along with the required callback (a function that is called when the asynchronous result is received successfully).

 public void PublishToAFriendWall()
        {
            try
            {
                attachment att = new attachment
                {
                    // Name of link
                    name = "",
                    // URL of link
                    href = "",
                    caption = "",
                    media = new List<attachment_media>()
                };

                attachment_media_image attMEd = new attachment_media_image
                {
                    // Image source
                    src = "",
                    // URL to go to if clicked
                    href = ""
                };
                att.media.Add(attMEd);

                action_link a = new action_link
                {
                    text = "What's this",
                    //URL to go to if clicked
                    href = ""
                };
                IList<action_link> tempA = new List<action_link> { a };

                // Use the typed friend UID to publish the typed message
                fbService.Stream.PublishAsync(friendWallTextBox.Text, att, 
			tempA, uidTextBox.Text, 0, PublishAsyncCompleted, null);
            }
            catch (Exception)
            {

            }
        }

        private static void PublishAsyncCompleted(string result, Object state, 
		FacebookExeption e)
        {

        } 

For the other functionalities, the Facebook APIs used are:

  • Photos.CreateAlbumAsync for creating albums asynchronously
  • Photos.UploadAsync for uploading photos asynchronously
  • Photos.AddTag for tagging photos
  • And for the adding issue, I used the Internet Explorer DLL file (refer to Background section) to pop up a controlled Internet Explorer window showing the page http://www.facebook.com/addfriend.php?id= appended to it the uid of the user to add as a friend.

Mentioning on this article how exactly to use the above APIs is a bit frustrating (240 lines of code!!), so please refer to the attached project. It shows how exactly to use each and every API with the required objects and callbacks.

My Experience

Through my experience, I noticed some things that must be considered:

  • You need to give the FacebookService a valid API key obtained from the application created on Facebook.
  • When publishing, make sure the
  • attachment,attachment_media_image and action_link objects are given a name and reference. Or else you can just "NULL" them and it will be fine.
  • Using the synchronous Facebook APIs is DANGEROUS...the application just hangs!!
  • When auto tagging a photo, mind the following. Facebook doesn't work in pixels, meaning that if you pass to the API say 550px and 480px, nobody would be tagged. That is mainly because Facebook deals in percentage of the screen, so what you need to do is multiply the value by 100 and divide by the full width/height of the screen (or whatever area you're considering).
  • You also need to know that if the passed uid to the Photos.AddTag API is of a non-friend, then tagging would fail.
  • MUST MUST MUST ask for the suitable user permissions as mentioned at the beginning of the previous section or else nothing would work. And again, refer to http://developers.facebook.com/docs/authentication/permissions for all available permissions, and again, if a permission is not listed within Enums.ExtendedPermissions, you will have to enter it yourself in the source code of the Facebook SDK, recompile and use the new .dll file.

If you have done all this and still have issues, please contact me.

Points of Interest

As a Facebook Developer Toolkit user, I must admit how excited I was to be able to use nearly all Facebook features within a desktop application. The main features are demonstrated throughout this article, however, many more are waiting to be tried... go on.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohamed Amr El Sisi
Engineer
Egypt Egypt
I graduated Computer Engineering - Cairo University in 2010 and currently I'm a software development engineer at Mentor Graphics Egypt.
Follow on   Twitter

Comments and Discussions

 
QuestionExtract DateTime for Like/Comment/status update for any cotact Pin
prash4812-Apr-15 22:28
memberprash4812-Apr-15 22:28 
QuestionI am receiving this error in this article how to solve it Pin
heemanshubhalla15-Jun-14 2:43
memberheemanshubhalla15-Jun-14 2:43 
QuestionLlave invalida para el SDK de Facebook Pin
Member 36756043-Jul-13 14:17
memberMember 36756043-Jul-13 14:17 
QuestionUsing Facebook SDK 3.01 with C#.NET Pin
Gaurav_Chhabra17-Jun-13 11:40
memberGaurav_Chhabra17-Jun-13 11:40 
AnswerRe: Using Facebook SDK 3.01 with C#.NET Pin
Member 36756043-Jul-13 10:12
memberMember 36756043-Jul-13 10:12 
QuestionError Pin
Esha Garg12-Mar-13 22:09
memberEsha Garg12-Mar-13 22:09 
AnswerRe: Error Pin
Gaurav_Chhabra17-Jun-13 12:58
memberGaurav_Chhabra17-Jun-13 12:58 
QuestionRead messages from wall Pin
lazartefederico3-Sep-12 5:53
memberlazartefederico3-Sep-12 5:53 
QuestionUpload a photo to a fanpage Pin
kavano27-Jun-12 3:17
memberkavano27-Jun-12 3:17 
GeneralMy vote of 4 Pin
Burak Tunçbilek11-Jun-12 10:35
memberBurak Tunçbilek11-Jun-12 10:35 
QuestionFacebook Photo Upload without logging in Pin
Mel Morris Cerbito18-May-12 17:06
memberMel Morris Cerbito18-May-12 17:06 
AnswerRe: Facebook Photo Upload without logging in Pin
Mel Morris Cerbito18-May-12 17:14
memberMel Morris Cerbito18-May-12 17:14 
QuestionProblem/Bug Pin
AmirAmirA16-Apr-12 10:09
memberAmirAmirA16-Apr-12 10:09 
AnswerRe: Problem/Bug Pin
Gaurav_Chhabra17-Jun-13 13:33
memberGaurav_Chhabra17-Jun-13 13:33 
Questionhow to add "like" button feature of facebook and follow us feature of twitter from c# window application?? Pin
vikastrust20051-Mar-12 1:39
membervikastrust20051-Mar-12 1:39 
QuestionKey Not found exception Pin
KiMO.133720-Feb-12 4:26
memberKiMO.133720-Feb-12 4:26 
QuestionSalam Muhammad How to upload a pic to the Group? Pin
Tassaduq00917-Feb-12 8:46
memberTassaduq00917-Feb-12 8:46 
QuestionMember Not Found Script error Sometimes when cancel button is clicked in login form Pin
knhema0622-Sep-11 4:14
memberknhema0622-Sep-11 4:14 
QuestionHow to show Progress bar while uploading photos? Pin
sujay kr singh18-Sep-11 21:29
membersujay kr singh18-Sep-11 21:29 
QuestionWhat happens with Login Form? Pin
DavidHarima15-Apr-11 8:23
memberDavidHarima15-Apr-11 8:23 
GeneralError Code 100 Invalid parameter Pin
krthk27-Mar-11 15:01
memberkrthk27-Mar-11 15:01 
Questionerror for other user Pin
jtkim7218-Mar-11 1:26
memberjtkim7218-Mar-11 1:26 
GeneralAbout retrieving the DOB and Educational details Pin
Anand Sonu20-Feb-11 21:54
memberAnand Sonu20-Feb-11 21:54 
GeneralAfter login how to change the user session and post with a new user [modified] Pin
EdgarMex200911-Feb-11 10:27
memberEdgarMex200911-Feb-11 10:27 
GeneralRe: After login how to change the user session and post with a new user [modified] Pin
Gaurav_Chhabra18-Jun-13 7:26
memberGaurav_Chhabra18-Jun-13 7:26 
Questionhi thanks for great article one q? Pin
fine_dost21-Dec-10 4:23
memberfine_dost21-Dec-10 4:23 
AnswerRe: hi thanks for great article one q? Pin
Mohamed Amr El Sisi21-Dec-10 4:26
memberMohamed Amr El Sisi21-Dec-10 4:26 
GeneralRe: hi thanks for great article one q? Pin
fine_dost22-Dec-10 13:01
memberfine_dost22-Dec-10 13:01 
QuestionConnect automatically (pass user and password from variables) Pin
fernandomierhicks7-Dec-10 14:04
memberfernandomierhicks7-Dec-10 14:04 
AnswerRe: Connect automatically (pass user and password from variables) Pin
Mohamed Amr El Sisi7-Dec-10 21:45
memberMohamed Amr El Sisi7-Dec-10 21:45 
GeneralPost a Message Pin
SulakshanaS1-Nov-10 19:40
memberSulakshanaS1-Nov-10 19:40 
GeneralRe: Post a Message Pin
Mohamed Amr El Sisi2-Nov-10 6:32
memberMohamed Amr El Sisi2-Nov-10 6:32 
GeneralConfiguration errors [modified] Pin
SulakshanaS1-Nov-10 3:26
memberSulakshanaS1-Nov-10 3:26 
GeneralRe: Configuration errors Pin
Mohamed Amr El Sisi3-Nov-10 3:38
memberMohamed Amr El Sisi3-Nov-10 3:38 
Generalproblems with authentication Pin
gianka!25-Oct-10 1:10
membergianka!25-Oct-10 1:10 
GeneralRe: problems with authentication Pin
Nuiarse1-Nov-11 6:43
memberNuiarse1-Nov-11 6:43 
GeneralRe: problems with authentication Pin
Gaurav_Chhabra17-Jun-13 13:30
memberGaurav_Chhabra17-Jun-13 13:30 
QuestionUse FB login to login to other site ? Pin
All Time Programming19-Oct-10 22:01
memberAll Time Programming19-Oct-10 22:01 
AnswerRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi19-Oct-10 22:19
memberMohamed Amr El Sisi19-Oct-10 22:19 
GeneralRe: Use FB login to login to other site ? Pin
All Time Programming19-Oct-10 22:35
memberAll Time Programming19-Oct-10 22:35 
GeneralRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi19-Oct-10 22:43
memberMohamed Amr El Sisi19-Oct-10 22:43 
GeneralRe: Use FB login to login to other site ? Pin
All Time Programming20-Oct-10 0:57
memberAll Time Programming20-Oct-10 0:57 
GeneralRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi20-Oct-10 1:03
memberMohamed Amr El Sisi20-Oct-10 1:03 
GeneralRe: Use FB login to login to other site ? Pin
All Time Programming20-Oct-10 1:47
memberAll Time Programming20-Oct-10 1:47 
GeneralRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi20-Oct-10 2:22
memberMohamed Amr El Sisi20-Oct-10 2:22 
GeneralRe: Use FB login to login to other site ? Pin
All Time Programming20-Oct-10 4:05
memberAll Time Programming20-Oct-10 4:05 
GeneralRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi20-Oct-10 4:19
memberMohamed Amr El Sisi20-Oct-10 4:19 
QuestionRe: Use FB login to login to other site ? Pin
All Time Programming26-Oct-10 3:04
memberAll Time Programming26-Oct-10 3:04 
AnswerRe: Use FB login to login to other site ? Pin
Mohamed Amr El Sisi26-Oct-10 3:29
memberMohamed Amr El Sisi26-Oct-10 3:29 
GeneralRe: Use FB login to login to other site ? Pin
All Time Programming27-Oct-10 23:53
memberAll Time Programming27-Oct-10 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 10 Aug 2010
Article Copyright 2010 by Mohamed Amr El Sisi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid