Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

Thursday, January 16, 2014

SharePoint 2013: Toggling between minimized and debugged version of java scripts

There is no doubt that including a minimized versions of your scripts (and css files) is best practice, not only for SharePoint but for any Web Application. However, this practice is often ignored by many programmers. Minimized version of script significantly reduces the page’ payload, especially for large javascript (and css files). Just to give you a comparison, debug version of JQuery 2.0.3 version is 246KB but its minimized counterpart is only 82KB.

On the other hand, debugged version of scripts are convenient for developers because setting breakpoints and debugging using developer toolbars is easier.

Now, what if we could archive best of both the worlds! What if we could toggle between debugged version and minimized version without re-deploying anything? Well, I would love it and so I thought about using the technique that I described here.

Note following though:

  • Even though this post uses some SharePoint 2013, with just a little tweak it can be applied to SharePoint 2010 as well.
  • The technique here shows only one JavaScript file, but as many JavaScripts as you want can be included.
  • Similar technique can be used for CSS files as well
  • I have used a couple of site collection level FEATURES which are visible on Site Collection Features page. But if you prefer, you may make them hidden and activate them using PowerShell.

Let’s go to the business now!

The first step is to create a UserControl in your solution and put it under ControlTemplates mapped folder as shown below:

image

Define a property in your code behind of the User Control as shown below:

   1:   private bool _minimized = false;
   2:   
   3:          public bool Minimized
   4:          {
   5:              get { return _minimized; }
   6:              set { _minimized = value; }
   7:          }



Now refer your scripts on in the .ascx file as shown below:

<% if (Minimized == true) {%>
<script type="text/javascript" src="/_layouts/MyApp/scripts/jquery-2.0.3.min.js"></script>
<!-- Other files goes here-->
<% }else{ %>
<script type="text/javascript" src="/_layouts/MyApp/scripts/jquery-2.0.3.js"></script>
<!-- Other files goes here-->
<%} %>




You may have to provide your own script paths..



Next, create two FEATURES and Add two empty elements using VS.NET’s “Empty Element” project item as shown below:


image


image


Add one empty element to each feature. You may use your own naming conventions for your FEATUREs and Elements.


The content of MimimizedScript –> Elements.xml file would look like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control Id="AdditionalPageHead" ControlSrc="/_controltemplates/15/MyApp/MyAppScriptReferences.ascx" Sequence="20">
<Property Name="Minimized">true</Property>
</Control>
</Elements>



And the content of RegularScripts—>Elements.xml file would look like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control Id="AdditionalPageHead" ControlSrc="/_controltemplates/15/MyApp/MyAppScriptReferences.ascx" Sequence="21">
<Property Name="Minimized">false</Property>
</Control>
</Elements>



Note the path to my user control, in SharePoint 2010, the path would look different.


Deploy your solution and get ready to see it in action. I have two FEATURES and I have activated the minimized version as shown below:


image 


When minimized version FEATURE is activated, I enjoy the performance benefits of minimized version of my scripts.


image


If I want to debug my JavaScript, all I need to do is deactivate the old feature and activate the feature which provisions regular debug version of scripts. As soon as I do that, regular JavaScript becomes available as shown below:


image


I think this is very convenient but it may not be necessarily the best option. If you find this useful or have a better idea, please leave comments below.


Monday, October 18, 2010

Creating a Web Application with all out of the box templates

As a SharePoint Developer, I am sure you would think about creating one site collections for all available web templates for learning purpose. I used to to do this as first thing after I was done with configuring my server farm. After doing it several times manually, I thought of automating it and I am sharing my experience here…

Welcome to the world of SharePoint 2010 and PowerShell, it is not too difficult to do this.

Here are the steps to do so….

First, we need to create Web Application that will host several Site Collections (as many as the out of the box web templates). I took an approach of creating a .csv file as an input to my web application script. For now, I will have just one row in my WebApps.csv but you can have as many as you want!

Here is the script which access the WebApps.csv file and creates web application:

Add-PsSnapin Microsoft.SharePoint.PowerShell
Import-Csv -Path "WebApps.csv" -Delimiter "," | ForEach-Object {New-SPWebApplication -Name $_.Name -Port $_.Port -ApplicationPool  $_.ApplicationPool -ApplicationPoolAccount (Get-SPManagedAccount $_.PoolAccount)}
Write-host "Operation was executed successfully."
Read-Host

For the purpose of this exercise, my WebApps.csv looks like:

image

Next, I created a list of site collections and then provisioned each site collection with out of the box site template I used same .csv approach above and used following PowerShell script to create the site collections:

Add-PsSnapin Microsoft.SharePoint.PowerShell
Import-Csv -Path "Templates.csv" -Delimiter "," | ForEach-Object {New-SPSite -Url $_.Url -OwnerAlias "Sp10\Administrator" -Language 1033 -Template $_.Name -Name $_.Title }
Write-host "Operation was executed successfully."
Read-Host

As you can see we needed an official name of the site template if you want to create a site collection programmatically. And I used another SharePoint cmdlet to get list of appropriate site templates as shown below

Get-SPWebTemplate | where { ( $_.'IsHidden' -eq [System.Boolean]$False ) -and ( $_.'IsSubWebOnly' -eq [System.Boolean]$False ) }

The result of above command would look like:

image

I used above result to create a .csv file that I need, my final Templates.csv file looks like:

Lcid,Title,Name,url
1033,Team Site,STS#0,http://#YourWebAppUrl#
1033,Blank Site,STS#1,http://#YourWebAppUrl#/Sites/Blank
1033,Document Workspace,STS#2,http://#YourWebAppUrl#/Sites/DocWS
1033,Basic Meeting Workspace,MPS#0,http://#YourWebAppUrl#/Sites/BasicMeetWS
1033,Blank Meeting Workspace,MPS#1,http://#YourWebAppUrl#/Sites/BlankMeetWS
1033,Decision Meeting Workspace,MPS#2,http://#YourWebAppUrl#/Sites/DecisionMeetWS
1033,Social Meeting Workspace,MPS#3,http://#YourWebAppUrl#/Sites/SocialMeetWS
1033,Multipage Meeting Workspace,MPS#4,http://#YourWebAppUrl#/Sites/MeetWS
1033,Blog,BLOG#0,http://#YourWebAppUrl#/Sites/Blog
1033,Group Work Site,SGS#0,http://#YourWebAppUrl#/Sites/GroupSite
1033,Document Center,BDR#0,http://#YourWebAppUrl#/Sites/DocCenter
1033,Records Center,OFFILE#1,http://#YourWebAppUrl#/Sites/Records
1033,Business Intelligence Center,BICenterSite#0,http://#YourWebAppUrl#/Sites/BI
1033,Enterprise Search Center,SRCHCEN#0,http://#YourWebAppUrl#/Sites/EntSearch
1033,Publishing Portal,BLANKINTERNETCONTAINER#0,http://#YourWebAppUrl#/Sites/PubPortal
1033,My Site Host,SPSMSITEHOST#0,http://#YourWebAppUrl#/Sites/mysite
1033,Enterprise Wiki,ENTERWIKI#0,http://#YourWebAppUrl#/Sites/EntWiki
1033,Basic Search Center,SRCHCENTERLITE#0,http://#YourWebAppUrl#/Sites/BasicSearch
1033,FAST Search Center,SRCHCENTERFAST#0,http://#YourWebAppUrl#/Sites/FASTSearch
1033,Visio Process Repository,visprus#0,http://#YourWebAppUrl#/Sites/Visio

You may want to change the data in .csv file as needed!

Hope you enjoyed reading this and save some time by automating this process!