Exemplo n.º 1
0
    /// <summary>
    /// Gets and updates Facebook post. Called when the "Get and update post" button is pressed.
    /// Expects the CreateFacebookPost method to be run first.
    /// </summary>
    private bool GetAndUpdateFacebookPost()
    {
        // Get an page to which the post is tied
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        if (page == null)
        {
            throw new Exception("[FacebookApiExamples.GetAndUpdateFacebookPost]: Page 'My new page' wasn't found.");
        }

        // Get the Facebook post from DB
        FacebookPostInfo post = FacebookPostInfoProvider.GetFacebookPostInfosByAccountId(page.FacebookAccountID).FirstOrDefault();

        if (post != null)
        {
            // Update the properties
            post.FacebookPostText = post.FacebookPostText + " Edited.";

            // Save the changes into DB
            FacebookPostInfoProvider.SetFacebookPostInfo(post);

            return(true);
        }

        return(false);
    }
Exemplo n.º 2
0
    /// <summary>
    /// Creates Facebook post. Called when the "Create post" button is pressed.
    /// </summary>
    private bool CreateFacebookPost()
    {
        // Get an page to which the post is tied
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        if (page == null)
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPost]: Page 'My new page' wasn't found.");
        }

        // Create new Facebook post object
        FacebookPostInfo post = new FacebookPostInfo();

        // Set the properties
        post.FacebookPostFacebookAccountID = page.FacebookAccountID;
        post.FacebookPostSiteID            = SiteContext.CurrentSiteID;
        post.FacebookPostText = "Sample post text.";

        // Should the post be scheduled instead of directly posted?
        post.FacebookPostScheduledPublishDateTime = DateTime.Now + TimeSpan.FromMinutes(5);

        // Is the post tied to a document?
        post.FacebookPostDocumentGUID = null;

        // Save the Facebook post into DB
        FacebookPostInfoProvider.SetFacebookPostInfo(post);

        return(true);
    }
Exemplo n.º 3
0
    /// <summary>
    /// Initializes inner controls to its default state.
    /// </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;
        }
        txtPost.Text = content;

        if (!chkPostAfterDocumentPublish.Checked)
        {
            publishDateTime.Value = DateTime.Now;
        }

        pageSelector.ObjectSiteName = SiteIdentifier;
        FacebookAccountInfo defaultAccountInfo = FacebookAccountInfoProvider.GetDefaultFacebookAccount(SiteIdentifier);

        if (defaultAccountInfo != null)
        {
            pageSelector.Value = defaultAccountInfo.FacebookAccountID;
        }

        ClearPostState();
        urlShortenerSelector.SiteID         = SiteIdentifier;
        urlShortenerSelector.Value          = URLShortenerHelper.GetDefaultURLShortenerForSocialNetwork(SocialNetworkTypeEnum.Facebook, 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
            chkPostToFacebook.Enabled = false;
            DisplayForm = true;
        }
        else
        {
            // Form is displayed if it is pre filled with old data
            DisplayForm = mBackCompatibilityValue.HasValue;
        }
    }
Exemplo n.º 4
0
    /// <summary>
    /// Deletes Facebook page. Called when the "Delete page" button is pressed.
    /// Expects the CreateFacebookPage method to be run first.
    /// </summary>
    private bool DeleteFacebookPage()
    {
        // Get the Facebook page from DB
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        // Delete the Facebook page from DB
        FacebookAccountInfoProvider.DeleteFacebookAccountInfo(page);

        return(page != null);
    }
