How to allow special chracter using Rest aPI

Mohammad Qasim 576 Reputation points
2021-02-01T07:53:06.457+00:00

Greetings

Using Sharepoint 2016 ( on Prem )

I am using rest API to retrieve record by passing Value ( which is String value like "SEP/SPL/PRD/III/BMR/'S&S'/01-00" )

Problem : Its not retrieving record using REST Api when i pass special character. if I don't simple value , so it works like
( SEP/SPL/PRD/III/BMR/'SS'/01-00" )

Solution Required : How can I pass this special character to retrieve record

Note: In Code " nomenclature[0] = SEP/SPL/PRD/III/BMR/'S&S'/01-00"

Image attached is for reference.
62328-m-percent.png
62367-m-percentcode.png

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,582 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-02-02T03:09:31.1+00:00

    Hi @Mohammad Qasim ,

    & characters are not encoded by default in URL. We need to specify the encoded values for those special characters in rest api. And for a single apostrophe ( ' ), we need to replace a single apostrophe with two to make it work.

    You could use the below function to achive this:

    function fixedEncodeURIComponent(src) {  
    return encodeURIComponent(src).replace(/[']/g, function (c) {  
            return '%' + c.charCodeAt(0).toString(16) + '%' + c.charCodeAt(0).toString(16);  
        });  
    }  
    

    Then construct the url like this:

    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('TestList')/items?$filter=Nomenclature eq '" +fixedEncodeURIComponent(nomenclature[0])+"'",  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments