Blog Archives

How to access properties of Office.js objects that don’t exist in the Typescript definition file

When developing Office Add-ins and using Typescript, I’ve found the Office.js Typescript definition file available at DefinatelyTyped to only support a fraction of the objects and properties that are available within the Office.js library.

To give you an idea of what I mean, here is a list of properties that are available on the Office.context.mailbox.item object (according to the API documentation in the Outlook Dev Center)

clip_image004

 

And here are all the properties of that same object using the Typescript definition file:

image

I was left wondering where the rest of the properties were. They simply don’t exist in the Typescript definition file. So this leaves us in a bit of a bind, because we are using Typescript we can’t just reference a property that doesn’t exist in the Typescript definition file (even though we know the property will exist at run-time). The Typescript compiler will do it’s job well and throw up a compile time error that the property does not exist.

Without going to the effort of taking the Office.js Typescript definition file and extending it yourself to start filling it out you may want to consider the following work around.

We can declare an object in Typescript without a specific type by specifying it’s type as any. If we do this to an object within the Office.js library we can get an un-typed handle to the object. As the object in now un-typed, we can call any property of that object we like (whether it exists or not). Below is the code that will give us access to the subject of the email that is not available in the Typescript definition file.

image

If the property exists at runtime then great, if not then we will get a run-time error. It is definitely a step backwards and is why we use Typescript in the first place!

It would get a bit unwieldy if you used this technique throughout your code, and I’d like to think that as we get updated Office.js Typescript definition files that we can remove this type of code from our project and access the properties in a properly typed way. To isolate your use of this technique to a central location and facilitate removing the code later on, I’d suggest creating a class that takes in the object (e.g. Office.context.mailbox.item) then inside the class it gets the un-typed handle to the item and provides methods or properties that return the missing properties (with the bonus that the values returned can have a type associated with them). Below is an example of a class with static methods that provide typed access to missing properties on a mailbox item.

image

Hopefully the Office team will see the value in publishing current and complete Typescript definition files so we don’t have to write code like this in future.

Fingers crossed.

2 Simple Techniques for Creating SharePoint Views Grouped by Date Columns

Here are 2 techniques for creating views in SharePoint when you want to group on dates. Throughout this article I’ll be using the example of email that have been saved to SharePoint where the email date has been stored in a SharePoint column called “Email Date”. The Email Date is saved with both a date and time component.

sharepoint-views-date-group-by-cameron-dwyer-01-email-date-column

Here’s the All Documents view of the library showing the Email Date column

sharepoint-views-date-group-by-cameron-dwyer-02-all-documents

If we try to use the Email Date column to group by in the view

sharepoint-views-date-group-by-cameron-dwyer-03-group-by-email-date

The view creates a group for each different day (the time component is ignored)

sharepoint-views-date-group-by-cameron-dwyer-03-view-grouped-by-date

2 Level Grouping: by Year and then by Month

This view is going to get very busy with a grouping for each day, so how about we split it up a bit by creating 2 levels of grouping, firstly by year and then by month.

Unfortunately we can’t just use the existing Email Date column to achieve this, instead we will create two calculated columns to use for the groupings (one for year and another for month).

To create the year column:

Create as a calculated column
Set the returned data type as Single line of text (this gives better formatting control and the year will still sort properly as text)
Set the formula to =TEXT([Email Date], “yyyy”)

sharepoint-views-date-group-by-cameron-dwyer-04-calculated-year-column

To create the month column:

Create as a calculated column
Set the returned data type as Single line of text (this gives better formatting control and by padding a single digit month with 0 will still sort properly as text)
Set the formula to =TEXT([Email Date],”mm (mmmm)”)

sharepoint-views-date-group-by-cameron-dwyer-04-calculated-month-column

Now if we create a new view (based on the All Documents view) and add two levels of grouping based on our new calculated columns

sharepoint-views-date-group-by-cameron-dwyer-05-view-group-by-year-then-month

Our new view now renders in SharePoint giving collapsible grouping at both the Year and Month levels