Exemplo n.º 5
0
    /// <summary>
    /// Gets and updates Facebook page. Called when the "Get and update page" button is pressed.
    /// Expects the CreateFacebookPage method to be run first.
    /// </summary>
    private bool GetAndUpdateFacebookPage()
    {
        // Get the Facebook page from DB
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        if (page != null)
        {
            // Update the properties
            page.FacebookAccountDisplayName = page.FacebookAccountDisplayName.ToLowerCSafe();

            // Save the changes into DB
            FacebookAccountInfoProvider.SetFacebookAccountInfo(page);

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// OnBeforeDataLoad event - Ensures default account is pre-selected.
    /// </summary>
    private void Control_OnBeforeDataLoad(object sender, EventArgs e)
    {
        var facebookPost = Control.EditedObject as FacebookPostInfo;

        if (facebookPost == null)
        {
            return;
        }

        // if the post is newly created fill in default page
        if (facebookPost.FacebookPostID == 0)
        {
            FacebookAccountInfo defaultFacebookAccountInfo = FacebookAccountInfoProvider.GetDefaultFacebookAccount(Control.ObjectSiteID);
            if (defaultFacebookAccountInfo != null)
            {
                facebookPost.FacebookPostFacebookAccountID = defaultFacebookAccountInfo.FacebookAccountID;
            }
        }
    }
Exemplo n.º 7
0
    /// <summary>
    /// Creates Facebook page. Called when the "Create page" button is pressed.
    /// </summary>
    private bool CreateFacebookPage()
    {
        // Check if all the fields have been properly set
        if (String.IsNullOrWhiteSpace(txtPageUrl.Text) || String.IsNullOrWhiteSpace(txtFacebookPageAccessToken.Text))
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Empty values for 'Facebook page URL' and 'Access token' are not allowed. Please provide your page's credentials.");
        }

        // Get the application ID you want to associate the page with first.
        FacebookApplicationInfo app = FacebookApplicationInfoProvider.GetFacebookApplicationInfo("MyNewApplication", SiteContext.CurrentSiteID);

        if (app == null)
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Application 'MyNewApplication' was not found.");
        }
        int appId = app.FacebookApplicationID;

        // Create new Facebook page object
        FacebookAccountInfo page = new FacebookAccountInfo();

        // Set the properties
        page.FacebookAccountDisplayName           = "My new page";
        page.FacebookAccountName                  = "MyNewPage";
        page.FacebookAccountSiteID                = SiteContext.CurrentSiteID;
        page.FacebookAccountFacebookApplicationID = appId;

        // Use FacebookHelper to get the ID that facebook has given the page. It's used to identify the page later when posting on its wall. Throw exception if failed.
        string myFacebookPageId;

        if (!FacebookHelper.TryGetFacebookPageId(txtPageUrl.Text, out myFacebookPageId))
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Failed to get PageID from Facebook. The 'Page Url', you have entered is not a valid Facebook Page Url.");
        }
        // see https://developers.Facebook.com/docs/Facebook-login/access-tokens/ or http://Facebooksdk.net/docs/web/ or use https://developers.Facebook.com/tools/explorer/ to get access token now
        page.FacebookPageIdentity    = new FacebookPageIdentityData(txtPageUrl.Text, myFacebookPageId);
        page.FacebookPageAccessToken = new FacebookPageAccessTokenData(txtFacebookPageAccessToken.Text, null);

        // Save the Facebook page into DB
        FacebookAccountInfoProvider.SetFacebookAccountInfo(page);

        return(true);
    }
    /// <summary>
    /// Creates an object that represents information required by this page to display Facebook insights, and returns it.
    /// </summary>
    /// <param name="accountId">The Facebook account identifier.</param>
    /// <returns>An object that represents information required by this page to display Facebook insights.</returns>
    private PageContext GetPageContextForFacebook(int accountId)
    {
        FacebookAccountInfo account = FacebookAccountInfoProvider.GetFacebookAccountInfo(accountId);

        if (account == null || account.FacebookAccountSiteID != SiteContext.CurrentSiteID)
        {
            throw new Exception("[CMSModules_SocialMarketing_Pages_InsightsMenu.GetPageContextForFacebook]: Facebook 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.FacebookAccountPageID,
            ReportNamePrefix = "Facebook.page_",
            ReportNameFormat = "Facebook.{0}.{1}.{2}report",
            DisplayNameResourceNameFormat = "sm.ins.facebook.{0}"
        });
    }
