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
Not able to post photo on page wall
Please respond me if you have any suggestion on my above posted question.

you can email me at : amit.patel@adaptivepeople.com

Thanks,
Amit Patel
- by Amit Patel on 9/10/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
fb.Get("fql", new { q = query });
- by aniket on 3/9/2013
the method not working
Do I need  need to change any argument?
- by aniket on 3/9/2013
Problem
Hi! i have a problem the image is upload success in album (fb return the image id) but its no posible view image after
I need some permission? please help  thanks!
- by Edward on 5/12/2013
Was working for over a month but....
In the past 2 days I have received this error:  

Facebook.FacebookOAuthException: (OAuthException - #1) An unknown error has occurred.

one facebook developer page said my access token was good and valid for another hour, but I still get the error.  Help!


 

- by Johnny A on 11/3/2013
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :


» Member access
 
» Latest
» Random/Popular
© Codicode | Content and source © 2013