sharepoint-views-date-group-by-cameron-dwyer-06-view-result-group-by-year-then-month

This is now a lot easier to navigate and drill down, and it’s quite nice to see the counts against each grouping as well. In this example I set the groups (both Year and Month) to sort in descending order. This means that the latest will be at the top (notice 2016 is above 2015, and within 2016, February is above January).

Faking a 3 Level Grouping: by Year/Month Combined and then by Day

SharePoint has a limitation in that you can only create two levels of grouping. If we want to have a third level (under month) that grouped together all the email from the same day then we can’t just go and add a third level of grouping. What we can do instead though is create a slightly more complex calculated column that combines both the year and month and use it as our top level grouping. So let’s do that now by creating a Year/Month column:

Create as a calculated column
Set the returned data type as Single line of text (this gives better formatting control and we can carefully craft the text so it still sorts year/month properly as text)
Set the formula to =TEXT([Email Date],”yyyy-mm (mmmm)”)

sharepoint-views-date-group-by-cameron-dwyer-07-calculated-column-year-month

We are also going the need a calculated column to group on the specific day, so I’ll create the Day column

Create as a calculated column
Set the returned data type as Single line of text
Set the formula to =TEXT([Email Date],”dd (ddd, d mmm yyyy)”)

sharepoint-views-date-group-by-cameron-dwyer-08-calculated-column-day

Now we create our new view (based on the All Documents view) and add groupings based on the Year/Month column and the Day column. Again we will sort descending to get the latest at the top.

sharepoint-views-date-group-by-cameron-dwyer-09-view-settings-group-year-month-day

The resulting SharePoint view gives us the year/month breakdown at the first level and we can then drill down to a specific day within the month.

sharepoint-views-date-group-by-cameron-dwyer-10-view-group-by-year-month-day

I’m sure you can now go forward and add your own tweaks and formatting changes to get better date categorized views out of SharePoint.

 

Neat trick to enable LTE on Surface Pro 4 (for Windows Phone users)

Microsoft Surface Pro 4 doesn’t have built in LTE capability (at the time of writing this). What this means is that your Surface Pro 4 can’t connect to the internet on its own. It needs to connect to a wireless network/hotspot, or use a USB adaptor to provide access to the internet (e.g. physical Ethernet connection).

I’ve read a few articles dismissing the Surface Pro 4 because the lack of LTE (or SIM card). I don’t really find it an issue, why? Because of a neat little trick that the Surface can do with my Windows Phone.

Most smartphones these days support tethering (also called internet sharing, or wireless hotspot). This effectively shares the internet connection that your phone has with other devices. Other devices connect to your phone (which acts as a wireless hotspot) and then get access to the internet (which you can secure with a password). Ok, boring blurb over, you already knew that you could get out to the internet by using your phone right?

Here’s what you are probably used to:

  • Pull out your phone
  • Unlock your phone
  • Navigate into the phone settings area
  • Find the tethering/internet sharing settings
  • Turn tethering/internet sharing on
  • Now back on your Surface, if you’ve set up the Wi-Fi connection to your phone to auto connect you should find the connection is made automatically after a few seconds and you’re on the internet

But this process is just so clunky and slow.

So here’s the neat trick that your Windows Phone, teamed up with your Surface is capable of:

  • Leave your phone alone – in your pocket, bag, backpack, desk drawer (wherever as long as it within a reasonable range)
  • On your Surface, simply click on the Wi-Fi icon in the task tray to show any available Wi-Fi connections. You should see your phone listed (even though the tethering is not enabled on your phone). You can see my NOKIA Lumia phone in the list below and it shows as “Mobile hotspot, off”

microsoft-surface-pro-LTE-SIM-01-select-phone-hotspot-cameron-dwyer

  • Now I just select the NOKIA Lumia option in the list and click Connect

microsoft-surface-pro-LTE-SIM-02-enable-tethering-automatically-cameron-dwyer

  • Without touching my phone, the Surface is able to turn on the hotspot/tethering feature of the phone and connect to it.

microsoft-surface-pro-LTE-SIM-03-connected-cameron-dwyer

 

I would also suggest changing the connection to your phone to set it as a metered connection. This will prevent Windows from performing costly data transfers such as downloading updates.

So do I care that my Surface Pro 4 doesn’t have LTE (SIM card)? Not at all, because I’ve always got my phone close by and I can now share its internet connection with just 2 clicks. It’s a pretty cool integration that makes a world of difference.

I’ve only tried this on a Surface Pro 4 (Windows 10) and Nokia Lumia 930 (Windows 10), although the articles below suggest that this feature also works on Windows 8.1.

Further Reading

http://www.neowin.net/forum/topic/1229889-windows-can-now-control-internet-sharing-on-windows-phone/

http://www.surfaceforums.net/threads/automatic-tethering-to-windows-phone.6305/

How to Dock Windows Left & Right using Multiple Monitors

Here’s a quick tip that one of my colleagues showed my this week that makes working with multiple monitors a lot easier. Thanks to @FreeRangeEggs for this tip.

 

The Problem

You’ve got 2 windows (A and B) and you want to dock them side by side on monitor 1.

cameron-dwyer-windows-doc-multi-monitor-01-window-a-b

Its easy to dock window A to the left on monitor 1. Just drag the window to the left of the monitor and it will “snap” or dock to the left side and fill up half the screen.

cameron-dwyer-windows-doc-multi-monitor-02-drag-window-a-left

But now if we try to do the same with windows B, instead of docking the the right of monitor 1, the window just glides across onto monitor 2.

cameron-dwyer-windows-doc-multi-monitor-03-drag-window-b-rightcameron-dwyer-windows-doc-multi-monitor-04-window-pushes-across-monitor

The Solution

So how do we get window B to dock to the right side of monitor 1? First select window B then use the keyboard shortcut WINDOWS KEY + RIGHT ARROW.

cameron-dwyer-windows-doc-multi-monitor-05-win-plus-right

Simple as that.

Note: You can also use WINDOWS KEY + LEFT ARROW to dock to the left side of the current monitor. This can help you do the reverse (that is, if you are on monitor 2 and want to dock a window to the left side)

Working with Non-Office File Types in SharePoint & Office 365

Office 365 and SharePoint work quite nicely when you are working with Microsoft Office file types. Things like Word, Excel and PowerPoint files. Once you really start using SharePoint however, you want to store many more types of files in SharePoint. This is natural and you can actually get the files into SharePoint without too much hassle.

Editing and working on Office file types is pretty good. Just click on the file in SharePoint and you can now choose to do the edits directly in the browser (with online versions of the Office products) or edit the files in the full desktop version of the Office products.

But what’s the story with file types that don’t open in, or are not associated with the Office products?

Well that’s when things get a little clunky, and in this post I’m going to show you how OnePlaceDocs Explorer turns virtually any software application into a “SharePoint” aware application that you can use to open/edit and save files that live in SharePoint. No longer are you just restricted to using the Office application that were designed to work with SharePoint, now you can edit files in any application you want.

So what is OnePlaceDocs Explorer? It is a bit like Windows File Explorer except it is purpose built for looking at SharePoint and Office 365 environments rather than files on your local computer or network.

To give you some orientation, the screenshot below shows OnePlaceDocs Explorer and points out the 3 pane layout which is similar to Windows File Explorer.

edit-files-directly-from-sharepoint-01-office-365-explorer-cameron-dwyer

 

Let’s look at a common scenario…

Editing Images Files in SharePoint/Office 365

It’s actually very difficult to edit image files that are stored in SharePoint. If you try to open the file, the web browser simply displays the image in the browser (because it natively knows how to). This doesn’t help you when you want to edit the image though. Your options are to either:

  • Download the image from SharePoint to your local computer, edit it in your image editing program of choice, then manually upload the file back to SharePoint replacing the existing file
  • Sync the whole library offline via OneDrive and then you can work with the file as though it is a normal file on your desktop. Saving changes to the local file will sync back to SharePoint.

