0

I'm using sharepoint REST api using JavaScript to replace a file in a doc library. Here is the url structure I'm using.

<!-- language: lang-js -->

props.webUrl + "/_api/web/lists(guid'" + props.listId.slice(1, -1) + "')/RootFolder/Files/Add(url=@filename,overwrite=true)?@filename='" + encodeURIComponent(props.file.get_name()) + "'"

This call is not working if my filename contains apostrophe (ex: Shekar's.pptx). The browser is rejecting the call and throwing Bad request error and showing message like

The expression "web/lists(guid'cde87068-baf9-4863-910e-049bd7f3e04e')/RootFolder/Files/Add(Url='Shekar's.pptx',overwrite=true)" is not valid`.

I know the expression 'Shekar's.pptx'is wrong. I tried to encode with encodeUri, encodeUriComponent, escape and escapeProperly- None of them worked, still getting the same error messsage. Any ideas?

2 Answers 2

4

Use two quotes. That is 'Shekar''s.pptx'

1

@Nadeem Yousuf is correct. You should escape it using either double quotes or by using \

Here is a little function you can use for that

function escapeApostrophe(title) {
    title = title.replace(/'/g, "''")
    return title
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.