none
How to pass variable in to REST API query in JSOM sharepoint online RRS feed

  • Question

  • Hi Team,

    I need to pass variable value into REST API query in JSOM sharepoint online. I need to pass _ID like a filter  in the REST query in " url: " to retrieve records based on the ID value. Can someone suggest this?

    Below is the logic I am using:

    $(document).ready(function () {

        var _ID = GetUrlKeyValue('ID');     
        loadData();
    });

    function loadData() {
        try {
            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Logs')/items?$select=ID,SiteId,SiteTitle,ErrorDetails&$expand=SiteId",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },



    Ganesh

    Friday, August 16, 2019 12:53 PM

Answers

  • Hi Ganesh,

    Would you please provide us with an update on the status of your requirement?

    Did you check my code below:

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        var _ID = GetUrlKeyValue('ID');     
        loadData(_ID);
    });
    function loadData(keyID){
    	$.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Logs')/items("+keyID+")?$select=ID,SiteId,SiteTitle,ErrorDetails&$expand=SiteId",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function (data) {            
    			var log=data.d;
            },
            error: function (data) {
                //alert("Error");
            }
        });
    }
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Ganesh Naga Thursday, August 22, 2019 10:05 AM
    Thursday, August 22, 2019 2:43 AM
    Moderator

All replies

  • You need to pass the _ID to loadDate as loadData(_ID)

    and then use the below url:

    _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Logs')/items(_ID)?$select=ID,SiteId,SiteTitle,ErrorDetails&$expand=SiteId"

    Please mark as answer if your issue is resolved.

    Sunday, August 18, 2019 6:26 PM
  • Hi Ganesh,

    Would you please provide us with an update on the status of your requirement?

    Did you check my code below:

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        var _ID = GetUrlKeyValue('ID');     
        loadData(_ID);
    });
    function loadData(keyID){
    	$.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Logs')/items("+keyID+")?$select=ID,SiteId,SiteTitle,ErrorDetails&$expand=SiteId",
            type: "GET",
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function (data) {            
    			var log=data.d;
            },
            error: function (data) {
                //alert("Error");
            }
        });
    }
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Ganesh Naga Thursday, August 22, 2019 10:05 AM
    Thursday, August 22, 2019 2:43 AM
    Moderator
  • Thanks Dennis, this is working as expected and good to know. 

    and I also passed another way like below in to the REST query:

    $filter=SiteId/ID eq '" + _ID + "'";



    Ganesh

    Thursday, August 22, 2019 10:07 AM
  • Hi Ganesh,

    Glad to hear that you solve this issue, thanks for your sharing.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, August 23, 2019 1:00 AM
    Moderator