Here’s the OnePlaceDocs Explorer way.

Select the image file and select Open With (from the ribbon or context menu action)

edit-files-directly-from-sharepoint-02-open-with-cameron-dwyer

Select any application from the list of applications installed on your computer that recognise this file type. I’ll choose good old Microsoft Paint just to prove that a very basic application that has no interoperability with SharePoint will work fine.

edit-files-directly-from-sharepoint-03-open-with-paint-cameron-dwyer 

Paint now starts up and the image stored in SharePoint is sitting there ready for me to edit.

edit-files-directly-from-sharepoint-04-open-with-paint-original-cameron-dwyer

I’ll make a few changes and just save using the standard save action in Paint or pressing CTRL+S.

edit-files-directly-from-sharepoint-05-open-with-paint-edited-awesome-cameron-dwyer

Believe it or not, that is it.

If we return to OnePlaceDocs Explorer we can see in the changes showing in the preview pane.

edit-files-directly-from-sharepoint-06-changes-in-preview-pane-cameron-dwyer 

Just to prove that it really has changed the file in SharePoint, I’ll open this document library in a web browser.edit-files-directly-from-sharepoint-07a-open-in-browser-cameron-dwyer

We can then find the same file in SharePoint

edit-files-directly-from-sharepoint-07b-open-in-browser-cameron-dwyer

And there’s my modified image.

edit-files-directly-from-sharepoint-08-modified-file-in-sharepoint-cameron-dwyer

Editing the file using OnePlaceDocs Explorer really wasn’t any different to opening a file from my local computer. So now you have no excuse for not putting those files in SharePoint where they belong!

This same technique can be used to open any type of file with any installed application. Another common scenario is opening PDF files with Adobe Acrobat or another PDF authoring tool.

How to enable “Hey Cortana” with Windows Phone 8.1 Update in Australia

Currently only USA and UK Windows Phones can access Cortana Beta. Some other countries such as Australia have access to Cortana Alpha. The “Hey Cortana” feature which allows you to start Cortana listening by simply saying “Hey Cortana” is not included in the Alpha and we have to wait patiently for the Beta. There is always the option to opt in to the Developer Preview and get the latest goodies, but if you’d prefer to keep your phone on a stable build and still get access to the “Hey Cortana” feature here’s a quick way to acheive it. In a nutshell you can change your phones region to UK just long enough to enable “Hey Cortana” and then switch your region back. After doing this “Hey Cortana” says it’s disabled in the settings but seems to keep working fine.

Here’s how to do it:

Settings | System

I’ve you are in Australia you should see that Region currently says Australia, English (Australia) and Speech is set to English (United Kingdom)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (1)

Open the Region setting (it should currently be set similar to below)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (2)

Change the Country/Region from Australia to United Kingdom and then press the restart phone button that appears once you change the Country/Region

enable-hey-cortana-australia-windows-phone-cameron-dwyer (3)

When your phone restarts go to Settings | System. Hey Cortana will still show as disabled

enable-hey-cortana-australia-windows-phone-cameron-dwyer (4)

Open the Hey Cortana settings you should now be able to switch this feature on.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (6)

Continue on to setup (train) Cortana. You will only be able to do this while your Country/Region is set to United Kingdom, once we switch it back to Australia you will loose the ability to retrain.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (7)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (8)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (9)

Once you’ve completed the Cortana training you should be back at the settings screen.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (10)

Now your region will be set to United Kingdom, English (Australia)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (11)

Now lets change the Region | Country/Region back to Australia. Select the restart phone button when it becomes available.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (12)

Once your phone restarts go to Settings | System. You should find that Hey Cortana is disabled.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (13)

enable-hey-cortana-australia-windows-phone-cameron-dwyer (14)

But if you say “Hey Cortana” either while you phone screen is on (or even if you phone is locked with screen off), Cortana will respond by turning the screen on and making a beep sound. Cortana is now listening for your command.

