Install First Approach Explained

I have worked with SharePoint since 2005 and I have seen quite many ways of building SharePoint solutions.

Now when more and more companies are using cloud based services it’s time to make small change to development also. If you have followed at least a little bit of SharePoint scene during last few years, you have heard Microsoft talking about SharePoint Online and Apps model (currently Add-Ins, perhaps they will change that again to something new). At first it was how to build Apps with User Interface and how all developers should start building AppParts instead of WebParts. It has been now little less that three years since Richard diZerega posted an article about an app that will create new site collections to SharePoint (http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2013/04/04/self-service-site-provisioning-using-apps-for-sharepoint-2013.aspx) using Client Side Object Model (CSOM). Now we can benefit from the work done by pioneers. OfficeDevPnp and OfficeDevPnp-PowerShell provide us excellent base to build on. Thank you for all who has made this possible.

Creating the first project

We will create two projects (PowerShell and Web Application) inside one solution. The idea behind this is

To be able to deploy from the first second of the project.
To be able to configure each environment identical.

Why this is important? It’s important because we are building application in development environment. Then we need to deploy same code and configurations to test environment and after acceptance test we will deploy everything to production environment. We can (re)create out development or test environment whenever we want with one script even development environment would be our O365 development tenancy.

Start Visual Studio and select PowerShell | PowerShell Script Project.
Name: MyProject.SharePointApp1.Scripts
Solution name: MyProject.SharePointApp1
Delete Script1.ps1 file.

Add New ASP.Net Web Application to the Solution.
Name: MyProject.SharePointApp1.Web
Select Empty Template
Clear selection from Host in the cloud.
After you have created a project add new folder called Content.

After you have completed all tasks your solution should be similar to this.

Add New PowerShell Script Item ‘CreateSiteCollection.ps1’ to Scripts project. You can see list of time zone id’s and web templates from my previous posts. Execute script. It will create default team site that we can use as playground for next projects.

#
# CreateSiteCollection.ps1
#
Connect-SPOnline -Url “https://tenant-admin.sharepoint.com” -UseWebLogin
New-SPOTenantSite -Title App1 -Url https://tenant.sharepoint.com/sites/app1 -Owner “admin@tenant.onmicrosoft.com” -TimeZone 59 -Lcid 1053 -Template “STS#1” -RemoveDeletedSite

Now when we can create site, we also need to delete sites when we want to erase all old configurations. Therefore, we need to create new PowerShell Script Item ‘DeleteSiteCollection.ps1’.

#
# DeleteSiteCollection.ps1
#
Connect-SPOnline -Url “https://tenant-admin.sharepoint.com” -UseWebLogin
Remove-SPOTenantSite -Url https://tenant.sharepoint.com/sites/app1 -SkipRecycleBin

Adding jQuery

We will continue working with our App1 project by adding jQuery NuGet package to Web project.

  1. Selecting Web project from Solution Explorer
  2. Select Manage NuGet Packages from context menu.
  3. Change tab to Browse
  4. Use search to find jQuery and install it.

Creating the first script

Now we have added jQuery to our project. It’s time for simple hello world demo. Create new JavaScript file ‘project.js’ to Scripts folder inside Web project.

jQuery(document).ready(function () {
alert(“Hello World!”);
});

Updating files to SharePoint

After we have installed jQuery and created project.js file we need to create new PowerShell Script ‘UpdateFiles.ps1’. We will upload jQuery with our own script to the site.

#
# UpdateFiles.ps1
#
Connect-SPOnline -Url “https://tenant.sharepoint.com/sites/app1” -UseWebLogin
Add-SPOFolder -Folder “SiteAssets” -Name “Scripts” -ErrorAction:SilentlyContinue
Add-SPOFolder -Folder “SiteAssets/Scripts” -Name “jQuery” -ErrorAction:SilentlyContinue
Add-SPOFile -Path “C:\SourceCode\MyProject.SharePointApp1\MyProject.SharePointApp1.Web\Scripts\jquery-2.2.0.min.js” -Folder “SiteAssets/Scripts/jQuery”
Add-SPOFile -Path “C:\SourceCode\MyProject.SharePointApp1\MyProject.SharePointApp1.Web\Scripts\Project.js” -Folder “SiteAssets/Scripts”

Files have been uploaded there successfully as we can see from the following image, but the problem is that we didn’t get the message. We still need to add those scripts to the page so that we can execute them in user’s browser.

Adding scripts to the site

Adding custom scripts to site can be done using Add-SPOCustomAction. The problem is that you can’t define script source even it has parameter called Url. At first we need to create custom action and then we need to update ScriptSrc property. Now you can see “Hello World!” text on you site.

#
# ActivateScripts.ps1
#
Connect-SPOnline -Url “https://tenant.sharepoint.com/sites/app1” -UseWebLogin
Add-SPOCustomAction -Description “jQuery” -Location “ScriptLink” -Name “jQuery” -Title “jQuery” -Group “Project” -Sequence 10050 -Scope “Site”
$action = Get-SPOCustomAction -Scope “Site” | ? { $_.Description -eq “jQuery” }
$action.ScriptSrc = “~sitecollection/SiteAssets/Scripts/jQuery/jquery-2.2.0.min.js”
$action.Update()
Execute-SPOQuery

Add-SPOCustomAction -Description “ProjectScript” -Location “ScriptLink” -Name “ProjectScript” -Title “ProjectScript” -Group “Project” -Sequence 10051 -Scope “Site”
$action = Get-SPOCustomAction -Scope “Site” | ? { $_.Description -eq “ProjectScript” }
$action.ScriptSrc = “~sitecollection/SiteAssets/Scripts/Project.js”
$action.Update()
Execute-SPOQuery

Now we have simple template that allows us to update scripts and sites with scripts. If we need to set up new environment, all we need to do is to execute those scripts and environment is ready for action.

Leave a Reply

Your email address will not be published. Required fields are marked *