Saturday 2 March 2019

Transpile ES6 (ES2015) to ES5 code using GULP.

In this blog, I will be sharing how to transpile ES6 (ES2015) to ES5 code using GULP.

Steps to transpile ES6 (ES2015) to ES5 code using GULP.

Babel is the go-to transpiler for ES6.
Download and install Node.JS on your system from https://nodejs.org.

1. Create a project folder and navigate there in the terminal (cmd.exe).

2. To start, we need to generate a package.json file, use


  npm init


This will ask for a few properties: What is the name of your project, version, description, and entry point (it sets the main file of your project)? Leave it as index.js

After all options are selected, a "package.json" file will be created with all properties. The package.json file can be edited to update the property’s value set above.



3. Now install the dependencies for the Babel package.
Babel
We need the Babel core, CLI, and ES2015 presets..

Babel-CLI: used to compile files from the command line.
Babe-Core: is a library that gives the ability to use the new functionalities of ES6 for ES5
ES2015 preset: ensures the ability to transpile ES6


  npm install --save-dev babel-cli babel-core babel-preset-es2015


After running the above command, you will see a few new changes in your project directory. First, you will notice that there is now a "node_modules" folder, and second, there is a new entry inside your "package.json" file.

The --save-dev flag will save the modules babel-cli, babel-core, and babel-preset-es2015 in your package.json file under the devDependencies section. This section is used for modules that will be used in development, meaning they are not needed in production. If we had run our install with --save instead, it would have put the module under the dependencies section.




4. Project structure


  |-- dist // Build will end up here
  |-- src // All source code should be inside this folder
      |-- index.js
  |-- gulpfile.js
  |-- package.json


5. Configuration
Babel
Configure Babel to use the ES2015 plugin in your package.json (or .babelrc if you prefer).


  "babel": {
      "presets": ["es2015"]
   }


The first method of transpiling we will look at is using NPM to run Babel. In the "package.json" file, there is a section called "scripts," where we can define commands we would like to run. Using the Babel CLI, we can set up a script to compile our code and output it where we would like. The scripts section takes a JSON object as a value. The keys in this object will be the name of our script, and the value will be the command to run.


  "scripts": {
      "buildjs": "babel ./src/index.js --out-file ./dist/index.js"
   }


Adding the above to our package.json in the scripts section will take code from src/index.js, run it through Babel, and output the file in dist/index.js. The Babel command has many options that we will look at later, but the --out-file flag is used to determine the destination of the compiled source.
Add some code to the src/index.js file and type npm run buildjs into your terminal so you get the output in dist/index.js.

Now, when we run the script, it will output the file and stop. If you want to keep working on the file, we need to run this repeatedly. We can configure the script to watch over the files and run when they change!


  "scripts": {
      "buildjs": "babel ./src/index.js --watch --out-file ./dist/index.js"
  }


By adding the --watch flag, we are telling Babel to listen for any changes in the "src/app.js" file, and when changes are made, we want to output a transpiled version of that code to "dist/app.js".



6. Gulp is an NPM module and a task runner that helps to automate. We need to install Gulp globally on the system, so we install it.


  npm install -g gulp


Now install the dependencies for the gulp package.
We need gulp and the Babel module for gulp.


  npm install --save-dev gulp gulp-babel


After running the above, you will see a few new changes again in your project directory in the "node_modules" folder, and second, that there is a new entry inside your "package.json" file.



7. Configuration
Gulp
Create a new file called "gulpfile.js" and require the two gulp libs:


  const gulp = require('gulp');
  const babel = require('gulp-babel');


The require function will pull in any modules from the ‘node_modules’ folder. The code in gulpfile.js can be written in ES6 or ES5 both will work.

Gulp is all about tasks. We will start by defining some simple tasks in this file.


I have created the task "default". You can provide any task name, with the exception of default. This task JS will pick up the JS files from /src folder, compile them with Babel, and save everything in /dist folder.

To run this default task

The ES6 code will be compiled into JavaScript. Here, the command ran only once, and for every change, the command needs to be executed every time. 

Let us set up the ability for Gulp to keep watching our files. The gulp.watch method takes a few arguments: a file path that waits for file changes and an array of the task(s) you want to run.


To run task


This will listen for all the file changes and compile them to ES5 code.


🚀 "Happy Coding" 🚀

Friday 18 January 2019

SPFx Environment Setup Error: rollbackFailedOptional: verb npm-session.

In this blog, I will be sharing how I have troubleshooted and found the solution to one of the errors I faced while setting up the development environment for SharePoint Framework while following the official documentation provided by Microsoft.

Faced issues while running the command shown below.