enable-hey-cortana-australia-windows-phone-cameron-dwyer (15)

How to: Enable outlining (collapsible statement blocks) for C# code in Visual Studio

I’m often modifying existing C# code in Visual Studio and find myself trying to line up opening and closing braces and trying to figure out what level of nesting I’m currently at. It’s easy when the block fits on a single screen without scrolling, but dive into some complex logic where you’ve got plenty of nested if, else, switch, try, catch blocks it’s easy to get disoriented scrolling up and down trying to figure out the logic.

Yes I can hear the code purists begging me to restructure the code and encapsulate logic away into smaller more focused methods. I don’t disagree, it fact that might be the reason I’m looking at the code in the first place, but I need to understand the logic before I start pulling it apart.

Whatever the reason I’m sure you’ve all been in the situation, why can’t I just collapse this  switch block, or if block? Wouldn’t that make things simple? Luckily the solution to this problem is simple – there’s a free Visual Studio Extension called C# Outline and it’s available for Visual Studio 2010, 2012, 2013 & 2015.

This nifty extension provides outlining (expand/collapse) for all block elements that use curly braces { }; Just like you get out-of-the-box for classes and methods It’s simple and a massive time saver.

cameron-dwyer-csharp-visual-studio-outline-collapse-blocks

Office Lens: The simple and free way to create an Outlook Contact from a business card

I’ve found Office Lens to be an awesome app for Windows Phone. Recently the app was enhanced to allow for the specific task of scanning business cards.

So how does it work? Simply fire up the free Office Lens app on your Windows Phone and tell it you want to scan a business card. Take the photo (scan) and save it to OneNote. Here’s what I ended up with when I scanned my own business card.

office-lense-scan-business-cards-cameron-dwyer-01-onenote

In OneNote (either directly on the phone or back on your PC – once OneNote syncs) you get the full picture of the business card at the bottom of the OneNote page, but it creates a section of formatted text above the image which extracts the main details from the business card and creates hyperlinks to call, email or open website addresses. Pretty cool.

The awesomeness doesn’t stop there. Embedded in the OneNote page is a .vcf file “BizCard”.

office-lense-scan-business-cards-cameron-dwyer-01-open-business-card

Click on the “BizCard” file and a full Outlook Contact profile is composed with all the details filled out ready to save.

office-lense-scan-business-cards-cameron-dwyer-03-create-new-outlook-contact-from-business-card

The screenshots above are from OneNote Desktop on my PC, but you get similar functionality directly on your phone. You can see the “BizCard” link and pressing it composes a new contact directly on your phone with all the details filled out which you can then edit/change before saving. Now that is really cool.

I’m looking forward to the next SharePoint Conference when I come home at the end of the day with a wad of business cards in my pocket.

Here’s an official article about the feature

http://blogs.office.com/2014/12/08/office-lens-gets-networking-scan-business-cards-onenote-contacts-outlook

You can download the Office Lens Windows Phone app for free from the Windows Phone Store.

Getting Started: What are .ps1 files and how do you use them?

What is a .ps1 file?

powershell-ps1-cameron-dwyerIf you want to save a series of PowerShell commands in a file so you can run them again later then you effectively creating a PowerShell script. This is simply a text file with a .ps1 extension. The file contains a series of PowerShell commands, with each command appearing on a separate line.

Why would you use a .ps1 file?

When working with SharePoint I find that most of the time to achieve something useful I’m writing several lines of PowerShell with the result of each line feeding into subsequent lines. The scripts often end up resembling source code more than traditional command line input. So using .ps1 files work well, you simply write up the commands and then execute the .ps1 file to run all the commands, make changes re-run until you end up with a script that does what you want and you’ve ironed all the bugs out.

Saving your commands to a .ps1 file also makes them portable in that you can take the file onto another computer (another SharePoint server) and execute the same .ps1 file on it. By making use of input variables, input files, and prompting for user input you can craft .ps1 files that perform common functions and you can get a lot of reuse without having to change the commands within the .ps1 file.

