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

Sicherheitswarnung im Visual Studio beim debuggen des IIS

Wenn man im Visual Studio den Debugger an den IIS hängt kommt vorher ein Warnhinweis.

Das ist jetzt keine neue Information - und nach einer kurzen Suche kommt auch schnell wieder auf irgend einen Punkt an dem steht welcher Registry-Key auf welchen Wert gesetzt werden muss...

Da es mich aber immer etwas stört vorher suchen zu müssen und mich anschließend durch die Registry zu hangeln habe ich das mal als PowerShell-Skript abgelegt:

<#
 .SYNOPSIS
 Disable warning-messages when attaching VS to iis/w3wp.

 .DESCRIPTION
 When you attach VS to iis/w3wp for debugging, VS displays a warning.
 This script disables theese warnings by modifying the registry accordingly.
 WARNING: Make sure VS is not running when you start this script!

 .PARAMETER VsVersion
 The VS-version to modify - e.g. 10.0 for VS2010 or 14.0 for VS2015

 .PARAMETER AllVersions
 modify all installed Versions of VS

 .PARAMETER ReEnable
 Undo the changes done by this script - i.e. enable the warnings

 .PARAMETER IgnoreRunningVS
 continue script execution, even if a running VS is detected.

 .EXAMPLE
 .\Disable-VsDebuggerWarning.ps1 -VsVersion 12.0
 Disable the warning only for VS2013

#>

[CmdletBinding(DefaultParameterSetName="all")]
param (
 [Parameter(Mandatory=$false, ParameterSetName="all")]
    [switch]$AllVersions = $false,

 [Parameter(Mandatory=$true, ParameterSetName="one")]
    [string]$VsVersion,

 [Parameter(Mandatory=$false, ParameterSetName="all")]
    [Parameter(Mandatory=$false, ParameterSetName="one")]
    [switch]$ReEnable = $false,

 [Parameter(Mandatory=$false, ParameterSetName="all")]
    [Parameter(Mandatory=$false, ParameterSetName="one")]
    [switch]$IgnoreRunningVS = $false
)

$vsPath = "HKCU:\Software\Microsoft\VisualStudio\";

function Get-AllVsVersions()
{
    if(!(Test-Path $vsPath ))
    {
        Write-Error "No VisualStudio installations found. Exiting";
        Exit;
    }

    $versions = Get-ChildItem $vsPath | Where-Object { !$_.Name.EndsWith("_Config") } | Split-Path -Leaf

    Write-Host "$($versions.Length) VS installations found in registry: $versions"

    return $versions;
}

function Disable-Warning($version)
{
    $key = "DisableAttachSecurityWarning";
    $basePath = Join-Path $vsPath ($version + "\Debugger");
    $path = Join-Path $basePath $key;
    Write-Output "Setting $path to DWORD: $dword";

    # it's possible the key does not exist (e.g. in VS2015)
    $nonExistingItem = ((Get-ItemProperty $basePath -Name $key -ErrorAction SilentlyContinue) -eq $null);
    if($nonExistingItem)
    {
        if(!(Test-Path $basePath))
        {
            Write-Error "$basePath does not exist! Unable to proceed! Exiting";
            return;
        }

        New-ItemProperty $basePath -Name $key -Value $dword -PropertyType "DWord" 
    }
    else
    {
        Set-ItemProperty $basePath -Name $key -Value $dword
    }
}

$isVsRunning = ((Get-Process | where { $_.Name -eq "devenv" }).length -gt 0)

if($isVsRunning)
{
    Write-Host "A running VisualStudio was detected..." -ForegroundColor Red;
    if(!$IgnoreRunningVS)
    {
        Write-Output "If you want to prceed anyway, use the ""-IgnoreRunningVS""-Switch. Exiting.";
        Exit;
    }

    Write-Host "Ignoring the running VisualStudio... WARNING: this might not acually work! You have been warned!" -ForegroundColor Yellow;
}

$dword = 1;
if($ReEnable)
{
    $dword = 0;
}

$versions = @();
if($AllVersions)
{
    $versions = Get-AllVsVersions
}
elseif(![string]::IsNullOrWhiteSpace($VsVersion))
{
    $versions += $VsVersion
}
else
{
    Write-Host "Neither -VsVersion nor -AllVersions given. Nothing to do. :-(" -ForegroundColor Yellow
}

$versions | %{ Disable-Warning $_ }

Das Skript kann für eine bestimmte Version (z.B. mit "-VsVersion 10.0") oder einfach für alle Versionen ("-AllVersions") aufgerufen werden.