/// <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);
    }
    /// <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);
    }
    /// <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);
    }
    /// <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>
    /// 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}"
        });
    }
    /// <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);
    }
    /// <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);
    }