How do you run a .ps1 file?

You can’t just double-click on a .ps1 file in Windows Explorer to run it. The most common way to run the .ps1 file in from a PowerShell Command window. The syntax differs a bit from the old command line.

You can either specify the entire path to the file such as:

c:\temp\myscript.ps1

Or if the current directory in the PowerShell window is set to the directory containing the .ps1 file you can use the .\ shortcut to execute the script:

.\myscript.ps1

That’s it for a very basic intro to .ps1 files, happy scripting!

Getting Started: The Basics of Using PowerShell with SharePoint

I’m a SharePoint guy, and certainly no PowerShell expert. I know enough PowerShell to get things done in SharePoint from time to time. I can often go months without using PowerShell and talking to colleagues I don’t think I’m alone in the SharePoint community.

So if you are just starting out with using PowerShell to do something with SharePoint, or it’s been a while and you need a refresher on the basics then you’ve come to the right place.

Before we get started I’m writing this article based on SharePoint 2013 running on a Windows 2012 server.

The ground rules

1) Run your PowerShell scripts from the SharePoint server itself

It is possible to run PowerShell against a remote SharePoint server rather than on the server itself, but it requires some setup on the SharePoint server side and let’s face it, it’s not basic. If you need to do it you need to be searching for “Remote PowerShell in SharePoint”.

2) Use Windows PowerShell ISE (not SharePoint Management Shell)

PowerShell needs to be executed/run at a PowerShell Console or PowerShell Window – this window looks like a standard Command Line window and I don’t find it too inviting.

Instead of the SharePoint 2013 Management Shell, I use the Windows PowerShell ISE.

sharepoint-powershell-getting-started-cameron-dwyer-windows-powershell-ise

Why? It’s like comparing Visual Studio with Notepad. The ISE is an environment for developing PowerShell scripts that gives you nice syntax highlighting, debug with breakpoints, intellisense and more. It’s more like a development environment than a command line. I think you’d agree it looks slightly more advanced.

sharepoint-powershell-getting-started-cameron-dwyer-powershell-ise

3) Ensure the SharePoint PowerShell snapin is loaded

When using the “SharePoint 2013 Management Shell” (the ugly black one) it automatically loads a “snapin” which is basically a PowerShell extension that gives you a series of commands for working with SharePoint objects. When you use the Windows PowerShell ISE it has no idea of SharePoint, so you need to load the SharePoint snapin manually. The simplest way to do this is just to add the following code snippet to the start of all your scripts.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell
}

 

4) Save a script to file before you try to run it

Windows PowerShell ISE will have trouble running a script if you haven’t saved it to disk yet. PowerShell scripts are saved as files with a .ps1 extension.

 

5) Talk to SharePoint via the SharePoint PowerShell Cmdlets

Here’s a reference of all the SharePoint 2013 PowerShell Cmdlets you can use to work with SharePoint.

 

Your first script

Pre-flight checklist:

  • Start Windows PowerShell ISE
  • Add code snippet for loading the SharePoint snapin
  • Save the script as a .ps1 file

sharepoint-powershell-getting-started-cameron-dwyer-first-script

You can now start cutting and pasting examples from the internet and modifying them to work with your environment. The following 2 lines get a handle on the SharePoint website at the URL http://vs-server12 and then output the ID of the website.

To run the script and see if it work, click the Run button.

sharepoint-powershell-getting-started-cameron-dwyer-run-script

Any output from running your script is shown in the output window below the script editor.

sharepoint-powershell-getting-started-cameron-dwyer-output

You’re on your way, just remember you can only access the SharePoint server on which you are running your scripts. Accessing remote SharePoint servers is possible but you need to do special magic stuff to make that happen Smile

 

If you encounter an error along the lines of:

Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern “Microsoft.SharePoint.PowerShell” were found.

You’ve ignored ground rule 4 and the pre-flight checklist by forgetting to save your script before running it (yes, I still do this too).

 

%d bloggers like this: