User:C Sharp

From Facebook Developer Wiki

Jump to: navigation, search

Contents


[edit] Libraries

[edit] Getting Started

This section has been designed to give you details on how to begin using C# with the Facebook API. One of the easiest ways to begin coding with the Facebook API and C# is to download the Microsoft SDK for Facebook Platform from CodePlex.

For a step by step tutorial on how to get a C# project up and running for the first time see Steve Trefethen's blog on Facebook application development in ASP.NET. The article links to a VS.NET Starter Kit which will get you going quickly.

There also are some alternative C# libraries for Facebook. These will be detailed at the end of this article.

[edit] Assumptions

This article makes the following assumptions

  1. You have a basic knowledge of C#
  2. You have a basic understanding of Facebook
  3. You have a Facebook Development Account
  4. You have an IDE installed (preferably Visual Studio)
    1. Download a free version of Visual Studio Express

For more information that is outside the scope of this article please refer to other sections of the Facebook Developer Wiki, or the Microsoft website.

[edit] ASP.NET / C# Sample Code

The Microsoft SDK for Facebook Platform has fully launched. It is a substantially positive new version of the platform.

You can view a tutorial on How to Use the Facebook Developer Toolkit 2.0.

[edit] Code Samples

Some examples based on the Microsoft SDK for Facebook Platform. Please refer to the CodePlex website for more information.

[edit] How To

[edit] Get Online Friends

You can get online friends by getting a list of friends and checking each one to see if they're online.

This will return only the friends who are online, through one query to facebook (add to FacebookAPI.cs).

       public Collection<User> GetOnlineFriends()
      {
          Collection<string> onlineFriends = GetOnlineFriendIds();
          return GetUserInfo(StringHelper.ConvertToCommaSeparated(onlineFriends));
      }
       public Collection<string> GetOnlineFriendIds()
       {
           Collection<string> friendList = new Collection<string>();
           string xml = GetOnlineFriendsXML();
           if (!String.IsNullOrEmpty(xml))
           {
               XmlDocument xmlDocument = LoadXMLDocument(xml);
               XmlNodeList nodeList = xmlDocument.GetElementsByTagName("fql_query_response");
               if (nodeList != null && nodeList.Count > 0)
               {
                   XmlNodeList results = xmlDocument.GetElementsByTagName("user");
                   foreach (XmlNode node in results)
                   {
                       friendList.Add(XmlHelper.GetNodeText(node, "uid"));
                   }
               }
           }
               return friendList;
       }
       public string GetOnlineFriendsXML()
       {
           Dictionary<string, string> parameterList = new Dictionary<string, string>(3);
           parameterList.Add("method", "facebook.fql.query");
           
           if (!string.IsNullOrEmpty(_userId))
           {                
               parameterList.Add("query", 
                   String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}",
                                  "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=", _userId, ") AND 'active' IN online_presence"));
           
           }
           else
           {
               throw new FacebookException("User Id is required");
           }
           return ExecuteApiCallString(parameterList, true);
       }

Jim 08:36, 16 May 2008 (PDT)


[edit] Console Application

[edit] Get All Friends

Writes Friends to Console
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Facebook; // remember to add a reference to C:\Program Files\Coding4Fun\Facebook\Binaries\Facebook.dll to your project

namespace f8test
{
    class Program
    {
        private const string apikey = "YOUR API KEY";
        private const string apisecret = "YOUR SECRET";

        [STAThread]
        static void Main(string[] args)
        {
            Facebook.Components.FacebookService _fbService = new Facebook.Components.FacebookService();
            _fbService.ApplicationKey = apikey;
            _fbService.Secret = apisecret;
            // GetFriends() stores all User attributes in a Collection
            Collection<User> friends = _fbService.GetFriends(); // this will automatically trigger _fbService.ConnectToFacebook()
            foreach (User u in friends) {
                Console.WriteLine(u.Name);
            }
        }
    }
}

[edit] FQL Examples

[edit] Get Online Friends

This is the FQL query to get active online friends:

SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=USERID) AND 'active' IN online_presence

Be sure to change USERID to the uid of the logged in user.

[edit] ASP.NET MVC Preview 3 Example

There is an Addon for the FB Developer Toolkit that you can download that will make developing a Facebook MVC application a lot easier. Check it out on CodePlex: Facebook Developer Toolkit MVC Addon

[edit] Current Issues

[edit] Location.cs

(As of release 9824) Location.cs is missing an enumerator for UnitedKingdom. If you have friends in the UK, GetFriends() will error. A simple fix is to add UnitedKingdom to the enum Country. Then, compile the new library and use that.

Also, for those of you into bit-twiddling, you can force the enumerations to use byte as a base type (default is int). Just remember, a byte only ranges from 0-255. Another 20 Countries in the enumerator, and byte will crap out. If that happens, you can use ushort as the base type, which holds values 0 to 65,535.

Jim 08:31, 16 May 2008 (PDT)

[edit] Lost Session Variables Using IE6 in Frameset

Issues Described by Microsoft

The problem is that in IE, if a parent frame has a different domain than the child page, the session data (stored in the Session object) is not preserved as a security precaution.

If you have this problem, the answer is to add the following code to your ASP.NET code-behind.

   protected override void OnPreRender(EventArgs e)
   {
       Response.AppendHeader("P3P", "CP=\"CAO PSA OUR\"");
       base.OnPreRender(e);
   }

This will add the right headers to every page.

[edit] Additional Information

[edit] Updates to the Dev Kit from FB users