Category : .Net | Author : Chtiwi Malek | First posted : 6/13/2012 | Updated : 11/27/2012
Tags : facebook, wall, c#, api, graph, post, page, snippet, code, timeline, profile, sdk
Post pictures to a Facebook Page's Wall Photos

Post pictures to a Facebook Page's Wall Photos

In this article I will share a function I wrote that posts a picture to a user’s Wall/Timeline or one of his pages wall using the Facebook C# SDK.

The parameters needed are the picture’s full disk path, a description/caption to post along with the image and the Facebook user access token if you are posting to the user wall or the page access token if you are posting to one of his Facebook pages (there’s a difference between page access token and a user access token).

This function uses FQL to search the Album “Wall Pictures” Object ID, if the album is not found the function will exit.

private bool Post_Picture(string Picture_Path, string Picture_Caption, string AT, bool Is_User_Wall)
{
    // new FacebookClient instance (AT: user access token or page access token)
    var fb = new FacebookClient(AT);

    string AlbumID = "";
    if (!Is_User_Wall)
    {
        // Find the 'Wall Photos' Album Object_ID
        // alter this part to publish photos to a different album
        var query = string.Format("SELECT object_id,name FROM album WHERE owner = me()");
        dynamic r1 = fb.Get("fql", new { q = query });
        foreach (var post in r1.data)
        {
            if (post.name == "Wall Photos") { AlbumID = Convert.ToString(post.object_id); }
        }

        if (AlbumID == "") { MessageBox.Show("the album 'Wall Photos' not found."); return false; }
    }

    dynamic parameters = new ExpandoObject();
    parameters.message = Picture_Caption;
    parameters.source = new FacebookMediaObject
    {
        ContentType = "image/jpeg",
        FileName = Path.GetFileName(Picture_Path)
    }.SetValue(File.ReadAllBytes(Picture_Path));

    try
    {
        if (Is_User_Wall)
        {
            // Post the image/picture to User wall
            fb.Post("me/photos", parameters);
        }
        else
        {
            // Post the image/picture to the Page's Wall Photo album
            fb.Post("/" + AlbumID + "/photos", parameters);
        }

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

This function will return true if the picture is posted successfully.

Have fun and don't hesitate to comment or ask questions.

About the author :
Malek Chtiwi is the man behind Codicode.com
34 years old full stack developer.
Loves technology; but also likes design, photography and composing music.
Comments & Opinions :
Not able to post photo on page wall
I have used your code to post photo on page's wall so that to find available wall for existing page I have used below code by slight change with yours,

var query = string.Format("SELECT object_id,name FROM album WHERE owner = 89451217417");

Here 89451217417 is my page id,

I am able to get available Album with name "Wall Photos" and use that album ID to post photo but I am always getting error "provided album is not exist" and when I am trying to post feed without fb.Post("/89451217417/Feed/",Paremters) then I am not able to post picture that is only post messages even I have given picture in parameter,

So how can I accomplish this with your given approach.

You help will be really help to me.

Thanks,
Amit
- by Amit Patel on 9/4/2012
Got the solution for this with below code
Actually I was trying access token of my page

var fb = new FacebookClient.FacebookClient(AT);

// AT= Access tocken of your page not parent user tocken

dynamic parameters = new ExpandoObject();
parameters.message = Picture_Caption;
parameters.subject = "test 7979";
parameters.source = new FacebookMediaObject
{
ContentType = "image/jpeg",
FileName = Path.GetFileName(Picture_Path)
}.SetValue(File.ReadAllBytes(Picture_Path));

try
{
if (Is_User_Wall)
{
// Post the image/picture to User wall
fb.Post("me/photos", parameters);
}
else
{
// Post the image/picture to the Page's Wall Photo album
fb.Post("/368396933231381/", parameters); //368396933231381 is Album id for that page.
}

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

Please email me if you have any more question on this.
- by Amit Patel on 10/5/2012
the method not working
Do I need  need to change any argument?
- by aniket on 3/9/2013
fb sdkc#
How can I set permissions ?


Facebook.FacebookOAuthException: (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
- by pregunton on 11/17/2014
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :