Twitter


List of examples:

Twitter apps

Creating a Twitter app



// Specifies the app's credentials
// Replace the values with the credentials for your own configured Twitter app
// See the documentation for creating a new Twitter app: https://devnet.kentico.com/CMSPages/DocLinkMapper.ashx?version=latest&link=sm_connecting
string consumerKey = "yourConsumerKey";
string consumerSecret = "yourConsumerSecret";

// Creates a new Twitter app object
TwitterApplicationInfo app = new TwitterApplicationInfo();

// Sets the properties of the app
app.TwitterApplicationDisplayName = "New Twitter app";
app.TwitterApplicationName = "NewTwitterApp";

app.TwitterApplicationConsumerKey = consumerKey;
app.TwitterApplicationConsumerSecret = consumerSecret;

app.TwitterApplicationSiteID = SiteContext.CurrentSiteID;

// Saves the Twitter app to the database
TwitterApplicationInfoProvider.SetTwitterApplicationInfo(app);

> Back to list of examples

Updating a Twitter app



// Gets the app
TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("NewTwitterApp", SiteContext.CurrentSiteName);

if (app != null)
{
    // Updates the app
    app.TwitterApplicationDisplayName = app.TwitterApplicationDisplayName.ToLowerCSafe();

    // Saves the modified app to the database
    TwitterApplicationInfoProvider.SetTwitterApplicationInfo(app);
}

> Back to list of examples

Deleting a Twitter app



// Gets the app
TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("NewTwitterApp", SiteContext.CurrentSiteName);

if (app != null)
{
    // Deletes the app
    TwitterApplicationInfoProvider.DeleteTwitterApplicationInfo(app);
}

> Back to list of examples

Twitter channels

Creating a Twitter channel



// Specifies the properties for accessing a Twitter channel
// Replace the values with the access credentials for your own configured Twitter channel
string accessToken = "yourAccessToken";
string accessTokenSecret = "yourAccessTokenSecret";

// Gets the app to which the channel is tied
TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("NewTwitterApp", SiteContext.CurrentSiteName);

if (app == null)
{
    throw new Exception("[ApiExamples.CreateTwitterChannel]: Application 'NewTwitterApp' was not found.");
}

// Creates a new channel object
TwitterAccountInfo channel = new TwitterAccountInfo();

// Sets the properties for the channel
channel.TwitterAccountDisplayName = "New Twitter channel";
channel.TwitterAccountName = "NewTwitterChannel";

channel.TwitterAccountAccessToken = accessToken;
channel.TwitterAccountAccessTokenSecret = accessTokenSecret;
channel.TwitterAccountSiteID = SiteContext.CurrentSiteID;
channel.TwitterAccountTwitterApplicationID = app.TwitterApplicationID;

// Saves the channel to the database
TwitterAccountInfoProvider.SetTwitterAccountInfo(channel);

> Back to list of examples

Updating a Twitter channel



// Gets the channel
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel != null)
{
    // Updates the properties of the channel
    channel.TwitterAccountDisplayName = channel.TwitterAccountDisplayName.ToLowerCSafe();

    // Saves the changes to the databse
    TwitterAccountInfoProvider.SetTwitterAccountInfo(channel);
}

> Back to list of examples

Deleting a Twitter channel



// Gets the channel
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel != null)
{
    // Deletes the channel
    TwitterAccountInfoProvider.DeleteTwitterAccountInfo(channel);
}

> Back to list of examples

Twitter posts

Creating a Tweet



// Gets the channel to which the tweet is tied
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel == null)
{
    throw new Exception("[ApiExamples.CreateTwitterPost]: Account 'NewTwitterChannel' was not found.");
}

// Creates a new Twitter post object
TwitterPostInfo tweet = new TwitterPostInfo();

// Sets the properties for the tweet
tweet.TwitterPostTwitterAccountID = channel.TwitterAccountID;
tweet.TwitterPostSiteID = SiteContext.CurrentSiteID;
tweet.TwitterPostText = "Sample tweet text.";

// Specifies the publish time for the tweet
tweet.TwitterPostScheduledPublishDateTime = DateTime.Now + TimeSpan.FromMinutes(5);

// Saves the tweet to the database
TwitterPostInfoProvider.SetTwitterPostInfo(tweet);

> Back to list of examples

Updating a Tweet



// Gets the channel to which the tweet is tied
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel == null)
{
    throw new Exception("[ApiExamples.GetAndUpdateTwitterPost]: Account 'NewTwitterChannel' was not found.");
}

// Gets the first tweet tied to the channel
TwitterPostInfo tweet = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID).TopN(1).FirstOrDefault();

if (tweet == null)
{
    throw new Exception("[ApiExamples.GetAndUpdateTwitterPost]: There are no posts tied to 'NewTwitterChannel'.");
}

// Updates the text of the tweet
tweet.TwitterPostText = tweet.TwitterPostText + " Edited.";

// Saves the changed tweet to the database
TwitterPostInfoProvider.SetTwitterPostInfo(tweet);

> Back to list of examples

Deleting Tweets



// Gets the channel to which the tweets are tied
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel == null)
{
    throw new Exception("[ApiExamples.DeleteTwitterPosts]: Account 'NewTwitterChannel' has not been found.");
}

// Gets all tweets tied to the channel
var tweets = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID);

// Deletes the tweets from Xperience and from Twitter
foreach (TwitterPostInfo tweet in tweets)
{
    TwitterPostInfoProvider.DeleteTwitterPostInfo(tweet);
}

> Back to list of examples

Publishing a Tweet



// Gets the channel to which the tweet is tied
TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("NewTwitterChannel", SiteContext.CurrentSiteID);

if (channel == null)
{
    throw new Exception("[ApiExamples.PublishTweetToTwitter]: Account 'NewTwitterChannel' was not found.");
}

// Gets the first tweet tied to the channel
TwitterPostInfo tweet = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID).TopN(1).FirstOrDefault();

if (tweet == null)
{
    throw new Exception("[ApiExamples.PublishTweetToTwitter]: There are no posts tied to 'NewTwitterChannel'.");
}

// Publishes the tweet. The tweet is scheduled for publishing if its TwitterPostScheduledPublishDateTime value is set in the future.
TwitterPostInfoProvider.PublishTwitterPost(tweet.TwitterPostID);

> Back to list of examples