Link to home
Start Free TrialLog in
Avatar of Mohamed ElSheikh
Mohamed ElSheikh

asked on

How can i Filter list view by part of the URL in SharePoint 2010

Hi Experts,

I have main site and 5 different sub sites. I've created a link list in the main site contains URL, Description and Department Code columns.
I named each sub site by department code. so the sub-sute links looks like that:

http://mysite/team/main/12345/SitePages/Home.aspx

12345 is a department code and i want to get this code from the above URL and filter the view by current department code.


please not that i don't have Visual studio or SharePoint designer

kind regards,
Mohamed
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

This would be really easy... if it were a querystring argument to filter on;

http://www.sharemuch.com/2013/11/18/sharepoint-silly-questions-filtering-list-view-with-query-string-parameters/

If you could influence your navigation links to include the department code as a parameter you could progress quickly.

Because this is part of a url I can't see an alternative to using script. (VS and Designer not needed) If you're comfortable with script you need to do something like the below. Word of caution this is very thrown together and not tested. It can be improved! (For one thing check that a querystring param doesn't already exist and clash!) Deploy via Page layout or as CEWP on the page;

<script src="/SiteAssets/jquery-1.8.0.min.js" type="text/javascript"></script>

<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("appendDeptAsQS");

//Utility Function
function nth_occurrence (string, char, nth) {
    var first_index = string.indexOf(char);
    var length_up_to_first_index = first_index + 1;

    if (nth == 1) {
        return first_index;
    } else {
        var string_after_first_occurrence = string.slice(length_up_to_first_index);
        var next_occurrence = nth_occurrence(string_after_first_occurrence, char, nth - 1);

        if (next_occurrence === -1) {
            return -1;
        } else {
            return length_up_to_first_index + next_occurrence;  
        }
    }
}

//Get Department code between 5th and 6th / in url
function getDeptCode(url) {
	
	var leadSlash = nth_occurrence(url, '/', 5);
	var trailingSlash = nth_occurrence(url, '/', 6)
	
	return url.substring(leadSlash,(trailingSlash-leadSlash));
}

//Append Dept name as Query String
function appendDeptAsQS() {
	var origUrl = window.location.href;
	
	window.location.href = origUrl + '?dept=' + getDeptCode(origUrl) ;
	
}

</script>

Open in new window

Avatar of Mohamed ElSheikh
Mohamed ElSheikh

ASKER

thank you Jamie
sorry to be late. currently, I am away in a business trip
I will test your code when I return back to my office
i will let you know
thanks
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.