The execution got struck and freezes at this "rollbackFailedOptional" and after about ~5 minutes it gets completed with the below error messages:


After analyzing the error and log files, it appears that the error is related to network connectivity or some proxy problem. But I am not using any proxy. I'm on a corporate network. When I checked for any type of proxy, there was nothing.

I also checked the internet settings in the IE browser under Internet Settings >> Settings >> LAN Settings, and it also doesn't show any proxy settings. So, I believe I am not behind any proxy server.


I checked the internet but didn’t get any solutions, so I opened an issue on GitHub and in the NPM community.
https://github.com/SharePoint/sp-dev-docs/issues/3266
https://npm.community/t/sharepoint-spfx-npm-install-error-using-node-8-15-0/4645



The default value of the "proxy" and "https-proxy" keys of the NPM config is NULL. After doing some more R & D, I tried to set the proxy setting value to the above key (this solution works in my scenario).

npm config set proxy http://n****500:8080
npm config set https-proxy http://n****500:8080




After successful execution of the above command, a .nmprc file will be created at C:\Users\username. The file created will be hidden, so make sure to check the "Hidden items" checkbox in the ribbon.



Also, the same proxy settings need to be applied as shown below.

Conclusion: In corporate or some environments, the proxy is a somewhat mandatory setting; otherwise, NPM install will not work.



🚀 "Happy Coding" 🚀

Tuesday 20 March 2018

Enable Anonymous access to search results.

I am working on one task to display list items using a display template to anonymous users. I have also made the managed search property safe for the anonymous user, but it is still not working for the anonymous user.

Out-of-the-box, the search results webpart (via display template) will try to display data to users (anonymous and authenticated users); authenticated users will see results, while anonymous users will see no results.

Here I will use the PowerShell script to allow list-level anonymous access.



The commented AnonymousPermMask property, according to MSDN, is deprecated and you need to use AnonymousPermMask64.



Instead of ViewFormPages, we need to use AnonymousSearchAccessList, which is described as "Make content of a list or document library retrieveable for anonymous users through SharePoint search. The list permissions in the site do not change".

Then run a full search crawl.


🚀 "Happy Coding" 🚀

Sunday 7 May 2017

SharePoint search query length error.

SharePoint 2013 search results query works properly if select, filter, and other options are in the search query text limit. If the search query limit exceeds the search results, it gives an error.

"An administrator configuration transformed your query into an invalid query"


The search length (in KB) is controlled by the property MaxKeywordQueryTextLength of the SharePoint search service application. The default "MaxKeywordQueryTextLength" value is 4096 (4 KB). Use the PowerShell commands given below.




So increasing the MaxKeywordQueryTextLength value from 4 KB to 8 KB helped in fixing the error.

The reference blog: https://blogs.msdn.microsoft.com/sridhara/2014/05/06/sharepoint-2013-searchserviceexception-the-maximum-allowed-value-is-4096/


🚀 "Happy Coding" 🚀

Sunday 20 November 2016

SharePoint 2013 Apps Environment Configuration.

Setting up a SharePoint development environment has always been challenging, and with the new Apps model in SharePoint 2013, there are even more options and requirements.


First, setup and configure DNS on a Windows server. (http://www.tomsitpro.com/articles/configure-dns-windows-server-2012,2-793.html)

The steps to configure the SharePoint 2013 apps environment.

a) Create Apps Forward Lookup Zone

SharePoint 2013 Apps have their own isolated URLs, which are separate from the URLs of the sites where the app is being deployed and where the app is being used. In order to provide isolation, apps should run in their own domain instead of in the same domain name as your farm. Using a different domain name for apps helps prevent cross-site scripting between apps and SharePoint sites.

Microsoft recommends that the new domain name should NOT be a subdomain of the domain that hosts the SharePoint sites.

1. Open DNS Manager and run it as an administrator from the Start screen.

2. In DNS Manager, right-click Forward Lookup Zones, then the New Zone context menu option.



3. Click Next on the New Zone Wizard dialogue box.

4. On the Zone Type step, select the Primary Zone and click the Next button.

5. On the Zone Name step, enter the app domain name (like spappsdeveloper.com), and click the Next button.



6. On the Dynamic Update step, select the type of dynamic updates you want to allow. Here, with the Allow both nonsecure and secure dynamic updates option selected, click the Next button.





b) Link the SharePoint App Domain to the SharePoint Server

Now DNS will forward all the requests from spappsdeveloper.com to the SharePoint server hosting the apps.

1. In DNS Manager, under Forward Lookup Zones, right-click the newly created zone or app domain (spappsdeveloper.com). Click New Alias (CNAME) from the context menu option.

