Monday, December 21, 2015

Upload content type template throgh CSOM

A few years ago I bloged, read it there, on how through server site code could upload new content types templates. This snippet of code do it through CSOM.
1 string targetUrl = ""; 2 string username = ""; 3 string password = ""; 4 string contentTypeId = "0x01010043A8F0AB23DEB745AF7BF9D49A69F638"; 5 string templatePath = ""; 6 7 using (ClientContext clientContext = new ClientContext(targetUrl)) 8 { 9 SecureString pwd = new SecureString(); 10 foreach (char c in password.ToCharArray()) pwd.AppendChar(c); 11 12 //clientContext.Credentials = new NetworkCredential(username, pwd); 13 clientContext.Credentials = new SharePointOnlineCredentials(username, pwd); 14 clientContext.RequestTimeout = Timeout.Infinite; 15 16 FileInfo file = new FileInfo(templatePath); 17 18 ContentType contentType = clientContext.Web.ContentTypes.GetById(contentTypeId); 19 Web web = clientContext.Web; 20 21 clientContext.Load(contentType, c => c.Name); 22 clientContext.Load(web, w => w.ServerRelativeUrl); 23 clientContext.ExecuteQuery(); 24 25 string resourcePath = string.Format("{0}_cts/{1}", web.ServerRelativeUrl, contentType.Name); 26 27 using (FileStream stream = file.OpenRead()) 28 { 29 30 FileCreationInformation templateFile = new FileCreationInformation() 31 { 32 ContentStream = stream, 33 Url = file.Name, 34 Overwrite = true 35 }; 36 37 Folder folder = web.GetFolderByServerRelativeUrl(resourcePath); 38 folder.Files.Add(templateFile); 39 contentType.DocumentTemplate = file.Name; 40 contentType.Update(false); 41 clientContext.ExecuteQuery(); 42 } 43 }

Friday, November 6, 2015

Office Word add-ins: Insert template base on Word, Part 3

This is a three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. This is the last post of this three-part blog series on Office Word add-ins: Insert template base on Word is a resources post.


The two first links is for the and where to download the code.
- openxmldeveloper -
- mdownload -

The third link is to a snippet collection with sample for the build in Word add-in

The fourth link is to the MSDN section “JavaScript API for Office”, where everything about the API for Office Add-ins can be found.

Related blog posts

Read the first part here
Read the second part here

Thursday, November 5, 2015

Office Word add-ins: Insert template base on Word, Part 2

This is the second post of this three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. This post describe what framework I use to read whole Word file and interact with the current Word file.

Read the first part here

Add JavaScript frameworks to the Word Add-in

To interact with an Office document, I use the JavaScript API that comes with the Office add-in. But read parts or the whole document, from the API service, I use the Open XML SDK for JavaScript. This API has the ability to create a JavaScript object base on a base64 string and the notion of Word packages. This allows the JavaScript FlatOpc object to insert to the Word file.


OpenXML Developer - Download -

Steps, add OpenXmlSDK, retrieve and insert template:

  1. First we need to download and add the openxmlsdkjs to the project.
  2. Modify the Home.html, head html tag, to include the openxml.js and dependence.
  3. Modify the Home.html, body html tag, to include a button
  4. Modify the Home.js
    - Bind the click event to a function
    - Create the click event function
  5. Fill in the logic of the insertTemplate function
    - Get template (base 64 string) from API service
    - Create a openXml.OpenXmlPackage object
    - Save the OpenXmlPackage to a FlacOpt object
    - Use the build-in Word API to insert the FlacOpt object to the body of the Word file
  6. The test
    - F5 to run the project
    - Click the ‘insert template’ in the Add-in. clip_image012

Related blog posts

Read the first part of the Office Word add-ins: Insert template base on Word here

Wednesday, November 4, 2015

Office Word add-ins: Insert template base on Word, Part 1

This is a three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. The goal of these blog posts is to create an Office add-in that can insert a Word base template into another Word file. By describe how to create a Word add-in that can read a Word file and use the file as a template. This allow an user to choice and insert a prepare template and the templates is easy created in Word.

User case:

  1. An user starts Word with the add-in.
  2. The user presses a button in the add-in.
  3. The add-in call a services for the template.
  4. The add-in insert the template in the current Word file.

Steps, Create the project and setup API services to return a template:

  1. Create a Word add-in project base on the “App for Office”.clip_image002clip_image004clip_image006
  2. Add a WebAPI webservice to the project.
    Add a Web API Controller Class to the project. N.B.! remember to post-fixet the filename with Controller!
    clip_image008Edit the ApiController to return a base64 string.
    This example read a docx file from a sub folder “Documetns” in the website that host the Add-in.
    clip_image010Add a Global.asax to the project to allow ApiController to be start and call.

    Add the folder “Documents” file to the project and create a Word file named template1.docx. And the project structure and Word file shout look something like this.clip_image015

Related blog posts

Friday, September 18, 2015

SPBG ERFA omkring CRM og SharePoint i København

Hej SharePointer
Kom til SPBG erfa møde torsdag den 22. oktober, i København, og mød Kasper Birch Olsen og Svend Erik Laursen. Kasper er Principal Solution Architect og Svend Erik er Principal Architect begge fra ProActive.
Kasper og Svend Erik vil give en kort introduktion til hvad CRM og derefter vil vi vise hvordan man kan integrere CRM med SharePoint og dele deres erfaring fra forskelle kunde cases.
Vi afholder mødet hos Progressive fra kl. 17:00 til ca. 19 tiden. S.U den 18. oktober. Tilmelding sker på

Friday, September 11, 2015

Error when running the prerequisites for SharePoint Server 2016 Preview

When running the prerequisites setup for SharePoint Server 2016 Preview on my Windows Server 2012 (not R2), I got an error “application server role, web server (iis) role: configuration error”.

It seem not to a SharePoint Server 2016 Preview specific error for Christopher ( got a matching error and posted a blog about it. With a PowerShell script, that installs the Windows feature need for SharePoint.

Link to the blog post:

Wednesday, July 22, 2015

Wait for timerjob to finish

I had an old PowerShell latying around. The PowerShell script wait for a given timerjob to be finished.
1 $sleeptime = 5
2 $timerjobname = "MetadataHubTimerJob"
4 $timerjob = Get-SPTimerJob | Where {$_.Name -eq $timerjobname}
5 if($timerjob)
6 {
7 $countBefore = $timerjob.HistoryEntries | select "EndTime"
9 $timerjob.RunNow()
10 "Waiting for timerjob: $($timerjob.Name) to finish"
11 while($true)
12 {
13 Start-Sleep -Seconds $sleeptime
14 $countAfter = $timerjob.HistoryEntries | select "EndTime"
16 if($countAfter.Count -gt $countBefore.Count)
17 {
18 "The timejob $($timerjob.Name) is done"
19 break
20 }
21 else
22 {
23 "Still waiting on timerjob: $($timerjob.Name) to finish..."
24 }
25 }
26 }
27 else
28 {
29 "The timerjob: $($timerjob.Name) was not found."
30 }

Wednesday, July 8, 2015

PowerShell script to restart application pools, with remote Powershell

Has a follow up on my blog post about a PowerShell script to restart application pools. This script do it with remote PowerShell

$computername = "myserver"
$script = {   
    Import-Module WebAdministration
    (Get-Item "IIS:\Sites\*"| Select-Object applicationPool).applicationPool | % { Restart-WebAppPool $_ }
$cred = Get-Credential
$remotesession = New-PSSession -ComputerName $computername -Credential $cred
Invoke-Command -Session $remotesession -ScriptBlock $script

PowerShell script to restart application pools

Two line PowerShell script to restart all application pools on a server

1 #Import WebAdministration
2 Import-Module WebAdministration
3 #Find, pipe and restart
4 (Get-Item "IIS:\Sites\*" | Select-Object applicationPool).applicationPool | % { Restart-WebAppPool $_ }

Tuesday, June 30, 2015

Unable to type in visual studio

After not using an Azure VM in a few weeks, I booted machine and started Visual Studio to get some work done. But I could not type in any documents. It was like all the document was in some kind of read only mode :S After rebooting (several times) I found this stackoverflow post.

In short my resharpers project cache was broken and I need to delete it.

Saturday, June 20, 2015

SharePoint Crawler Troubleshooting using fiddler

A nice blog by Matthew McDermott ( to using fiddler as a proxy to see how SharePoints search crawlers is build its web request and what the web site reposonse back with.

Link to blog post:

Tuesday, May 26, 2015

DevCamp 2015

In danish:

Kom til et gratis heldags arrangement omkring udvikling i Office 365 i København den 16 juni. På dagen gennemgår vi at udvikle apps til Office 365 med standard web teknologier der udvider funktionaliteten af Office og Office 365. (kendskab til SharePoint udvikling ikke nødvendig!). Du kan læse mere om arrangement på hvor du også kan tilmelde dig. Arrangement bliver afhold hos Nets, Lautrupbjerg 10, 2750 Ballerup og starter kl 9:00 og slutter omkring 17:00.

Læse mere på

Tuesday, April 28, 2015

Register to attend the Microsoft MVP Virtual Conference

Hi All – I wanted to let you know about a great free event that Microsoft and the MVPs are putting on, May 14th & 15th.  Join Microsoft MVPs from the Americas’ region as they share their knowledge and real-world expertise during a free event, the MVP Virtual Conference.

The MVP Virtual Conference will showcase 95 sessions of content for IT Pros, Developers and Consumer experts designed to help you navigate life in a mobile-first, cloud-first world.  Microsoft’s Corporate Vice President of Developer Platform, Steve Guggenheimer, will be on hand to deliver the opening Key Note Address.

Why attend MVP V-Conf? The conference will have 5 tracks, IT Pro English, Dev English, Consumer English, Portuguese mixed sessions & Spanish mixed sessions, there is something for everyone!

Come learn from the best and brightest in the tech world today. All of the sessions will all be delivered by the Americas’ Region Microsoft MVPs. These MVPs are experts who present at premiere conferences, independent community events and local user groups all over the globe.

This is a technical conference focused on helping attendees to learn and develop skills for everything from everyday technical work to wackier weekend projects. Whether it is on the IT Pro, Dev or Consumer side of things, you can bet that the content of MVP V-Conf will be cutting edge, exciting and relevant.

Be sure to register quickly to hold your spot and tell your friends & colleagues.

The conference will be widely covered on social media, you can join the conversation by following @MVPAward and using the hashtag #MVPvConf.

Tuesday, March 31, 2015

Sunday, March 8, 2015

SharePoint dinner i Århus

I forbindelse med Office Camp, i Århus, bliver der også afholdt SPBG SharePoint dinner.

Selv om du ikke deltager til Office Camp, er du mere end velkommen til at deltage til Sharepoint dinner.

SharePoint dinner er den 27/3-2015 kl 18 på The Burger Joint (

Hvad SharePoint dinner går ud på, kan du læse om det her:(

Obs! Tilmelding sker ved på og seneste den mandag den 23. marts.

Thursday, March 5, 2015

A C# thing in PowerShell. String.Format

A PowerShell tips. A feature that I often use in C # is string.Format This function can also use the PowerShell. This can be done via [string]::Format("template {0}", $varb1) or you can call -f parameter after a string in PowerShell, for example. "template {0}" -f $ varb1

Wednesday, February 25, 2015

Call Exchange Web servies API through PowerShell

I had to test whether a given user has a given meeting requests in a calendar in Exchange. Therefore, I wrote this little piece of PowerShell script, that’s using Microsoft Exchange Web Services Managed API, to find the Exchange server based on user's email address. Then make a call to the user's calendar, where I could check if there was create meeting request.

Step 1. Download and execute the the Microsoft Exchange Web Services Managed API 2.0

Step 2. Change the PowerShell script
Change this variable:

Step 3. Run the PowerShell script

1 $exchangeversion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010
2 $pathToExchangeDll = "c:\ExchangeWebserviceApi\Microsoft.Exchange.WebServices.dll"
3 $folderName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar
4 $usersmtpadress = ""
5 $userlogin = "userlogind"
6 $userdomain = "domain"
7 $userpassword = "P@ssword!"
9 #callback function
10 function AutodiscoverCallback
11 {
12 param (
13 [System.String]
14 $Url
15 )
16 return $true
17 }
18 $callback = ${function:AutodiscoverCallback}
19 $service = new-object "Microsoft.Exchange.WebServices.Data.ExchangeService" $exchangeversion
20 $principle = new-object "Microsoft.Exchange.WebServices.Data.Mailbox" $usersmtpadress
21 $folder = new-object "Microsoft.Exchange.WebServices.Data.FolderId" $folderName, $principle
22 $connectingIdType = [Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress
23 [system.reflection.assembly]::loadfile($pathToExchangeDll) | Out-Null #e.g. c:\ExchangeWebserviceApi\Microsoft.Exchange.WebServices.dll
25 #setup call for Exchange web services
26 $service.Credentials = new-object "System.Net.NetworkCredential" $userlogin, $userpassword, $userdomain
27 $impersonatedUserId = new-object "Microsoft.Exchange.WebServices.Data.ImpersonatedUserId" $connectingIdType, $usersmtpadress
28 $service.ImpersonatedUserId = $impersonatedUserId
29 $service.AutodiscoverUrl($usersmtpadress, $callback)
31 #calling Exchange web services
32 $principleCalendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folder)
33 "User {0}'s calendar" -f $userlogin
34 $principleCalendar

Friday, February 20, 2015

Call for Speakers

Vores venner fra der danske SQL Community, er i gang med at planlægge den tredje SQL Saturday i København.
En SQL Saturday er et heldags event med indlæg omkring SQL server, SQL rapporting og SQL analysis.
Arrangøren af den kommende SQL saturday, ville rigtig gerne have et SharePoint spor denne gang. Så hvis du har noget SharePoint på hjertet, og selv om det ikke har noget med SQL at gøre og har lyst til at tale, så vil jeg gerne høre fra dig.
Du kan læse mere om SQL saturday på deres hjemmeside Eventet bliver afholdt den 19/9/2015 på ITU.