Exemplo n.º 9
0
    /// <summary>
    /// Deletes Facebook post. Called when the "Delete post" button is pressed.
    /// Expects the CreateFacebookPost method to be run first.
    /// </summary>
    private bool DeleteFacebookPosts()
    {
        // Get an page to which the post is tied
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        if (page == null)
        {
            throw new Exception("[FacebookApiExamples.DeleteFacebookPosts]: Page 'My new page' wasn't found.");
        }

        // Get the Facebook post from DB
        ObjectQuery <FacebookPostInfo> post = FacebookPostInfoProvider.GetFacebookPostInfosByAccountId(page.FacebookAccountID);

        // Delete the Facebook post from CMS and from Facebook
        foreach (FacebookPostInfo deletePost in post)
        {
            FacebookPostInfoProvider.DeleteFacebookPostInfo(deletePost);
        }

        return(post.Count != 0);
    }
Exemplo n.º 10
0
    /// <summary>
    /// Publishes a facebook post.
    /// </summary>
    private bool PublishPostToFacebook()
    {
        // Get an page to which the post is tied
        FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID);

        if (page == null)
        {
            throw new Exception("[FacebookApiExamples.PublishPostToFacebook]: Page 'My new page' wasn't found.");
        }

        // Get the Facebook post from DB
        FacebookPostInfo post = FacebookPostInfoProvider.GetFacebookPostInfosByAccountId(page.FacebookAccountID).FirstOrDefault();

        if (post == null)
        {
            throw new Exception("[FacebookApiExamples.PublishPostToFacebook]: No post has been created via these api Examples, or they have been deleted.");
        }

        // Publish the post. The post is scheduled to be published if its FacebookPostScheduledPublishDateTime is set in the future.
        FacebookPostInfoProvider.PublishFacebookPost(post.FacebookPostID);

        return(true);
    }
    /// <summary>
    /// Creates Facebook page. Called when the "Create page" button is pressed.
    /// </summary>
    private bool CreateFacebookPage()
    {
        // Check if all the fields have been properly set
        if (String.IsNullOrWhiteSpace(txtPageUrl.Text) || String.IsNullOrWhiteSpace(txtFacebookPageAccessToken.Text))
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Empty values for 'Facebook page URL' and 'Access token' are not allowed. Please provide your page's credentials.");
        }

        // Get the application ID you want to associate the page with first.
        FacebookApplicationInfo app = FacebookApplicationInfoProvider.GetFacebookApplicationInfo("MyNewApplication", SiteContext.CurrentSiteID);

        if (app == null)
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Application 'MyNewApplication' was not found.");
        }
        int appId = app.FacebookApplicationID;

        // Create new Facebook page object
        FacebookAccountInfo page = new FacebookAccountInfo();

        // Set the properties
        page.FacebookAccountDisplayName = "My new page";
        page.FacebookAccountName = "MyNewPage";
        page.FacebookAccountSiteID = SiteContext.CurrentSiteID;
        page.FacebookAccountFacebookApplicationID = appId;

        // Use FacebookHelper to get the ID that facebook has given the page. It's used to identify the page later when posting on its wall. Throw exception if failed.
        string myFacebookPageId;
        if (!FacebookHelper.TryGetFacebookPageId(txtPageUrl.Text, out myFacebookPageId))
        {
            throw new Exception("[FacebookApiExamples.CreateFacebookPage]: Failed to get PageID from Facebook. The 'Page Url', you have entered is not a valid Facebook Page Url.");
        }
        // see https://developers.Facebook.com/docs/Facebook-login/access-tokens/ or http://Facebooksdk.net/docs/web/ or use https://developers.Facebook.com/tools/explorer/ to get access token now
        page.FacebookPageIdentity = new FacebookPageIdentityData(txtPageUrl.Text, myFacebookPageId);
        page.FacebookPageAccessToken = new FacebookPageAccessTokenData(txtFacebookPageAccessToken.Text, null);

        // Save the Facebook page into DB
        FacebookAccountInfoProvider.SetFacebookAccountInfo(page);

        return true;
    }