2. On the New Resource Record dialogue, enter * as the Alias name.

3. For a Fully qualified domain name (FQDN) for target host box, select the Browse button.

4. On the Browse dialogue, select server > Forward Lookup Zones > SharePoint sites host domain > select the record that points to the server that hosts the SharePoint site. Ensure Hosts and Aliases (A and CNAME Records) is selected as the Record types, and click the OK button.

5. Click OK to close the dialogue box.


c) Creating the Subscription Settings and App

 Management Service Applications

1. In SharePoint Central Administration, click the Manage service applications option.

2. Click the New button, then the App Management Service menu item.

3. On the New App Management Service Application dialogue, enter “App Management Service” as the Service Application Name. Select the Use existing application pool option, and then select AppManagementService Pool from the drop-down. Ensure the Create App Management Service Application checkbox is checked. Click the OK button.

4. Or you can use the PowerShell command to create an App Management Service Application.

# get the service application pool
$serviceAppPool = Get-SPServiceApplicationPool -Identity "AppManagementService Pool"

# create app management service application and save its response in variable
$appMngmntSvc = New-SPAppManagementServiceApplication -ApplicationPool
$serviceAppPool -Name "App Management Service" -DatabaseName "App Management Service DB"

# create app management service application proxy and mapping it to service application
New-SPAppManagementServiceApplicationProxy -ServiceApplication $appMngmntSvc

5. Once returned to the list of service applications, ensure both App Management Service Applications are started.

6. Open the SharePoint 2013 Management Shell as an administrator and run.

7. Start the SPAdminV4 and SPTimerV4 service applications.

net start SPAdminV4

net start SPTimerV4

8. Set the domain used to host apps to the new zone created above. It can be set by PowerShell as "done" or via central administration. It can also be done later at the Configure App URL step (see below).

# App Domain Name.
Set-SPAppDomain "spappsdeveloper.com"


9. Start the AppManagementServiceInstance and SPSubscriptionSettingsServiceInstance service instances.

# Start the App Management & Subscription SharePoint services.
Get-SPServiceInstance | where{$_.GetType().Name -eq "AppManagementServiceInstance" -or $_.GetType().Name -eq "SPSubscriptionSettingsServiceInstance"} | Start-SPServiceInstance


10. Ensure the AppManagementServiceInstance and SPSubscriptionSettingsServiceInstance service instances are Online.

# Check the "Online" status of the App Management & Subscription SharePoint services.
Get-SPServiceInstance | where{$_.GetType().Name -eq "AppManagementServiceInstance" -or $_.GetType().Name -eq "SPSubscriptionSettingsServiceInstance" }


11. Create the SharePoint Subscription Service.

# get the service application pool
$account = Get-SPManagedAccount "WIN-EODPTE6TSSE\sppool"
$serviceAppPool = New-SPServiceApplicationPool -Name "SettingsServiceAppPool" -Account $account

$serviceAppPool = Get-SPServiceApplicationPool -Identity "AppManagementService Pool"

# create subscription settings service application and save its response in variable
$appSubSvc = New-SPSubscriptionSettingsServiceApplication –ApplicationPool
$appPoolSubSvc -Name "SettingsServiceApp" –DatabaseName "SPSubscriptionSettingsServiceDB"

# create subscription settings service application proxy and mapping it to service application
$proxySubSvc = New-SPSubscriptionSettingsServiceApplicationProxy –ServiceApplication $appSubSvc




d) Configure the App URL

Set the name for the site subscription.

1. Open Central Administration, click Apps, and then click Configure App URLs.

2. In the App Prefix box, type a name to use for the URL prefix for apps.

3. Or it can also be done by PowerShell.

# SharePoint App Subscription name.
Set-SPAppSiteSubscriptionName -Name "apps" -Confirm:$false



Create a new Developer Site site collection for local App deployment.
Now you're ready to deploy your SharePoint apps to your local SharePoint development environment.

I hope this will help; do write your comments and suggestions.


🚀 "Happy Coding" 🚀

Saturday 5 November 2016

Object doesn't support property or method "attachEvent" in Internet Explorer 11.

I am getting this error on my SharePoint site, which breaks many functionality, like the list AddNew form UI, which is coming out distorted in IE. The error is due to the fact that "attachEvent" is a deprecated function. This error is occurring in the init.js file, which is provided out-of-the-box by SharePoint.

Google Chrome and Mozilla Firefox are handling this error, but IE is unable to do that.

Add the JavaScript code snippet to the JS file used in the master page or used globally.

<script language="javascript">
if (typeof browseris !== 'undefined') {
    browseris.ie = false;
}
</script>


🚀 "Happy Coding" 🚀