Detecting the Current SharePoint User’s Regional Settings

Brian McCullough (@bpmccullough) pinged me on Twitter today with a question:

Brian was asking about an old blog post of mine where I basically screen scraped the data: Detecting the Current User’s Regional Settings with jQuery. It worked fine in the old days, but I didn’t think it would work anymore on SharePoint Online.

It was bugging me that I couldn’t remember the answer, so I went digging into some SharePoint pages to see what I could see. In two different SharePoint Online tenants, I saw that the _spPageContextInfo object has the following properties:

As I recall, if the userTimeZoneData  is not null, then the user has set their time zone themselves. If it is null, then the user is going along with the webTimeZoneData. I don’t have an example, but I’m pretty sure you can also check preferUserTimeZone  to see if the user wants to have her own time set.

Note that there are lots of other goodies in  _spPageContextInfo as well. The other properties which are pertinent to time zones are:

  • userTime24  – Does the user prefer 24 hour (military ) time?
  • webTime24  – Does the Web display 24 hour (military ) time?

The TimeZoneData  properties are also quite rich, showing the time zone description, the bias from GMT, Daylight Savings Time bias (usually 60 mins, but sometimes 30 mins), when DST starts and ends.

<update date=”2017-10-04″>

Bryan Mathews (@brmathews) reminded me on Twitter that there is a REST endpoint that provides some of this information as well.

If you use the /_api/web/RegionalSettings/TimeZone endpoint, you’ll get back information like this:

This is XML, but the data you’ll get back in JSON is the same. Note that there isn’t as much details here as in the  _spPageContextInfo, but this endpoint is available to you in any page.

</update>

Note that depending on the version of SharePoint you’re running, your mileage may vary.

Does anybody really know what time it is?

 

Advertisements

Auth, Auth, Auth – The Bane of Our Development Existence

I was reading through one of Bob German’s (@Bob1German) recent (and great!) posts about Porting REST calls to SharePoint Framework and thinking about a conversation Bob, Julie (@jfj1997) and I had at our “salon”* when I was reminded of a very specific truth about our working days.

Image from New York City Department of Transportation on Flickr

I would venture to say that some ridiculously high percentage of all of our time developing with SharePoint and Office 365 comes down to figuring out why we can’t get something to work due to authorization or authentication issues. Some say this is a necessary evil to create enterprise software, but I say it’s an unnecessary problem. Unfortunately, documentation about auth stuff tends to be tremendously opaque and difficult to understand. In the same way that I expect the server to be there and I want nothing to do with it directly, I want auth stuff to just work – easily.

Sadly, this has never been the case with so-called “enterprise-class” software. On some level, I think the obtuseness of auth is there on purpose to keep the bar high enough to exclude lower end hackers. Unfortunately, we all get caught up in the kudzu of auth as a corollary effect.

Image from NatalieMaynor on Flickr

Ok, so that’s my editorial on auth. I wish it were easier, but it isn’t.

Recently in one of my Single Page Applications (SPAs) for a client, we kept getting weird failures in posting data to a list. Weird mainly in that I never saw the error on my end, my client is far away, and screen sharing is tricky due to the technical proficiency on that end. I’m not dissing anyone; they are great at what they do, but debugging JavaScript with me is not in their wheelhouse.

If you write software, you know that the worst bugs to squash are those that happen sporadically, and only on someone else’s machine – especially if you don’t have direct access to that machine. As usually, though, it was simply me doing something dumb with auth – which is not easy. Have I mentioned that?

Basically, the problem was that while I was fetching the request digest from the page (this is a “classic” page in SharePoint Online), I wasn’t ever refreshing the token. In this application, people move around from page to page enough and use the application for short enough time periods that we simply hadn’t seen the problem in testing.

Smart people will think “Marc’s an idiot” on this one, but I play the fool so you don’t have to.

The code comes from a service I use everywhere in my applications for this particular client. It’s basically a set of utility functions that are useful when you’re using Angular with SharePoint. I’ve built the SPA using AngularJS (1.x), so my code below represents that. However, similar logic can work with jQuery or whatever instead of AngularJS and $q. I adapted it from some Typescript code Julie was using, so credit there, with a bit of add on from me. I’ve added a bunch of comments and have also left some of the console logging I used in debugging – but commented out.

The data we get back looks something like this:

The thing we care about most is the FormDigestValue , as that’s what we use to make our POSTs to SharePoint while it’s valid. But also note that there is an attribute in the returned JSON for FormDigestTimeoutSeconds. That’s the number of seconds that this particular token will be valid. In every tenant where I’m using the code, that works out to be 30 minutes. However, there may well be a setting which can change that, or Microsoft may change the time span on us. Because of this, I use the value to calculate how often to request a new token: T-2 minutes.

