/// <summary> /// Displays warning if there is no default account set on the site. /// </summary> private void DisplayWarningIfNoDefaultAccount() { if (TwitterAccountInfoProvider.GetDefaultTwitterAccount(SiteContext.CurrentSiteID) == null) { Control.ShowWarning(ResHelper.GetString("sm.twitter.nodefaultchannel")); } }
/// <summary> /// Creates a new tweet. Requires Create Twitter app and Create Twitter channel to be run first. /// </summary> private bool CreateTwitterPost() { // Get the channel the tweet is tied to TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { throw new Exception("[ApiExamples.CreateTwitterPost]: Account 'MyNewTwitterChannel' has not been found."); } // Create new post object TwitterPostInfo tweet = new TwitterPostInfo(); // Set the roperties tweet.TwitterPostTwitterAccountID = channel.TwitterAccountID; tweet.TwitterPostSiteID = SiteContext.CurrentSiteID; tweet.TwitterPostText = "Sample tweet text."; // Should the tweet be scheduled instead of directly posted? tweet.TwitterPostScheduledPublishDateTime = DateTime.Now + TimeSpan.FromMinutes(5); // Save the tweet into DB TwitterPostInfoProvider.SetTwitterPostInfo(tweet); return(true); }
/// <summary> /// Gets and updates a tweet /// </summary> private bool GetAndUpdateTwitterPost() { // Get the channel the tweet is tied to TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { throw new Exception("[ApiExamples.GetAndUpdateTwitterPost]: Account 'MyNewTwitterChannel' has not been found."); } // Get a post tied to the channel TwitterPostInfo tweet = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID).FirstOrDefault(); if (tweet == null) { throw new Exception("[ApiExamples.GetAndUpdateTwitterPost]: No post has been created via these api-examples. (There is no post tied to 'MyNewTwitterChannel'.)"); } // Update the properties tweet.TwitterPostText = tweet.TwitterPostText + " Edited."; // Save the changes into DB TwitterPostInfoProvider.SetTwitterPostInfo(tweet); return(true); }
/// <summary> /// Creates a Twitter channel. /// </summary> private bool CreateTwitterChannel() { if (string.IsNullOrEmpty(txtAccessToken.Text) || string.IsNullOrEmpty(txtAccessTokenSecret.Text)) { throw new Exception("[ApiExamples.CreateTwitterChannel]: Empty values for 'Channel access token' and 'Channel access token secret' are not allowed. Please provide your channel's credentials."); } // Get the app the channel is tied to. TwitterApplicationInfo app = TwitterApplicationInfoProvider.GetTwitterApplicationInfo("MyNewTwitterApp", SiteContext.CurrentSiteName); if (app == null) { throw new Exception("[ApiExamples.CreateTwitterChannel]: Application 'MyNewTwitterApp' was not found."); } // Create new channel object TwitterAccountInfo channel = new TwitterAccountInfo(); // Set the properties channel.TwitterAccountDisplayName = "My new Twitter channel"; channel.TwitterAccountName = "MyNewTwitterChannel"; channel.TwitterAccountAccessToken = txtAccessToken.Text; channel.TwitterAccountAccessTokenSecret = txtAccessTokenSecret.Text; channel.TwitterAccountSiteID = SiteContext.CurrentSiteID; channel.TwitterAccountTwitterApplicationID = app.TwitterApplicationID; // Save the channel into DB TwitterAccountInfoProvider.SetTwitterAccountInfo(channel); return(true); }
/// <summary> /// Initializes inner controls. Post data are loaded when editing post. /// </summary> private void InitializeControls() { chkPostAfterDocumentPublish.Checked = false; string content = String.Empty; if (mBackCompatibilityValue.HasValue) { // Pre fill form with old data chkPostAfterDocumentPublish.Checked = mBackCompatibilityValue.Value.AutoPostAfterPublishing; content = mBackCompatibilityValue.Value.Template; } else if (!String.IsNullOrWhiteSpace(mDefaultValue)) { // Default content given as control's string Value content = mDefaultValue; } else if ((FieldInfo != null) && !String.IsNullOrWhiteSpace(FieldInfo.DefaultValue)) { // Default content from field info content = FieldInfo.DefaultValue; } tweetContent.Text = content; if (!chkPostAfterDocumentPublish.Checked) { publishDateTime.Value = DateTime.Now; } channelSelector.ObjectSiteName = SiteIdentifier; TwitterAccountInfo defaultAccountInfo = TwitterAccountInfoProvider.GetDefaultTwitterAccount(SiteIdentifier); if (defaultAccountInfo != null) { channelSelector.Value = defaultAccountInfo.TwitterAccountID; } ClearPostState(); urlShortenerSelector.SiteID = SiteIdentifier; urlShortenerSelector.Value = URLShortenerHelper.GetDefaultURLShortenerForSocialNetwork(SocialNetworkTypeEnum.Twitter, SiteIdentifier); chkPostAfterDocumentPublish.Visible = (Document != null); campaingSelector.Value = null; campaingSelector.ObjectSiteName = SiteIdentifier; if ((FieldInfo != null) && !FieldInfo.AllowEmpty) { // Post must be created because field doesn't allow an empty value chkPostToTwitter.Enabled = false; DisplayForm = true; } else { // Form is displayed if it is pre filled with old data DisplayForm = mBackCompatibilityValue.HasValue; } }
/// <summary> /// Deletes a channel from DB /// </summary> private bool DeleteTwitterChannel() { // Get the channel from DB TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { return(false); } // Delete channel from DB TwitterAccountInfoProvider.DeleteTwitterAccountInfo(channel); return(true); }
/// <summary> /// Gets a Twitter channel from the database and modifies it. /// </summary> private bool GetAndUpdateTwitterChannel() { // Get the channel from DB TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { return(false); } // Update the properties channel.TwitterAccountDisplayName = channel.TwitterAccountDisplayName.ToLowerCSafe(); // Save the changes into DB TwitterAccountInfoProvider.SetTwitterAccountInfo(channel); return(true); }
/// <summary> /// OnBeforeDataLoad event - Ensures default account is pre-selected. /// </summary> private void Control_OnBeforeDataLoad(object sender, EventArgs e) { var twitterPost = Control.EditedObject as TwitterPostInfo; if (twitterPost == null) { return; } // if the post is newly created fill in default page if (twitterPost.TwitterPostID == 0) { TwitterAccountInfo defaultTwitterAccountInfo = TwitterAccountInfoProvider.GetDefaultTwitterAccount(Control.ObjectSiteID); if (defaultTwitterAccountInfo != null) { twitterPost.TwitterPostTwitterAccountID = defaultTwitterAccountInfo.TwitterAccountID; } } }
/// <summary> /// Creates an object that represents information required by this page to display Twitter insights, and returns it. /// </summary> /// <param name="accountId">The Twitter account identifier.</param> /// <returns>An object that represents information required by this page to display Twitter insights.</returns> private PageContext GetPageContextForTwitter(int accountId) { TwitterAccountInfo account = TwitterAccountInfoProvider.GetTwitterAccountInfo(accountId); if (account == null || account.TwitterAccountSiteID != SiteContext.CurrentSiteID) { throw new Exception("[CMSModules_SocialMarketing_Pages_InsightsMenu.GetPageContextForTwitter]: Twitter account with the specified identifier does not exist."); } if (!account.CheckPermissions(PermissionsEnum.Read, SiteContext.CurrentSiteName, MembershipContext.AuthenticatedUser)) { RedirectToAccessDenied(ModuleName.SOCIALMARKETING, PermissionsEnum.Read.ToString()); } return(new PageContext { ExternalId = account.TwitterAccountUserID, ReportNamePrefix = "Twitter.channel_", ReportNameFormat = "Twitter.{0}.{1}.{2}report", DisplayNameResourceNameFormat = "sm.ins.twitter.{0}" }); }
/// <summary> /// Deletes all tweets tied to channel 'MyNewTwitterChannel'. /// </summary> private bool DeleteTwitterPosts() { // Get the channel the tweet is tied to TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { throw new Exception("[ApiExamples.DeleteTwitterPosts]: Account 'MyNewTwitterChannel' has not been found."); } // Get all posts tied to the account ObjectQuery <TwitterPostInfo> tweets = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID); // Delete these posts from CMS and from Twitter foreach (TwitterPostInfo tweet in tweets) { TwitterPostInfoProvider.DeleteTwitterPostInfo(tweet); } return(tweets.Count != 0); }
/// <summary> /// Publishes a tweet. /// </summary> private bool PublishTweetToTwitter() { // Get the channel the tweet is tied to TwitterAccountInfo channel = TwitterAccountInfoProvider.GetTwitterAccountInfo("MyNewTwitterChannel", SiteContext.CurrentSiteID); if (channel == null) { throw new Exception("[ApiExamples.PublishTweetToTwitter]: Account 'MyNewTwitterChannel' has not been found."); } // Get a post tied to the channel TwitterPostInfo tweet = TwitterPostInfoProvider.GetTwitterPostInfoByAccountId(channel.TwitterAccountID).FirstOrDefault(); if (tweet == null) { throw new Exception("[ApiExamples.PublishTweetToTwitter]: No post has been created via these api-examples. (There is no post tied to 'MyNewTwitterChannel'.)"); } // Publish the post. The Tweet is scheduled for publishing if its FacebookPostScheduledPublishDateTime is set in the future. TwitterPostInfoProvider.PublishTwitterPost(tweet.TwitterPostID); return(true); }
/// <summary> /// Attempts to upgrade settings from the old way to he new-one. /// </summary> /// <param name="site">site we are importing for</param> /// <returns>true on success, false on failure</returns> private static void ImportTwitterSettings(SiteInfo site) { TwitterApplicationInfo twittAppInfo = new TwitterApplicationInfo() { TwitterApplicationDisplayName = site.DisplayName + "Twitter App", TwitterApplicationName = site.SiteName + "TwitterApp", TwitterApplicationConsumerKey = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterConsumerKey"), TwitterApplicationConsumerSecret = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterConsumerSecret"), TwitterApplicationSiteID = site.SiteID }; if (String.IsNullOrWhiteSpace(twittAppInfo.TwitterApplicationConsumerKey) || String.IsNullOrWhiteSpace(twittAppInfo.TwitterApplicationConsumerSecret)) { return; } try { TwitterApplicationInfoProvider.SetTwitterApplicationInfo(twittAppInfo); twittAppInfo = TwitterApplicationInfoProvider.GetTwitterApplicationInfo(site.SiteName + "TwitterApp", site.SiteName); } catch (Exception ex) { EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Twitter application storage to DB for site " + site.SiteName, siteId: site.SiteID); return; } TwitterAccountInfo twittAccountInfo = new TwitterAccountInfo() { TwitterAccountName = site.SiteName + "TwitterChannel", TwitterAccountDisplayName = site.DisplayName + " Twitter Channel", TwitterAccountTwitterApplicationID = twittAppInfo.TwitterApplicationID, TwitterAccountSiteID = site.SiteID, TwitterAccountAccessToken = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterAccessToken"), TwitterAccountAccessTokenSecret = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterAccessTokenSecret"), TwitterAccountIsDefault = true, }; twittAccountInfo.TwitterAccountUserID = GetTwitterUserId(twittAppInfo, twittAccountInfo); if (String.IsNullOrWhiteSpace(twittAccountInfo.TwitterAccountAccessToken) || String.IsNullOrWhiteSpace(twittAccountInfo.TwitterAccountAccessTokenSecret)) { return; } try { TwitterAccountInfoProvider.SetTwitterAccountInfo(twittAccountInfo); } catch (Exception ex) { EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Twitter channel storage to DB for site " + site.SiteName, siteId: site.SiteID); return; } // URL shortener if (!SettingsKeyInfoProvider.IsValueInherited(site.SiteName + ".CMSTwitterURLShortenerType")) { string shortener = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSTwitterURLShortenerType"); SettingsKeyInfoProvider.SetValue(site.SiteName + ".CMSSocialMarketingURLShorteningTwitter", shortener); } return; }