Office 365 send email using JavaScript (REST)

I have a fairly simple requirement for some JavaScript (jQuery) to send an email.   This is within the code for a SharePoint Online page (Office 365).

My initial thinking was to do a HTTP POST request to the Office 365 REST endpoint, but nothing could have prepared me for the next few hours of my day.

I had to jump through a few hoops, and perform backflips while juggling – and THEN changed tack entirely.   I thought I’d share my observations – and some sample code/link.

This was my starting point (MSDN) :    Send a new message on the fly (REST)

I wrangled with it for a few hours :

  • SharePoint hosted code (JS), calling to https://outlook.office365.com was resulting in a 401 – due to a X-Domain call.
  • HTTP POST with JSONP doesn’t work – only for GET
  • CORS is what was messing it up – 401’ing the request
  • Even trialling with the new http://graph.microsoft.com – same problem
  • Suggestions via Twitter were to do an Azure AD app – TO SEND AN EMAIL !!
  • I even found one ridiculous solution that was to create workflow from Visual Studio or SharePoint Designer – and then CALL it from CSOM.    #WTF !!

A colleague mentioned “surely SharePoint has something to send an email” – it could then process the email traffic SERVER SIDE.   

That was an unholy yee-haar gold nugget of joy.

Posting from my jQuery back to SharePoint Online is a simple REST call.   And, it does NOT suffer from X-Domain or CORS.    

http://tenant/_api/SP.Utilities.Utility.SendEmail

There’s a blog post that I found, with a great code sample – and it’s now working for me :

Sending email with SharePoint and jQuery

Just note that the recipient is limited to a valid SharePoint user for security reasons.    (within the same tenant)

So – it’s the simply “KISS” principle – keep it simple, stupid.   

😉

Add to Office365 PropertyBag (PowerShell)

Been fiddling with some script to ADD a value to the PropertyBag for a SharePoint site within Office365.

There’s a great post (and script) on the Microsoft site – for READING all properties :

How to read all property bags in a SharePoint Online Site Collection

But, for adding a new key, I had to fiddle & find an answer.

This post also – mentions the issue with “persisting” values :

Add and Retrieve property bag by CSOM (thanks !!)

NB.   This code (PowerShell) will only ADD a key/value – if not already in place.    (You’ll need to change slightly – to UPDATE the value)

        $rootWeb = $spoCtx.Web
        $spoCtx.Load($rootWeb);
        $spoCtx.ExecuteQuery();

        $allProps = $rootWeb.AllProperties;
        $spoCtx.Load($allProps);
        $spoCtx.ExecuteQuery();

        # DOESN’T WORK !!
        # $allProps.FieldValues.Add(‘KEY’, $value);

        $rootWeb.AllProperties[‘KEY’] = $value;
        $rootWeb.Update();


        $spoCtx.ExecuteQuery();

That’s not TOO tricky in the end – just needed some fiddley syntax to make it work – especially the POWERSHELL bit…

Hope that helps you – it was a battle for me.   

🙂

Office 365 SharePoint Site Templates

When creating a new site collection within O365, you can choose the default (RootWeb) template to use.  

Most of these are the same as SharePoint2013 – but there are a few slight differences.

image

The easiest way to check the site template ‘name’ for use in PowerShell is actually to use the F12 developer tools – and view the HTML – neat trick !

image

For reference, I’ve made a list of all of them below.

Collaboration

  • STS#0 – Team Site
  • BLOG#0 – Blog
  • DEV#0 – Developer Site
  • PROJECTSITE#0 – Project Site
  • COMMUNITY#0 – Community Site

Enterprise

  • BDR#0 – Document Center
  • EDISC#0 – eDiscovery Center
  • OFFILE#1 – Records Center
  • EHS#1 – Team Site – SharePoint Online configuration
  • BICenterSite#0 – Business Intelligence Center
  • POLICYCTR#0 – Compliance Policy Center
  • SRCHCEN#0 – Enterprise Search Center
  • SPSMSITEHOST#0 – My Site Host
  • COMMUNITYPORTAL#0 – Community Portal
  • SRCHCENTERLITE#0 – Basic Search Center
  • visprus#0 – Visio Process Repository

Publishing

  • BLANKINTERNETCONTAINER#0 – Publishing Portal
  • ENTERWIKI#0 – Enterprise Wiki

Hopefully that helps someone out there – it certainly made sense for me – as it was missing the CMSPUBLISHING#0 which is the normal SP2013 “Publishing” site.

🙂

SQL Azure, error when editing stored procs

I have a SQL Server database within my Azure tenant, but I’ve been unable to edit any Stored Procedures – or view the list of procs.

The message I get is “unknown property IsNativelyCompiled”.

image

There are procs in there – I can execute then, or see view via SP_HELPTEXT – but not within Enterprise Manager (Management Studio).

This is within SQL Server 2014 (Management Studio – just the client tools on my Win8.1 PC)

