1,474 articles and 10,250 comments as of Thursday, April 1st, 2010

Wednesday, March 4, 2009

Filtering Content Based on IP Address

Guest Author: Eric Alexander, moderator, Stump the Panel

Recently a question was asked in Stump the Panel if content could be filtered on a Sharepoint site based on IP address. This is very doable with Sharepoint Designer and the data view web part coupled with a little custom coding.  This article will describe the process of filtering a Sharepoint list based on IP address.  The principles displayed can be adopted for various requirements.

We had a need to display a list on a web site to specific groups of people depending on how they were accessing our Sharepoint system.  Some things were to be intranet only, whereas some were to be available to those logging in from outside the network.

To get started, we need to know what IP addresses we will be filtering on.  In our case, users accessing the site from our network will have a 150.216.X.X. address.  This is what we will use to set up our filter.

For this example, I have created a small contact list that only intranet users should be able to see. 

Filtering Content

Now we have to start the process of getting this list on our site and getting the filter in place.  It’s time to fire up Sharepoint Designer and navigate to our site.  Double click the default.aspx page to open it so we can begin the modifications.  In the top left corner of Sharepoint Designer, Click on Data Source Library.  You should see the Contacts list present.

  Filtering Content

Drag the Contacts list onto the page into a web part zone.  You should see the web part on your page that looks like this:

Filtering Content

We would like to display more data, so to alter the columns on the page, click the greater than arrow to expand the common data views tasks options.  Click on edit Columns to change what data is being displayed, when finished click OK.  With the layout how we want it, we can move onto defining our variables and working on the logic to show/hide this web part.

To define our variable, click the greater than arrow to expose the data view options and click the Parameters link.  Click the button for a New Parameter.  Give the parameter a name; in this case I selected IPAddress.  Set the Parameter Source as Server variable and add the IIS variable REMOTE_ADDR as the Server Variable Name.  This will allow us to utilize the user’s incoming IP address.  Click OK.

Filtering Content

We have our data on the page as we want it, our variable defined, and now we just have to modify the data view web part code to utilize this variable.  In our implementation we decided to leave the web part title being displayed and had it output a simple text string on the page letting the user know that they were not allowed to view the contents of the list due to their IP Address.  The following code will demonstrate how to do that.

Now we need to go up into the code section of our Sharepoint page.  By clicking on the data view web part in the preview pane, it will highlight the entire web part code in the code view.  In the parameter bindings section, you should see our variable defined:


	
	
	
	
	

When you scroll through the code of the web part, you’ll notice it is a big XSLT transform.  This allows us to use the xsl:choose criteria to allow us to do our filtering.  Find the following line of code in the data view web part: <xsl:template name="dvt_1">.  Paste the following directly after it, substituting your IP address range as appropriate.



You are visiting the site off the corporate network. You do not have the ability to see this.

After this is pasted in, when you click on preview pane below, it will throw an error because of invalid syntax.  We need to close our xsl tags to complete the filtering.  Sharepoint Designer will highlight </xsl:template> in yellow as to where the error is.  Paste </xsl:otherwise></xsl:choose> between the closing table tag and the closing template tag.

So altogether, our change looks like



You are visiting the site off the corporate network. You do not have the ability to see this.
Table
Full Name Address City State/Province ZIP/Postal Code E-Mail

Now we need to save our changes and see the results on the Sharepoint page.  To test the functionality of the show/hide, remove the ! in the above code, and the web part contents will disappear and display our custom message.

This provides the groundwork for using the IIS server variables to show or hide contents on a page.

 

Please Join the Discussion

7 Responses to “Filtering Content Based on IP Address”
  1. Ian Morrish says:

    Working example show all available variables
    http://www.wssdemo.com/Pages/ServerVariables.aspx
    (yes, I know it gives away a lot of information…)

  2. Nicola Williams says:

    Hi there, This is great – I originally asked the question, so thanks very much for posting this.
    Can I also ask how would you filter on a range of ip addresses i.e. 172.16.2.51 – 172.16.2.55? I’m not sure of the syntax you would use in XSL/SPD.

    Nicola

  3. Eric says:

    I’ll look into alternatives to account for a finite subset of IP ranges. As this was my first use of the data view web part and IIS server variables, I’m still a bit green on ways to use them.

    I’ll post a follow up comment or maybe turn this into a series.

  4. Edward Kelty says:

    This is really good information – can this be modified somehow to take the IP address and save it to a field in a list entry that’s being updated?

    Thanks, Ed

  5. prio says:

    This is really cool. I always trace people with http://www.ip-adress.com

    But i did not know anything about filtering based on the ip. This is good stuff.

  6. Eric says:

    Based on some research, there is some logic for using a “Between” function in XSLT. http://www.xsltfunctions.com/xsl/functx_between-inclusive.html is a link to the function. It might take rewriting of the transform as this is written in version 1.0 and the function is available in 2.0.

    @Edward, that I’m not sure of XSLT is a data formatting structure, not a scripting language. The variable is coming from IIS so it could be used in that manner.

    @Prio Thanks!

  7. johnm says:

    Very Handy. Just what I wanted. Well, sort of. Now, how to block a whole page or site based on IP?


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!