Publish Sitecore Page as a Facebook Post aka Sitecore – Social Media Integration – Part 1

I am planning to write a series of posts on Managing Social Media Contents from Sitecore/Sitecore – Social Media Integration and this is the first of the series.

Social Media plays an integral part of Digital Marketing. Contents posted on a Facebook page is created from different sources. Although these contents have different origins, they are shared across Facebook, Twitter or any other social media.

These posts most probably contains links to our Website pages about our products/services. When I say different origins I meant that it also includes the CMSs where these pages are created in the first place.

Now in terms of CMS, once a particular page is created by the digital marketers, these links are shared across different pages/ twitter handles. Usually each organization would have their own Facebook Pages or Twitter Handles etc as a Social Media front. The admin/Editor of these pages share these links in those mediums by logging in to these Accounts.

Analytics details of these posts/tweets like interactions and how these posts are performing is important. The performance of these posts are decided my multiple factors and one of the undeniable parameter is the Content of this post – in simple terms, the post’s captions/Images. (Which is again a Content to us)

“Although we have Facebook Analytics, Twitter analytics to provide us that info, I personally felt that it creates a tiny separation between a Page and a Facebook Post even if the post is about the same page and I feel that it should be considered as a single entity.

When thinking about this deeper, what better place to manage those social media contents (Posts/Tweets) than it’s origin – CMS (which Sitecore in our case.)

Although I wanted to share a wide, more complete solution, I’m afraid whether I would be able to make time for doing it in near future hence this first part.

When I say wide, we can use Graph/Insights API and Twitter  Analytics API and create a Sitecore SPEAK application probably with graphs and tables as stats to show how each post associated with a Sitecore page is performing in different Social Media.

But as I said for now, I was able to make the first step alone. Hoping to work on this more and share the complete solution.

All the above paragraphs were meant to give you the answer for why this post.

Now coming back to the title, In this post, I am sharing my experience on creating a custom sitecore command that allows you to post a Sitecore page as Facebook Post from Sitecore Content Editor.

Of course this would require some stuffs like a Facebook Page, it’s admin credentials, Authorization token.

We’ll be using Facebook’s Graph API for posting to your Facebook Page.

Once, the APIs, Tokens are setup, we would be using a similar logic like  Create Custom Compress Command   and create a Custom Command named Publish to Facebook.

Getting to know about Graph API:

  • Login to https://developers.facebook.com and create an App. We will be making use of this app to post to Facebook via Sitecore.
  • Facebook has a decent documentation on it’s Graph API that is more than enough for us to get started.
  • You can access the documentation here
  • Once the App is created make note of it’s ID (APP ID)  and Secret Key (APP Secret)
  • Add Login product, setup page access token for the app for managing/publishing contents to the page.
  • For the time being I made use of Permanent Access Token, but for higher environment, this is not recommended and it is best practice to make use of Facebook’s OAuth 2.0 authentication. https://graph.facebook.com/{page-id}/feed?message=Hello_Fans!&access_token={page-access-token}
  • This is the API we will be using for posting the page content to Facebook.
  • For getting the access token, please refer.

Publish to Facebook Command

  • I wouldn’t be walking through on the steps to create a custom command. For more details on this as already mentioned above refer this post.
  • Here, I have made use of SEO Fields of a Page item and added them as Post captions. Of course we can create a separate field names Facebook Caption and make use of it.
  • After adding the necessary menu items in Core DB, the corresponding class and creating the corresponding config patches,
