1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to post to Twitter when creating an Event

How to post to Twitter when creating an Event

1 posts, 0 answered
  1. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    25 Mar 2010
    Link to this post
    I just wanted to share how I was able to get an Event posted to Twitter when creating an Event. This way, there is no need to do double entries. Here is a sample of the code:

    public static bool UpdateTwitterStatus(string content, string url, string hash, bool makeShortUrl)
    {
        //VALIDATE INPUT
        if (String.IsNullOrEmpty(content))
            return false;
     
        HttpContext context = HttpContext.Current;
        //CANNOT USE ProfileCommon SINCE WE ARE IN A SEPARATE LIBRARY FROM THE WEB APPLICATION
        string login = (string)context.Profile.GetPropertyValue("Twitter.Username");
        string password = (string)context.Profile.GetPropertyValue("Twitter.Password");
        bool success = false;
     
        if (!String.IsNullOrEmpty(login) && !String.IsNullOrEmpty(password))
        {
            //SHORTEN URL IF APPLICABLE
            if (makeShortUrl)
            {
                url = ShortenUrl(url);
            }
     
            //FORMAT INPUTS
            url = (!String.IsNullOrEmpty(url)) ? " " + url : String.Empty;
            hash = (!String.IsNullOrEmpty(hash)) ? " " + hash : String.Empty;
     
            //BUILD STRING ACCORDING TO TWITTER CHARACTER LIMIT
            string trailer = url + hash;
            int charsLeft = 140 - trailer.Length;
            if (content.Length > charsLeft)
            {
                content = content.Substring(0, charsLeft) + trailer;
            }
            {
                content += trailer;
            }
     
            //SEND POST TO TWITTER
            ServicePointManager.Expect100Continue = false;
            Twitter twit = new Twitter();
            try
            {
                twit.Update(login, password, content, Twitter.OutputFormatType.JSON);
                success = true;
            }
            catch (Exception ex)
            {
                //DO LOGGING HERE
            }
        }
        return success;
    }

    The rest of the code with screenshots and explanation can be found here:

    I would love to hear feedback or suggestions.
1 posts, 0 answered