7

I've been banging my head against my desk for a while now trying to use this REST endpoint in order to convert local time to UTC time.

The specific url I'm using is this:

http://<sitecollection>/<site>/_api/web/RegionalSettings/TimeZone/localTimeToUTC(date)

What I can't seem to figure out is how to pass a date and time to it without having it throw this error in my face:

A potentially dangerous Request.Path value was detected from the client (:).

Or claim that it cannot parse the date and time.

I simply cannot find any documentation that explains how I should encode the date and time to avoid the issue. The only answers seems to be to disable the security handler that throws the error, but I don't want to potentially compromise the system simply so that it will receive my date. This is part of the REST api so there must be some other way to encode the date and time that will allow me to use the endpoint right?

These are some of the urls I've tried and the error they return:

//Potentially dangerous (:)
/_api/Web/RegionalSettings/TimeZone/localTimeToUTC("2015-11-16T14:54:06.802Z")
/_api/Web/RegionalSettings/TimeZone/localTimeToUTC("2015-11-16T14%3A54%3A06.802Z")

//InvalidClientQueryExpression
/_api/Web/RegionalSettings/TimeZone/localTimeToUTC("2015-11-16T145406802Z")
/_api/Web/RegionalSettings/TimeZone/localTimeToUTC("2015-11-16T145406")
/_api/Web/RegionalSettings/TimeZone/localTimeToUTC("2015-11-16")

Edit:

For those looking for a way to do this with CSOM/JSOM, here is the way I solved it.

var date = "2015-11-17 16:00:00",
    ctx = SP.ClientContext.get_current(),
    regionalSettings = ctx.get_web().get_regionalSettings(),
    timeZone = regionalSettings.get_timeZone(),
    utcTimeResult = timeZone.localTimeToUTC(date);

ctx.load(regionalSettings); //Note that regional settings is the object you need to load
ctx.executeQueryAsync(function(){
    var utcTime = utcTimeResult.get_value(); //Proper Date object with timezone offset
});
  • 1
    The same issue with asterisk(*) – Serhiy Jun 22 '17 at 8:55
1

Have you tried the format that SharePoint throws out if you make a REST call?

2015-11-16T16:15:15Z

Also try to escape special characters

A little bit more of your Code would be helpfull

  • 1
    Thanks for the quick reply. There is no code as I'm trying to call the REST apis directly using Postman (Chrome plugin). I'll post some of the urls I've tried that fail. – Thomas Nov 16 '15 at 15:23
  • Do you have the permissions to the list you want to call via REST? I've just tried a REST call with filtering by date and it seems to be working pretty well... /_api/web/lists/getbytitle('Tasks')/items?$filter=StartDate gt '2014-11-03T23:00:00Z' – Dave Nov 16 '15 at 15:34
  • I'm running as site collection admin. The problem is specific to the localTimeToUTC REST call as the date is part of the "path". For filters and the like it works fine. – Thomas Nov 17 '15 at 5:04
0

The same case i have faced when i was using redirect and post in C#. So what i did i encode the parameters using UrlEncode method. In JS you can try encodeURIComponent(param)

  • This does not work as the character % is also apparently a dangeours path character (the second url above is the url encoded date using exactly encodeURIComponent) – Thomas Nov 22 '15 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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