image

It turns out there’s a SP1 for SQL2014 – which also updates the client tools.

Refer to this blog, for the various SQL build versions.

Unfortunately, the SP1 has been pulled – doh.    (At time of writing).   

For now, I’ve installed SQL 2014 CU6 – which is 640MB !

I can now view the list of Stored Procedures – and edit/update – phew !

Simple AngularJS – getting started

With the buzz around AngularJS, I’ve kinda missed the boat on some of it – and getting started ‘late’.

Then again, I’m a ‘fashionably late’ kinda person – and that’s A-OK with me !

🙂

In order to get started, I thought I’d share some observations and ideas.

YES – it’s a big topic – and this first post is a ‘hello world’ style mini-app.

To get it working, just save the content to a separate file – one for the HTML and one for the JavaScript (app.js).

(1) The HTML

The following HTML is as basic as you can get – explanation below.

<!DOCTYPE html>
<html ng-app=”testApp”>
<body ng-controller=”testCtrl”>
/spana%20href=
http://app.js
<h3>{{ greeting }}</h3>
</body>
</html>

The main pieces are highlighted :

  • testApp is the name of the ‘program’ like and is a container holder, for the whole page
  • testCtrl is the name of the ‘controller’ which is the portion of the app, and used for the model/controller execution.

You’ll note that these have special Angular attributes >  ng-app and ng-controller.

There are also a few JavaScript references – for the AngularJS code (include) – and our code (app.js).

(2) The JavaScript

Again, to keep it simple, this is pretty much the only bits you need.

var app = angular.module(‘testApp‘, []);

app.controller(‘testCtrl‘, [‘$scope‘, function ($scope) {
$scope.greeting = “Hello friends !”;
}]);

This does the following logic :

  • Get a reference to the ‘program’ – ie. testApp – into a variable called ‘app’.
  • Use the ‘app’ variable and define the ‘controller’ – ie. testCtrl
  • Define a function to set the property (element) of ‘greeting’.

The key piece here is the thing called “$scope”.   This is a variable passed into the function – and thus you can do >  $scope.messageText.

This is because the value in HTML has {{braces}} around it.

Hope that’s enough to get a quick 101 jump start – the journey of a thousand miles begins with a single step…!

=============

Here’s a cool sample that builds upon the above – with more fields :

http://blogs.msmvps.com/deborahk/angularjs-101-ways-to-create-a-controller-actually-8/

SQL Azure Admin Password

When creating a brand-new SQL Azure Database, I was unsure on how to connect to it.

There is a button to “Show connection strings” – but that shows as :

image

Thee is the ability to open within Visual Studio (bottom row of buttons) – but it would then ask me to provide a password – but HOW/WHAT is it !!??!

image

It turns out that there is an easy way to do this.

If you consider that each DATABASE will live on a specific SERVER, you need to set the Administrator password for the ‘SERVER’ itself.

  • Click on the link for the ‘server’

image

  • Flip to the ‘Dashboard’ tab
  • There’s a link to ‘Reset Admin Password’

image

Obviously – you don’t want to do this for EVERY database – but if you’re doing it for the first time, then you need to SET the password.

P.S.  The default login account is :   AzureCloudAdmin

Too easy – in the end !

Invalid issuer or signature

I’ve been deploying a SharePoint app to a new farm (actually, it’s a different O365 tenant), and so I had to generate a new CLIENT ID and SECRET.

I’d updated this within the WEB.CONFIG of my provider hosted app – ASP.NET MVC running on Azure – and then published to my Azure WebSite.

BUT – I’m getting this error when using the app from SharePoint :

SecurityTokenException: Invalid issuer or signature.
image 

It turns out that I’d forgotten to update the properies within Azure.   

Those settings in the WEB.CONFIG are for a localhost deployment – you need to use the “CONFIGURE” page for Azure.

image

Scroll down – and check that you’ve correctly set the (NEW) client id and secret.

image

Make sure to click “SAVE” – and then you should be A-OK to simply re-try the SharePoint app.

It would be nice if the exception message gave more details !     “check the ClientID and Secret” !   

🙂

Hopefully that helps someone !

The (very important) APP DOMAIN

SharePoint error : An error occurred while processing your request

I’ve just trying to get a Provider Hosted sample working – with a AZURE website – and an Office 365 App.   This has been an “all day” thing – not working !

I’d done all the steps – with the ClientId – and ClientSecret – and included in my WEB.CONFIG.

But – when accessing my App, I’d simply see a message :

An error occurred while processing your request

This is coming from the following code – within the ‘default.aspx’ for the website.

protected void Page_PreInit(object sender, EventArgs e)
{
    Uri redirectUrl;
    switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl))
    {
        case RedirectionStatus.Ok:
            return;
        case RedirectionStatus.ShouldRedirect:
            Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
            break;
        case RedirectionStatus.CanNotRedirect:
            Response.Write(“An error occurred while processing your request.“);
            Response.End();
            break;
    }
}

