Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Facebook SDK 3.01 with C#.NET

, 10 Aug 2010 CPOL
This article shall demonstrate how to use Facebook SDK 3.01 functionalities with C#.NET
Facebook_demonstration.zip
Facebook_demonstration
Facebook_demonstration
bin
Debug
facebook.dll
Facebook.Winforms.dll
Facebook_demonstration.exe
Facebook_demonstration.pdb
Facebook_demonstration.vshost.exe
Facebook_demonstration.vshost.exe.manifest
Interop.SHDocVw.dll
obj
Debug
Facebook_demonstration.csproj.GenerateResource.Cache
Facebook_demonstration.exe
Facebook_demonstration.Form1.resources
Facebook_demonstration.pdb
Facebook_demonstration.Properties.Resources.resources
Refactor
ResolveAssemblyReference.cache
TempPE
Properties
Settings.settings
Facebook_demonstration.4.5.resharper.user
Facebook_demonstration.suo
_ReSharper.Facebook_demonstration
CachesImage.bin
ProjectModel
ProjectModel.dat
TodoCache
.version
8
17cf4c75.dat
WebsiteFileReferences
.version
WordIndex.New
.version
6
b8651cf.dat
Xaml
CacheProvider.dat
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Utility;
using Facebook.Winforms.Components;
using FacebookController;
using SHDocVw;

namespace Facebook_demonstration
{
    public partial class Form1 : Form
    {
        #region Class members
        readonly FacebookService fbService = new FacebookService();

        private ScreenShotFormatter screenShotFormatter;

        private List<long> friendsUids;

        private List<Point> friendsPositions;
        #endregion

        public Form1()
        {
            InitializeComponent();

            // 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);            
        }

        #region Button Handlers
        private void PublishToFriendWall_Click(object sender, EventArgs e)
        {
            PublishToAFriendWall();
        }

        private void PublishToMyWall_Click(object sender, EventArgs e)
        {
            PublishToMyWall();
        }

        private void screenShotButton_Click(object sender, EventArgs e)
        {
            PublishPhotos();
        }

        private void AddFriend_Click(object sender, EventArgs e)
        {
            AddNewFriend();
        }
        #endregion

        #region Facebook Interfaces
        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, FacebookException e)
        {

        }

        public void PublishToMyWall()
        {
            try
            {
                attachment att = new attachment
                {
                    name = "",
                    href = "",
                    caption = "has used the application",
                    media = new List<attachment_media>()
                };

                attachment_media_image attMEd = new attachment_media_image
                {
                    src = "",
                    href = ""
                };
                att.media.Add(attMEd);

                action_link a = new action_link
                {
                    text = "",
                    href = ""
                };
                IList<action_link> tempA = new List<action_link> { a };

                fbService.Stream.PublishAsync(myWallTextBox.Text, att, tempA, null, 0, PublishAsyncCompleted, null);
            }
            catch (Exception)
            {

            }
        }

        public bool PublishPhotos()
        {
            // Just for demonstration////////////////////////////////////////////////

            // The origin of the screen shot
            Point origin = new Point(0,0);

            // The size of it
            Point size = new Point(800,600);

            /////////////////////////////////////////////////////////////////////////

            // Used for the tagging feature//////////////////////////////////////////

            // Uids of friends to be tagged
            friendsUids = new List<long>();

            // Corresponding positions
            //Note: Positions are in terms of percentage relative to screen shot size 
            friendsPositions = new List<Point>();

            /////////////////////////////////////////////////////////////////////////
            
            try
            {
                screenShotFormatter = new ScreenShotFormatter(origin.X, origin.Y, size.X, size.Y);

                IList<album> albums = fbService.Photos.GetAlbums();

                string albumAid = "";
                foreach (album album in albums)
                {
                    // Album name to create - if doesn't exist - is "Trial Album"
                    if (album.name == "Trial Album")
                    {
                        albumAid = album.aid;
                        break;
                    }
                }

                // If not found, create it
                if (albumAid == "")
                {
                    fbService.Photos.CreateAlbumAsync("Trial Album", null, "Album description", CreateAlbumCallback, null);
                    return true;
                }

                fbService.Photos.UploadAsync(albumAid,
                    "A photo to remember.",
                    screenShotFormatter.
                        GetScreenShot(),
                    Enums.FileType.png,
                    UploadCallback,
                    null);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void CreateAlbumCallback(album album, object state, FacebookException e)
        {
            fbService.Photos.UploadAsync(album.aid,
                "A photo to remember.",
                screenShotFormatter.GetScreenShot(),
                Enums.FileType.png,
                UploadCallback,
                null);
        }

        private void UploadCallback(photo p, object state, FacebookException e)
        {
            if (friendsUids != null && friendsPositions != null)
                PhotoTagger(p.pid);
        }

        private void PhotoTagger(string photoPid)
        {
            for (int i = 0; i < friendsUids.Count; i++)
            {
                fbService.Photos.AddTag(photoPid, friendsUids[i], null, friendsPositions[i].X, friendsPositions[i].Y);
            }
        }

        public void AddNewFriend()
        {
            object o = null;
            InternetExplorer ie = new InternetExplorerClass();

            IWebBrowserApp wb = ie;
            wb.Visible = true;
            wb.ToolBar = 0;
            wb.Width = 600;
            wb.Height = 600;
            wb.Navigate("http://www.facebook.com/addfriend.php?id=" + uidToAddTextBox.Text, ref o, ref o, ref o, ref o);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

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