Content Organizer Rule (CSOM/O365)

Our client is using Office365, and we’re provisioning new sites using an Azure WebJob, which will do the necessary configuration.

One aspect they want is to do the “Content Organizer” rule.   This will mean people can upload to a document library (DropOff Lib), and it will be re-routed.  

This is for school students to submit assignment work.

After looking at CSOM code, and searching on Google for stuff like “EcmDocumentRouterRule”, I had a brain wave to just do it SIMPLE – for my fairly simple rule.

The Content Organizer from SiteSettings is just a SharePoint list – and ListItems are added.  So – looking at the REST API – there are simply a bunch of properties ;


So – it follows that I simply need to create a listitem – and it just works !


** NB.  PRIOR TO THIS, need to make sure you have turned ON the feature :

// Load the features
FeatureCollection webFeatures = clientCtx.Web.Features;

//turn on site feature for Content Organizer, will create drop off library
webFeatures.Add(new Guid(“7ad5272a-2694-4349-953e-ea5ef290e97c”), false, FeatureDefinitionScope.None);

Here’s the code, ready to copy+paste :

//only way to do within CSOM is to add the necessary listitem to “ROUTINGRULES” list  
List routingRulesList = clientCtx.Web.Lists.GetByTitle(“Content Organizer Rules”);

//get the current url, to use when defining target for rule
var web = clientCtx.Web;

var currentUrl = web.Url;
string targetLibraryUrl = currentUrl + “/SubmittedWork”;

ListItemCreationInformation routingRuleInfo = new ListItemCreationInformation();
ListItem routingRule = routingRulesList.AddItem(routingRuleInfo);
routingRule[“Title”] = “Move to Submitted library”;
routingRule[“RoutingRuleName”] = “Move to Submitted library”;
routingRule[“RoutingPriority”] = 1;
routingRule[“RoutingEnabled”] = true;
routingRule[“RoutingContentType”] = “Document”;
routingRule[“RoutingConditionProperties”] = “Content Type”;
routingRule[“RoutingTargetLibrary”] = “Submitted Work”;
routingRule[“RoutingTargetPath”] = targetLibraryUrl;
routingRule[“RoutingRuleExternal”] = false;
routingRule[“RoutingAliases”] = null;
routingRule[“RoutingRuleDescription”] = null;
routingRule[“RoutingTargetFolder”] = null;
routingRule[“RoutingAutoFolderProp”] = null;
routingRule[“RoutingCustomRouter”] = null;


Hopefully that helps with your Office365 configuration – good luck !



SharePoint page showing BLANK

I have an Office 365 site collection that I’ve been provisioning from within PowerShell – and automatically uploading webparts.   I’m getting some weird behaviour though :


When looking at VIEW SOURCE – it’s even stranger – that’s the ENTIRE page !


Even when looking at the page request/s via FIDDLER – nothing seemed off.   And nothing in the console or error log in the F12 developer tools.

I could view the page with the ?contents=1 appended to the URL – but still no answer.


I worked out that it’s actually a SEARCH webpart that is causing the issue – but I couldn’t work out WHY…

This was my PowerShell code to upload the webpart to the page – some other O365 guff around this – but this is the basic code (using CSOM) :

$wpm = $pubFile.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared);       
$wpcoll = $wpm.WebParts

$webPartFileContents = [System.IO.File]::ReadAllText($filepath);
$wpd = $wpm.ImportWebPart($webPartFileContents);

$wpdNew = $wpm.AddWebPart($wpd.WebPart, $xmlwp.ZoneID, $webPartCount);

Within the .WEBPART file, there’s the usual XML bits & pieces – and a bunch of properties.


After lots of blog post hunting – off/on for a few days actually – and re-building the WebPart contents piece-by-piece, I noticed it was breaking on properties like this >

<property name=”RenderTemplateId” type=”string”>~sitecollection/_catalogs/masterpage/Display Templates/Search/Control_SearchResults.js</property>

It was THIS post from Chris O’Brien that made me hunt down this path :

..but then the page does not load (blank screen, HTML not output properly) and ULS shows the following runtime error:

Application error when access /SitePages/CSWP_Provisioned.aspx, Error=Cannot make a cache safe URL for “item_picture3lines_cob_provisioned.js”, file not found. Please verify that the file exists under the layouts directory.
at Microsoft.SharePoint.Utilities.SPUtility.MakeBrowserCacheSafeLayoutsUrl(String name, Boolean localizable, Int32 desiredVersion)   

I couldn’t check the log files – with Office 365 – but I think it’s the same issue.

My next thing to try was to change the markup within my .WEBPART – and I had success !


Changed to :


Just needed to change from ~ to the equivalent in encoding ~

I’m unsure exactly WHY this is occurring – perhaps the PowerShell upload is losing this and/or encoding it wrong – but it worked !

Hopefully that helps someone – or at least, this post is a reference for ME, if it ever happens AGAIN !

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 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 – 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.    


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

        $allProps = $rootWeb.AllProperties;

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

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


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.   


Yammer from C# – create a group

There’s a surprisingly woeful lack of doco and information about using the API for Yammer – from C# especially – there’s more for the JavaScript way, but not for .NET.

The one I need – to create a Yammer Group – is this one :{0}&private={1}

The best approach is to simply call the REST API – using the .NET Web Client classes (HttpWebRequest).

It follows that you need to do a POST to this URL – the clincher is to define an ‘accessToken’ to do so.   This is like a SharePoint 2013 style ClientId/ClientSecret.

Click here to register your app, and get the token – it is then registered against YOUR Yammer network.   


You’ll get a CLIENTID and CLIENTSECRET – need to plug these into the following URL >[:client_id]&redirect_uri=[:redirect_uri]

And then you’ll get a CODE value.    THEN, need to call the following >[:client_id]&client_secret=[:client_secret]&code=[:code]

And you’ll get a TOKEN – which is the magic bean you need for the following C# code.

Here’s some doco about it >

Then – you can just call the following method within C# :

private static string PostYammerJson(string url, string accessToken)
    //make the request
    string json = null;
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = “POST”;
    request.Headers.Add(“Authorization”, “Bearer” + ” ” + accessToken);
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        Encoding encode = Encoding.GetEncoding(“utf-8”);
        StreamReader reader = new StreamReader(response.GetResponseStream(), encode);
        json = reader.ReadToEnd();
    return json;

Within the C# code – I can then do this :

string postUrl = “”;

//replace with what you get from Yammer register App page

string accessToken = “1234567890qwertyuiop”;     

PostYammerJson(postUrl, accessToken);

This is a GREAT pattern – thanks to the folk at the Microsoft PnP on GitHub.

There’s a whole solution/project that does this – and a LOT more – great examples :


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.


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 !


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


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


  • 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


  • 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”.


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)


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”>
<h3>{{ greeting }}</h3>

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 :

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 :


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 !!??!


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’


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


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.

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.


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


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 !