So – it was LOADING my site, but couldn’t display.

It turns out, that I’d been ‘faster-stepping’ through the wizard to assign the “CLIENT ID” and “CLIENT SECRET”.

If/when you’re using APPREGNEW.ASPX – make sure to put the correct Azure WebSite domain

As below – the APP DOMAIN is important – don’t just put ‘contoso’ or ‘localhost’.

image

It turns out, I’d been “super careful” on the CLIENT ID and CLIENT SECRET – but not paying much attention to the App-Domain.

Hope this helps – it woulda saved me a bunch-of-hours…!

SharePoint Search Results with DateTime field refiner

When configuring a SEARCH RESULTS page, you can include any of your own custom columns as ‘refiners’.   These show on the side of the results, allowing users to filter down the results.

When you choose a field as a refiner, the data shown will depend on the type of the field.      Eg.  TEXT fields, will just show as the item values

image

When using a DATE field – for some reason, there’s a nasty symptom that doesn’t show the values correctly !

This symptom was also highlighted and blogged by Simon Doy.

WHY ?    The field is showing as 2,012 and 2,013 ??

My refiner is doing the same thing – compared to the Out Of The Box Created field :

image

RESOLUTION

I’m happy to report that there’s a very simple fix – and it’s related to the NAME of the managed property you create.

In my example :

  • DON’T name the property as “STaRCreatedDateTime”
  • Instead – it should be “STaRCreatedDateTimeOWSDATE

There’s some SharePoint JavaScript code that looks for the ‘type’ of the field – within the tail of the field name.    Sure enough – it works !

image

Thanks to SIMON for the tips and investigation – his blog goes into a lot of depth, with a custom refiner to change the look/feel.

It was this tip that saved the day :

image

Thanks Eman !      (Credit where credit is due)

🙂

Show status image in SharePoint column

Recently, I was discussing a clever trick with a colleague, for showing HTML in a SharePoint column.  

For SP2003 and SP2007, you needed to include some additional JavaScript (TEXT2HTML / examples) – but with SharePoint 2010 + 2013, you can do it using a calculated column.

Source column : Priority

Firstly, we need to add a column that we’ll use at the ‘checker’ column – this might be something like a STATUS column, or % COMPLETE, or PRIORITY.

  • Create Column : Priority
  • Type : Number

image

Add some List Data

Just add a few items to the list – and pick some priorities.

image

You can do whatever VIEWS, or SORTING or GROUPING – whatever…!

Find some images

Use your favourite search engine to look up some status images – I found a cool set here (more instructions here) :

image

Save these images locally – and upload to your SharePoint site – eg. SiteAssets, or Style Library.  I’ve loaded mine into an “IMG” folder.

Eg.   /Style Library/IMG/red.png

Calculated column

We can now do the magic + fun bit.

  • Create a calculated column : Indicator
  • Type : Calculated
  • Formula :

=”<DIV><img src='” & IF(Priority=1, “/Style Library/IMG/red.png”, IF(Priority=2,”/Style Library/IMG/yellow.png”, IF(Priority=3,”/Style Library/IMG/green.png”, IF(Priority=4,”/Style Library/IMG/blue.png”)))) & “‘></DIV>”

This is essentially doing the following :

  • If Status = 1, put an IMG tag for RED
  • If Status = 2, put an IMG tag for YELLOW
  • etc

NB.  The way that the formula works is like EXCEL :

  • IF (condition, TRUE, FALSE)

This method (above) is nesting these :

  • IF (condition1, IMAGE, IF (condition2, IMAGE, IF (condition3, IMAGE, IMAGE ) ) )

Click back to your list to view your column :

image

What the !!?!    It’s just showing HTML !!

😦

The UberMagic Tip

You’d be thinking that this is a “dud” tip – and you’ll need some “TEXT2HTML” JavaScript and so on – which would be annoying.

But no – the amazingly simple tip is this :

  • Change the return type to be a NUMBER.

Yep.   If defies logic – and doesn’t make sense – might even be a BUG !

But – it works.

image

Take a bow

If you’ve done the steps correctly, it should look like this :

image

Too easy – right !!?!

** You might need to include the ‘site collection’ prefix within the formula – pro tip !

=”<DIV><img src='” & IF(Priority=1, “/sites/SC1/Style Library/IMG/red.png”, IF(Priority=2,”/sites/SC1/Style Library/IMG/yellow.png”, IF(Priority=3,”/sites/SC1/Style Library/IMG/green.png”, IF(Priority=4,”/sites/SC1/Style Library/IMG/blue.png”)))) & “‘></DIV>”

Next ideas

Check this blog for some other renditions – very cool !

http://sharepoint.alanchai.com/2012/05/conditional-formatting-for-list-sp2010.html

image

image

Let me know if you like this tip – my buddy DanCol was #MindBlown over this one !

🙂