My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Friday, November 6, 2009

Google Search API with C# and JSON.NET

Google exposes a free search API.  Of course, they do not provide a lot of support for Microsoft developers—they do have a .NET library available, but it is for .NET 1.1, which is several versions behind. 

I found that they have a REST API available, and the basic search API does not even require a developer token.  Connecting is straightforward enough:

image

In the first line, you can see by the end of the string that I am searching for “Monster Truck.”  Then we use the WebClient class to download the result into a string.  The results look similar to this.

That’s JSON.  I really haven’t spent much time working with JSON in .NET, but there are several libraries available to help with it.  I chose to look at JSON.NET.  Its basic functionality is to take JSON and parse it into a hierarchy of dictionary objects.  Here is how I can loop over the results of the Google search query:

image

JObject.Parse takes a string and parses the JSON.  This allows navigating the hierarchy using lines like o[“responseData”][“results”].  More information on the classes available in JSON.NET is available here.

Labels: , , ,

Wednesday, November 4, 2009

Sliding Date Range Filter in the Data View Web Part

I had a client request to filter items from a Data View Web Part based on a date column.  The tricky part was that they wanted items to return only if the value in the date column was in the coming week.  So I needed some way to filter out all dates that were earlier than today or later than today + 7 days.  The date column in this example is “Bid Date.”

The scenario I created has 4 items from 2 different lists:

  • Item 1 – no bid date selected (should not show)
  • Item 2 – bid date within 7 day range (should show)
  • Item 3 – bid date in the past (should not show)
  • Item 4 – bid date in the future, beyond the 7 day range (should not show)

With no filters, my data view web part shows all 4 items:

clip_image002

From SharePoint Designer, open the page with the data view web part and click the chevron in the upper-right corner. Click “Filter:” and note that currently there are no filters applied:

image

The Filter Criteria dialog box opens.

clip_image006

We will add two clauses, one to filter out any item with a Bid Date earlier than today, and another to filter out any Bid Date with a date beyond 7 days from today. Click to add a new clause:

clip_image008

The first one is easy—we required that the Bid Date be greater than or equal to the current date. Note that this filter alone will filter out empty Bid Dates as well as Bid Dates in the past:

clip_image010

For the second filter, we must go a little deeper to achieve the dynamic date of “today plus 7 days.” Click the Advanced Button and note the existing XPath, which is our “greater than or equal to today” clause we just created:

clip_image012

We can now modify this to add the “today plus 7 days” filter. The highlighted portion below is the new part, and the red circle is the + 7, which is key to the equation:

clip_image014

The result is a nice filter that shows only items with Bid Dates between today and 7 days out:

clip_image016

Labels: ,

Tuesday, November 3, 2009

Adding FBA Users to SharePoint Groups

We had a client that wanted to use a custom user database to authenticate users within SharePoint.  We first wrote a simple implementation of a custom membership provider for forms-based authentication.  Then we followed the necessary steps of setting up zones and modifying the web.config files with the membership information. 

The problem we ran into was the custom user database had no concept of roles.  In previous projects, I was able to grant access to the right users to the right places in SharePoint by manually granting access to a handful of roles, and allowing the users to continue to maintain user-role relationships as they always had.  In this implementation, we needed to figure out a way to programmatically grant FBA users access to the SharePoint site.  The code turned out to be pretty straightforward:

image

If you already have an SPWeb object from an existing SPContext, you do not need to create a new SPSite or SPWeb like I have done above.