Wednesday, August 19, 2015

How to integrate Bing News with Sharepoint 2013

Hello, you may be here to know how to display your Bing news results onto your SharePoint page.

Firstly, you have to register an application at Bing Search API site and then you get an AccountKey.

Secondly, you have to use AccountKey in the API to get access to the Bing search API and get news results of the query.

Here is a sample BnHelper.cs code:

Next, you have to create a controller as shown:

 //Bing controller to get news from Bing search API
using Integration.Bing;
using log4net;

namespace SiteAPI.Controllers
{
    public class BingController : ApiController
    {
        private readonly ILog _log = LogManager.GetLogger("SiteAPI");

        private const string RootUrl = "https://api.datamarket.azure.com/Bing/Search";
        private const string Market = "en-us";
        private const string NewsCategory = "rt_ScienceAndTechnology";

        public List GetBingNews(string Query)
        {
            try
            {
                if (_log.IsDebugEnabled) { _log.Debug("Calling GetBingNews"); }
                return Integration.Bing.BnHelper.GetBingNews(Query, RootUrl, Market, NewsCategory);
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);

                //Throw custom exception to the caller
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("An error occurred, please try again or contact the administrator."),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }
    }
}

Then you have to publish the API to your Azure instance, here is test site url to get Bing news: https://{enteryoursite}.azurewebsites.net/api/bing/GetBingNews?Query={enterQuery}

Finally you can use this url in SharePoint using JQuery:
Place below code to a Sharepoint master gallery file, for example: /script/custom.js
//Bing News
function _initBingNews(query) {
    var bnfeed = $('#bing-news-feed');
    // get Bing News from api
    function __getBingNews(callback) {
        var apiUri = 'https://{enteryoursite}.azurewebsites.net/api/bing/GetBingNews?Query=';

        $.ajax({
            url: apiUri + query,
            type: 'GET',
            success: function (result) {
                callback(result);
            },
            error: function () {
                //bnFeed.closest('.ms-webpartzone-cell').hide();
            }
        });
    }

    // format the data returned from the bing search api
    function __processBingNews(posts) {
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var bingNewsHtml = '';
        var title;
        var description;
        var link;
        var source;
        var date;
        var post;
        for (var post in posts) {
            title = posts[post].ContentTitle;
            description = posts[post].ContentDescription;
            link = posts[post].ContentUrl;
            date = posts[post].ContentDate.match(/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/);
     date = formatDate( date[2], date[3], date[1]);
           
            if (title.length > 0) {                
                if (link != null) {
                    post = '
  • '; } bingNewsHtml += post; } } bingNewsHtml = '
      '+bingNewsHtml+'
    '; return bingNewsHtml; } // get Bing news posts and add them to the Bing News UI __getBingNews(function (bingNewsPosts) { bnFeed.html(__processBingNews(bingNewsPosts)); }); }

    Then create a SharePoint page and in edit mode, add a Script editor web part and add below snippet:
    //Bing News Feed
    

    Once you save the page, you will see the Bing News loaded onto the web part.
    Happy Coding...Hope you like this post!!! Please share or comment if you like it.

    No comments:

    Post a Comment