Montag, 31. August 2015

SharePoint liefert debug-JS

Das im SharePoint verwendete JavaScript ist minified, aber manchmal möchte ich es trotzdem debuggen...

Das geht über eine Einstellung in der web.config oder über das ScriptMode-Attribut am ScriptManager in der MasterPage.

Ein Attribut in der web.config scheint mir einfacher als die MasterPage anzupassen... Mit der Einschränkung, dass eine manuelle Anpassung der web.config im SharePoint keine wirklich gute Idee ist. Microsoft hat uns dafür die SPWebConfigModification mitgegeben.

Damit das ein- und abschalten der debugging-JS trotzdem einfach ist habe ich mir das folgende Skript gebaut:

<#
    .SYNOPSIS
    Set your SharePoint webApplication to use debugging JS-files 
    instead of minified.

    .DESCRIPTION
    SharePoint uses minified JavaScript (e.g. SP.js) but is also able to
    use debugging versions of the files instead. (e.g. SP.debug.js)
    Setting SharePoint to deliver debug-JS requires a setting
    in web.config. 
    This script sets the required value by adding a SpWebConfigModifaction
    to your WebApplication.

    .PARAMETER WebApplication
    URL to WebApplication

 .PARAMETER Remove
    Remove this modification (and thereby restore the original web.config)

    .EXAMPLE
    .\Enable-SpJsDebugging.ps1 -WebApplication http:/your.farm/site/
    Apply webConfig-Modification to allow for debugging-js to be used.

    .EXAMPLE
    .\Enable-SpJsDebugging.ps1 -WebApplication http:/your.farm/site/ -Remove
    Remove webConfig-Modification and retore original web.config
#>
[CmdletBinding()]
param (
 [Parameter(Mandatory=$true)]
    [string]$WebApplication,

    [Parameter(Mandatory=$false)]
    [switch]$Remove = $false
)

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
 Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$name = "PowerShell.Scripts.EnableSpJsDebugging";

function Get-Modifications([Microsoft.SharePoint.Administration.SPWebApplication]$webApp){
    return $webApp.WebConfigModifications | ?{ $_.Owner -eq $name }
}

function Remove-Modifications([Microsoft.SharePoint.Administration.SPWebApplication]$webApp, 
    [Microsoft.SharePoint.Administration.SPWebService]$contentService){
    $mods = @();
    $removed = @()
    Get-Modifications($webApp) | %{ $mods = $mods + $_};
    $mods | %{ $removed += $webApp.WebConfigModifications.Remove($_) };
    $webApp.Update();
    $contentService.ApplyWebConfigModifications();
    $success = $removed | ?{ $_ -eq $true };
    $fails = $removed | ?{ $_ -eq $false };
    
    Write-Output "Removed $($success.Length) modifications.";
    if($fails.length > 0) {
        Write-Output "$($fails.Length) tries failed.";
    }
}

function Add-Modification([Microsoft.SharePoint.Administration.SPWebApplication]$webApp, 
    [Microsoft.SharePoint.Administration.SPWebService]$contentService){
    $mod = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification";
    $mod.Path = "configuration/system.web/compilation";
    $mod.Name = "debug";
    $mod.Sequence = 0;
    $mod.Owner = $name;
    $mod.Type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureAttribute;
                     
    $mod.Value = "true";

    $unused = $webApp.WebConfigModifications.Add($mod);
    $webApp.Update();
    $contentService.ApplyWebConfigModifications();
    Write-Output "Added modification.";
}

$gc = Start-SPAssignment
try {
    $webApp = Get-SPWebApplication $WebApplication -AssignmentCollection $gc;
    $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

    if($Remove){
        Remove-Modifications $webApp $contentService;
        Exit;
    }

    if((Get-Modifications $webApp).length -gt 0) {
        Write-Output "Modification already applied...";
        Exit;
    }

    Add-Modification $webApp $contentService;
} finally {
    Stop-SPAssignment $gc
}

Die Verwendung ist dann denkbar einfach:
> .\Enable-SpJsDebugging.ps1 -WebApplication http:/your.farm/site/
> .\Enable-SpJsDebugging.ps1 -WebApplication http:/your.farm/site/ -Remove

Keine Kommentare:

Kommentar veröffentlichen