Exemplo n.º 12
0
    /// <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 ImportFacebookSettings(SiteInfo site)
    {
        FacebookApplicationInfo fbAppInfo = new FacebookApplicationInfo()
        {
            FacebookApplicationDisplayName    = site.DisplayName + " Facebook App",
            FacebookApplicationName           = site.SiteName + "FacebookApp",
            FacebookApplicationSiteID         = site.SiteID,
            FacebookApplicationConsumerKey    = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookConnectApiKey"),
            FacebookApplicationConsumerSecret = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookApplicationSecret")
        };

        if (String.IsNullOrWhiteSpace(fbAppInfo.FacebookApplicationConsumerKey) || String.IsNullOrWhiteSpace(fbAppInfo.FacebookApplicationConsumerSecret))
        {
            return;
        }

        try
        {
            FacebookApplicationInfoProvider.SetFacebookApplicationInfo(fbAppInfo);
            fbAppInfo = FacebookApplicationInfoProvider.GetFacebookApplicationInfo(site.SiteName + "FacebookApp", site.SiteName);
        }
        catch (Exception ex)
        {
            // LogException
            EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Facebook Application storage to DB for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        // FB Page Part
        FacebookAccountInfo fbPageInfo = new FacebookAccountInfo()
        {
            FacebookAccountFacebookApplicationID = fbAppInfo.FacebookApplicationID,
            FacebookAccountSiteID      = site.SiteID,
            FacebookAccountDisplayName = site.DisplayName + " Facebook Page",
            FacebookAccountName        = site.SiteName + "FacebookPage",
            FacebookAccountIsDefault   = true,
        };

        FacebookPageAccessTokenData?accToken = GetToken(SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookAccessToken"));

        if (accToken.HasValue)
        {
            fbPageInfo.FacebookPageAccessToken = accToken.Value;
        }
        else
        {
            // Log error importing settings for site
            EventLogProvider.LogEvent(EventType.ERROR, "Upgrade to 8.0", "Upgrade of SocialMarketing", eventDescription: "Error during parsing of PageAccessToken for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        FacebookPageIdentityData?PIData = GetPageIdentity(site);

        if (PIData.HasValue)
        {
            fbPageInfo.FacebookPageIdentity = PIData.Value;
        }
        else
        {
            // Log error importing settings for site
            EventLogProvider.LogEvent(EventType.ERROR, "Upgrade to 8.0", "Upgrade of SocialMarketing", eventDescription: "Error during Getting of PageIdentity for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        if (String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageAccessToken.AccessToken) || String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageIdentity.PageId) || String.IsNullOrWhiteSpace(fbPageInfo.FacebookPageIdentity.PageUrl))
        {
            return;
        }

        try
        {
            FacebookAccountInfoProvider.SetFacebookAccountInfo(fbPageInfo);
        }
        catch (Exception ex)
        {
            // Log Exception
            EventLogProvider.LogException("Upgrade to 8.0", "Upgrade of SocialMarketing", ex, additionalMessage: "Error during Facebook Page storage to DB for site " + site.SiteName, siteId: site.SiteID);

            return;
        }

        // URL shortener
        if (!SettingsKeyInfoProvider.IsValueInherited(site.SiteName + ".CMSFacebookURLShortenerType"))
        {
            string shortener = SettingsKeyInfoProvider.GetStringValue(site.SiteName + ".CMSFacebookURLShortenerType");
            SettingsKeyInfoProvider.SetValue(site.SiteName + ".CMSSocialMarketingURLShorteningFacebook", shortener);
        }

        return;
    }