Friday, November 25, 2011

YELP API in C#

Hi Every one,

Yelp is a power full API to get restaurants details and show it to your site and when it comes to c# most of users are pointed to API version @ YELP API and face a night mare of no support so i have used certain features of this API and added simple functionality to parse by JSON.net.

So First down load YELP API in c# and then download bineris form JSON.net include both in user visual studio solution and reference to your website.

Now open the YELP API code and add the following functions in class YelpSearchService.

public static string SearchLocationWithString(String location, String category, String term)
        {
            return SearchLocationWithString(location, category, term, MaxResultCount);
        }

        public static string SearchLocationWithString(String location, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("location", location);
            request.Parameters.Add("category", category);
            request.Parameters.Add("term", term);
            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequestWithString();
        }


After this add the following functions in class YelpRequest.


public string ExecuteRequestWithString()
        {
            this.CreateRequest();

            Monitor.Enter(YelpServices.GlobalLock);

            try
            {
                TimeSpan timeSinceLastRequest = DateTime.Now.Subtract(YelpServices.LastRequest);

                if (timeSinceLastRequest.TotalSeconds < 2.0)
                {
                    Thread.Sleep(2000 - (Int32)timeSinceLastRequest.TotalMilliseconds);
                }

                return this.getResponse((HttpWebResponse)this.m_HttpRequest.GetResponse());
            }
            finally
            {
                YelpServices.LastRequest = DateTime.Now;

                Monitor.Exit(YelpServices.GlobalLock);
            }
        }


        private string getResponse(HttpWebResponse WebResp)
        {

            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            return _Answer.ReadToEnd();

        
        }

Now its time to go to page where you want to show restaurants Data just call the newly created functions and pass then over to JSON.NET



YelpServices.APIKey = "YOUR API KEY";
            string json = YelpSearchService.SearchLocationWithString("RESTURANT ADDRESS", "",
             "CATEGORY/NAME OF RESTURANT");
            bool hasReviews = false;
            List<Business> bList = new List<Business>();

            JObject o = JObject.Parse(json);

            IList<object> reviews = o.SelectToken("businesses").Select(s => (object)s).ToList<object>();
            foreach (Object j in reviews)
            {
                try
                {
                    Business deserializedProduct = JsonConvert.DeserializeObject<Business>(j.ToString());
                    //Review r = (Review)j;
                    bList.Add(deserializedProduct);
                }
                catch (Exception)
                {

                }

            }


And you will get all the business in the YELP API business class if you find Nulls in values try changing the property names in Business calls to the ones you find in JSON response.

Hope it helps.

Happy Coding.. :)

Friday, November 11, 2011

Migrating Existing .NET site to Umbraco

Hi Every one,

Its been long time since i have written any post item but today i am going to share a tip for users who want to Move an existing system to umbraco with minimal effort.

Benefits of this Practice:
1: You will have an admin section to manage out every content of the system including UI
2: If you are using any custom CMS it will work the same way as always.

Prerequisites:
1: Install Umbraco. [share me a comment if you need any help]
2: You Existing site with code.

Steps to follow:
1: Take a backup of your existing site.
2: Copy Master Pages to umbraco masterpages folder.
3: Copy Script files to scripts folder.
4: Copy css files to css folder.
5: Now convert all the pages in the site to user controls, the process is simple.
i: Rename the file extension from .aspx to .ascx
ii: If its a web application then go to designer and change the class name as well.
6: copy all the newly created user controls to usercontrol folder in umbraco directory.
7: run the site and go to yourdomain/umbraco/login.aspx and login to the admin site.
8: Go to the developer tab and add every control in macro section don't forget to Tick [Use in editor] property. [and you are half way there :)]
9: You will find your master pages in settings section under master pages; if you don't you can add them with same name over here.
10: In the same setting section Create a new document type and set its template to any one of the master pages.
11: Create a separate document type for each masterpage
12: In the Dcoument Type goto generic properties Tab and create a property for each content section of master page and set the type of property to rich text editor.
13: Now go back to template where you have created master pages and replace all the content Areas with Umbraco page field name, you will find the Properties you just created in that list. Simply add them.
14: Go to the content section add new content pages by selecting respective master page template for each.
15: you will find a Rick text editor in each content area.
16: Add a Macro to that content area. [And you are done]


This Is the most simple way to migrate any site in umbraco; if you need any help just let me know ill respond to your comments immediately.

Happy Coding. :)