@Social Media 
Susi ng 
us ane 
using 
u51 ng 
Mark3.FeaturesSocialMedia.Publish.PostToFacebook 
Sitecore. Data. Items; 
Sitecore. Di agnost ics ; 
Sit ecore. Shell. F rm•ork. Cc—ands ; 
Sy stem; 
System. Collections. Generic ; 
System. Ling; 
System. Text; 
System _ Threading. Tasks; 
mark3. Features. . Repository; 
mark3 Jfoundati6iiL Si tecoreExtensicm. 
12 
Enamespace 
O refe 
public class PostToFacebook : CCNmand 
public override void Execute(ConnandContext context) 
Assert. (context. " context" 
Item item = context.ltems[øl; 
IPubIiShTOFacebcnkRePOSitor•y = publishTOFacebOOkRePOSitory(); 
string E?sponse — _pubIishTOFacebookRepository.PubIishFacebOOkPost(item); 
/ /WeryState 
public override Ct—andState QueryState(CornandContext context) 
Sf (context. Items. Length — 
Item currentlt& — context. 
if (currentltem.VisuaIization. Layout I—null) 
return base. Werystate(context); 
else 
return CormandState. Disabled; 
return base. ;
Social Media 
using 
using 
u51ng 
using 
using 
using 
Mark3.FeaturesSociaI Media.Repository.PublishTOFaceb00kRepc 
Mark3. Features . .l%dels; 
Newton S Oft. ; 
Si tecore. Data. ; 
System; 
System. 10 ; 
System. Net ; 
. Foundation. Sitecor•eExtensIon. ; 
Enamespace mark3. Features Repository 
class PublishToFacebookRepository : 
public string PublishPost(string 
string Respmse — string. E.ty; 
FacebookResponse response — new FacebookResponse(); 
string Access Token — "Permanent Access Token", 
string urI - 
"htrps: / 'eraph_ token-{1)"; 
string endpoint string. ; 
request 
request.Method — "POST"; 
request.ContentType — char-utf-8"; 
webRe%Xjnse = 
using (Stream stre— = 
if (stream null) 
using (StreamReader reader new 
string result; 
result - reader. ReadToEnd(); 
if 
response = JsonConvert . 
Respnse = id;
etch (kbExc%'tim webex) 
throw æbex; 
catch (Exception ex) 
throw ex; 
return Resmse; 
public String 
return it&. . Value; 
public string item) 
— FacebOOkPost() ; 
= SitecoreHelper 
p ageURL 
fbpost . PostCaption - 
string response — PublishPost(fbpost *ssage); 
•turn respnses
Guardian 
Home 
404 
El 500 
Around the World 
•O Business 
India Today 
Logistics 
Technology 
Sports 
Champions League permutations Who is thr 
Is Liverpool vs Man City the Premier League 
Shami Ashwin leave Bangladesh in tatters 
Article Page 
Shared Content 
MySite 
Insert 
Search 
Publish To Facebook 
Duplicate 
Delete 
Rename 
Copying 
Sorting 
Edit 
Experience Editor 
Publish Item 
Console 
Is Liverpool vs Man City the Premier League title decider 
components has been added to this page. 
you want to create a test? 
Create a test 
item is controlled by Unicorn 
nges to this item will be written to disk so they can be shared with others. 
figuration: Mark3DefauIt 
2dicate Component: Content 
Ysical path:D: Liverpoo 
{75106703-FCCF-4C21 -ACBI B7FC91 OED) 
Is Liverpool vs Man City the Premier League title decider
Guardian 
Home 
404 
.5 500 
Around the World 
•s' Business 
India Today 
Logistics 
Technology 
Sports 
Champions League permutations Who is through and who c 
Is Liverpool vs Man City the Premier League title decider 
Shami Ashwin leave Bangladesh in tatters 
Article Page 
Shared Content 
Meta Data Section 
Browser Title: 
Is Liverpool vs Man City the premier League title decider? 
Description: 
The two best teams in England clash in a colossal affair at Anfield this afternoon. Will the result decide where the Premier League title resides come May? 
Keywords: 
Football, News, Premier, League, Fixtures, Results 
Author. 
Guardian
posts 
i 'ubl•shed by Post to Page 2 mins 
The two best teams in England clash in a colossal affair at Anneld this 
afternoon. Will the result decide where the premier League title resides 
come May? 
https://cms.mark3.com/Sports/ls-Liverpool-vs-Man-City-the-Premier- 
League-title-decider 
Like 
Comment 
Boost Post 
Share

Similar to this we can create a Publish to Twitter command and make use of Twitter API to post it on corresponding twitter handle.

Leave a comment

Design a site like this with WordPress.com
Get started