I’m pretty sure that there are pages in SharePoint which don’t do this correctly, so I’m not feeling like too much of an idiot. For example, when we click on an “app” – usually a list or library to you and me – in Site Contents, it opens in a new browser tab. Very often when I go back to that Site Contents page – when it has been sitting there for a while – I’ll get an unauthorized error. This may be fixed by now, though it has happened to me a lot.

I hope this is helpful. Using functions like this, we can make the whole auth thing easier on ourselves – and there zero reason to need to write this code from scratch every time. Store int in one place and if anything changes, you’ll have one place to fix it later.


  • We have occasional “software salons” – usually in one of our homes – where we simply get together to kick around what’s going on in our work lives, interesting things we’ve solved, etc. It’s a tremendously educational and useful exercise. I would encourage you to do something similarly informal if you don’t have enough water cooler time. At Sympraxis, we have no water cooler.

SharePoint and Office 365: The New Beautiful Cookbook Series

Most of us are “meat and potatoes” people when it comes to the technology we use. We like what we know and we know what we like. (Yes, there are vegan “seitan and potatoes” people, vegetarian “sprouts and potatoes” people, pescatarian “cod and potatoes” people, etc. I’m not trying to leave anyone out.)

Every once in a while, though, someone hands us a new ingredient – something we’ve never seen before, something we’ve never cooked with.

Image from the Netflix show Chef’s Table S3E6 – Virgilio Martinez

That new ingredient becomes a part of our pantry, and we want to try to cook with it. We’ve probably heard how delicious it is or how it can make an ordinary dish taste amazing.

Sometimes, we get a whole new palette of ingredients. (Many of us love to watch cooking shows for just this reason: we see novel dishes and decide if we’d like to try them at home.)

Image from the Netflix show Chef’s Table S3E6 – Virgilio Martinez

We need to take a ton of time to figure out what the new ingredients are, how we can work with them, and what we can cook. If we don’t cook with the ingredients pretty often, then we lose the knowledge of how to use them, what ripeness is best.

Writing off something because it tastes bad in one context means we may miss a great use of the ingredient later – a ripe plum tastes so much better than an unripe one. Once someone has eaten an unripe plum, they may decide they hate plums.

But if we can overcome these hurdles and learn about the new ingredients, we can make some incredible dishes.

Image from the Netflix show Chef’s Table S3E6 – Virgilio Martinez

This is what I think we are going through with SharePoint and Office 365 right now. Microsoft is offering us an entirely new set of ingredients with which to make our stew.

Let me give you an example…

In my “meat and potatoes” way of looking at the world, which has been pretty consistent for the last ten years or so, even though SharePoint and my approaches have evolved, I might use this set of ingredients:

  • A Single Page Application (SPA) written with AngularJS or KnockoutJS – or even just plain old JavaScript
  • A dollop of values passed on the query string to a…
  • Standard list form, with a little JavaScript mixed in to pre-populate some columns in the form
  • A SharePoint Designer workflow to add notifications on top (Substitute Alerts if your local market doesn’t carry SharePoint Designer)

But there are new ingredients now. Instead we could whip something up with these:

  • A SharePoint Framework Web Part (still maybe written with AngularJS or KnockoutJS)
  • Creating list items using REST based on the values in our SPFx Web Part
  • Microsoft Flow to add in the notifications and any process
  • Stir in a pinch of PowerApps – until they are ready

That’s quite a shift. We’re being asked to think about cooking in a very different way. We’ve been through stages of evolution before – new cooking techniques like sous vide (Sandbox Solutions), gelification (Add-In Model, nee App Model), etc. – but this time it’s really different. We’re not even sure if we’re supposed to like everything we taste. Is it just the next wave of kale frenzy or is it an ingredient that will last?

At this point, Microsoft is asking us to dream big, and reach for the previously unimaginable. I think we need to try to do it.

Image from the Netflix show Chef’s Table S3E6 – Virgilio Martinez

Some of us will be able to cook up truly amazing solutions on the “modern” platform. Don’t be afraid to give it a taste.

Image from the Netflix show Chef’s Table S3E6 – Virgilio Martinez

In case you didn’t figure it out, this post was inspired by the Netflix show Chef’s Table S3E6, which profiles the Peruvian chef Virgilio Martinez. It’s an outstanding series, and this particular episode was stellar.

Also see any volume in the Beautiful Cookbook series.