Link to home
Start Free TrialLog in
Avatar of sharepoint0520
sharepoint0520

asked on

Generate the Dynamic REST API url in sharepoint 2013

Hi,
 We have sharepoint list view where user filter and sort data. Now we would like get sorting and filter parameters and generate rest api url by script editor to fetch some list items. is it possible to read filter and sorting parameter by url or ctx object?

There is a object ctx.ListData.Filterlink gives some parameters but it's in another format.

"?FilterField1=LinkTitle&FilterValue1=A&SortField=LinkTitle&SortDir=Asc&PageView=Shared&"

Here is the REST Api url where i need to add filter and sorting parameters.
 url: siteUrl + "/_api/lists/getbytitle('Test_Custom_List')/Items?$select Title",



 <script>
var siteUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
 
jQuery.ajax({
  // need to generate dynamic url based on filter and soting on page
    url: siteUrl + "/_api/lists/getbytitle('Test_Custom_List')/Items?$select Title",
    type: "GET",    
    headers: { 
        "accept": "application/json;odata=verbose",
        "content-type":"application/json;odata=verbose",
        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
    },
    success: function(d) {
        var stringData = JSON.stringify(d);
        var jsonObject = JSON.parse(stringData);
        var results = jsonObject.d.results;
        for(i = 0; i < results.length;i++) {
            console.log(results[i]["Title"])
        }
    },
    error: function() {
        console.log('fail');
    }
});
</script>

Open in new window

Avatar of Uttam (Kukdai) Dhakal
Uttam (Kukdai) Dhakal
Flag of United States of America image

Looks like you already have the idea of adding the filter and sorting in the URL.
This should be pretty simple just to add a filter and sorting parameters.
https://www.c-sharpcorner.com/article/sharepoint-2013-using-rest-api-selecting-filtering-sortin/
Is it something that you are looking.
If you can provide more details?
Avatar of sharepoint0520
sharepoint0520

ASKER

Hi,
 I have to generate filter and sorting parameter dynamically by reading the current url. For instance user filter and sort the sharepoint listview so url will change. Now we have to read that url and fetch filter and sort parameter and generate the url for rest api uri. In below url i have filter by name and sort by Employee ID.

https://sharepoint.com/sites/TeamSite/Lists/Employee/Test.aspx#InplviewHash7e69bb93-ffcc-4b23-b598-93de4e891d5c=SortField%3DEmployee_x0020_ID-SortDir%3DAsc-FilterField1%3DLinkTitle-FilterValue1%3Dtitle1

So my requirement is to read current url and generate rest api url.

Let me know if you still have any confusion.
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.