Twitter SharePoint webpart

This is an article will tell, you how to link SharePoint with twitter site. You can get data from twitter website and display the Twitter status, feed text, twitter date and Twitter screen details etc.

In SharePoint create a web part using visual studio. To connect with twitter site using SharePoint server object model.
Data required to pass as parameter in twitter screen are screen name, authentication token, authentication secrete token, authentication consumer key, authentication consumer secret token, authentication version, authentication signature method.

Create a request header using all the above parameters. Connect with Twitter using delegate policy to avoid security issues.
Make HttpWebResponse to the site and store it into object.Convert the response to JArray using using Newtonsoft.Json.Linq.dll
Loop into the response object using Array and get data (name, Status, feed text, twitter date and Twitter screen details and hyperlinks referred in a post)



Use following namespaces in web part.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Configuration;
using Microsoft.SharePoint;
using Microsoft;
using Newtonsoft.Json.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

On page load event write the following code according to your twitter screen and authentication tokens
string screen_name = “********”;
string resource_url = “https://api.twitter.com/1.1/statuses/user_timeline.json”;
var oauth_token = “64173525-GZ9LyRRw2Yg3qPypVn1eBlECSMAWJMxq9LtskJNz9”;
var oauth_token_secret = “n1rwmmxQj6E6yLREPqvZL8exQjncMEovbduPN8kq4NI”;
var oauth_consumer_key = “0banOoHlbeH1EmGhELWSg”;
var oauth_consumer_secret = “TXWIhUqypQI7oMH0RrAdxliZJQAkdXAGyLZC5DvbE”;

// oauth implementation details
var oauth_version = “1.0”;
var oauth_signature_method = “HMAC-SHA1”;

// unique request details
var oauth_nonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
– new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
string count = “10”;

// create oauth signature
var baseFormat = “count={0}&oauth_consumer_key={1}&oauth_nonce={2}&oauth_signature_method={3}” +
“&oauth_timestamp={4}&oauth_token={5}&oauth_version={6}&screen_name={7}”;

var baseString = string.Format(baseFormat,
Uri.EscapeDataString(count),
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(screen_name)

);

baseString = string.Concat(“GET&”, Uri.EscapeDataString(resource_url), “&”, Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
“&”, Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

// create the request header
var headerFormat = “OAuth oauth_nonce=\”{0}\”, oauth_signature_method=\”{1}\”, ” +
“oauth_timestamp=\”{2}\”, oauth_consumer_key=\”{3}\”, ” +
“oauth_token=\”{4}\”, oauth_signature=\”{5}\”, ” +
“oauth_version=\”{6}\””;

var authHeader = string.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
);

ServicePointManager.Expect100Continue = false;

ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
delegate(
object sender2,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
});

// make the request

var postBody = “screen_name=” + Uri.EscapeDataString(screen_name);//
resource_url += “?count=”+Uri.EscapeDataString(count)+”&” + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add(“Authorization”, authHeader);
request.Method = “GET”;
request.ContentType = “application/x-www-form-urlencoded”;
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var objText = reader.ReadToEnd();

// myDiv.InnerHtml = objText;/**/
string html = “”;
try
{
JArray jsonDat = JArray.Parse(objText);
if (jsonDat.Count > 0)
{
for (int x = 0; x < jsonDat.Count; x++)
{
// html += jsonDat[x].ToString() + “<br/>”;

string tDate = jsonDat[x][“created_at”].ToString();


string tweettext;
string[] res = tDate.Split(new string[] { ” ” }, StringSplitOptions.None);
string month = res[1];
string day = res[2];
string year = res[5];
string tweetDate = “<h3>” + month + ” ” + day + “, ” + year + “</h3>”;
if (jsonDat[x][“text”] != null)
{
string text = jsonDat[x][“text”].ToString();
string[] tweetinfo = text.Split(new string[] { “http” }, StringSplitOptions.None);
if (tweetinfo.Length == 1)
{
tweettext = tweetinfo[0].ToString();
if (tweettext.Length > 60)
{
string textnew = tweettext.Substring(0, 60);
int index = textnew.LastIndexOf(‘ ‘);
tweettext = textnew.Substring(0, index) + “….”;
}
}
else
{
tweettext = tweetinfo[0].ToString();
if (tweettext.Length > 60)
{
string textnew = tweettext.Substring(0, 60);
int index = textnew.LastIndexOf(‘ ‘);
string tweetlink = “http” + tweetinfo[1].ToString();
tweettext = textnew.Substring(0,index)+”….” + “<b> Related Link-</b>” + “<a href=” + tweetlink + “>” + tweetlink + “</a>”;
}
}
if (jsonDat[x][“user”][“name”] != null)
{
string name = “<h2>” + jsonDat[x][“user”][“name”].ToString() + “</h2>”;
if (jsonDat[x][“user”][“screen_name”] != null)
{
string screen_namenew = “<h5>” + “@” + jsonDat[x][“user”][“screen_name”].ToString() + “</h5>”;
string tweet = name + screen_namenew + tweetDate + “<br/>” + tweettext;
html += tweet + “<br/>”;
}
}
}
}
}
}
catch (Exception twit_error)
{
myDiv.InnerHtml = html + twit_error.ToString();
}
myDiv.InnerHtml=html;

Thus html div will display the twitter information on the webpart. You can place the webpart in you screen. Thanks Abhishek Bhardwaj for helping me to post this article. He did this twitter webpart on his own. Hope it will be helpful for you all friends.


You May Also like the Following SharePoint Online Tutorials:

About jayabharathi

The turning point in the process of growing up is when you discover the core of strength within you that survives all hurt.

View all posts by jayabharathi →

Leave a Reply