Wednesday, March 27, 2013

Custom error setting for error in layouts page

I was getting an error in the layouts page http://msite/_layouts/new.aspx, but instead of displaying error description below error message was displyed


Server Error in '/' Application.


Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>


To fix the issue finally I modified <customErrors/> tag in web.config file at below location C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS



Friday, March 8, 2013

while using spsecurity.runwithelevatedprivileges if we want to create or update a splistitem using the elevated previledges, we need to create new SPSite and SPWeb objects, but you would get an error “The security validation for this page is invalid …” since a new SPContext object is created.
 
There are two ways to get past this error
1) set SPWeb.AllowUnsafeUpdates = true
2) Use SPUtility.ValidateFormDigest()

Tuesday, March 5, 2013

Install WSP Solutions

Add and deploy features in SP2010

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/12/02/adding-and-deploying-solutions-with-powershell-in-sharepoint-2010.aspx

Add assembly to GAC using powershell

http://blog.goverco.com/2012/04/use-powershell-to-put-your-assemblies.html

Powershell batch file to remove and install wsp file again, need to remove last line and add -force attribute to deploy command

http://jstevensblog.com/post/Powershell-Script-to-Automatically-Deploy-Sharepoint-WSP-Packages.aspx

Finally, above batch file looks like shown below after adding commands to deploy farm level solution and activate features at the web application level. Please note, this file needs to be saved in the folder where wsp files to be installed are located, and also this file would install all wsp files in the folder to all web applications at the farm.

# Change Powershell execution directory to the scripts folder
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase

# Declare constants
$packages = (dir *.wsp | Select-Object name
$currentDir = (Get-Location).Path 
$urlWebApplication = "http://sharepoint:1234/"

if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
    Write-Progress -Activity "Loading Sharepoint Powershel Snapin" -Status "Loading Microsoft.SharePoint.PowerShell"
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}


Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.
Write-Host "Started package installation"

# Wait for existing timer jobs to finish
function WaitForJobToFinish ([string]$solutionName)   
{
    $JobName = "solution-deployment-$solutionName*"   
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }   
    if ($job -eq $null)    
    {
        Write-Host "Timer job not found"
    }
    else
    {
        $JobFullName = $job.Name       
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"           
        while ((Get-SPTimerJob $JobFullName) -ne $null)        
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2       
        }
        Write-Host  "Finished waiting for job.."   
    }
}

# Retract each solution .wsp file at web application
foreach ($i in $packages
    Write-Host -NoNewLine "Waiting for deployment jobs to finish..."           
    while ((Get-SPTimerJob  | ?{$_.Name -like "solution-deployment*"}) -ne $null)        
    {
        Write-Host -NoNewLine .
        Start-Sleep -Seconds 2       
    }
    Write-Host  "Finished waiting for job.."   
    Write-Host "Retracting: "$i.Name
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    Write-Host $solution.Name 

   

    #Uninstall soltion
    if ($solution -ne $null
    { 
        Write-Host "Solution Found..."

        # Deactivate features
        $features = Get-SPFeature -Limit ALL | where {$_.solutionId -eq $solution.SolutionId}
        foreach($feature in $features)
        {
            If($feature.Status -ne "Offline")
            {
                Write-Host  "Deactivating Feature: "$feature.Name
                Disable-SPFeature -identity $feature.Id -URL $urlWebApplication -Confirm:$false
            }
        }
        if ($solution.Deployed -eq $true
        { 
            Write-Host "Uninstalling..."
            try
            {
                if ($solution.ContainsWebApplicationResource)
                {
                    Uninstall-SPSolution -Identity $i.Name -AllWebApplications -Confirm:$false
                }
                else
                {
                    Uninstall-SPSolution -Identity $i.Name -Confirm:$false
                }
            }
            catch
            {
                Uninstall-SPSolution -Identity $i.Name -Confirm:$false 
            }
        }
        do
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        until ($solution.Deployed -eq $false)
        WaitForJobToFinish $i.Name
        Start-Sleep -Seconds 5
        Write-Host "Retract Completed!"
        Write-Host "Removing Solution..."
        Remove-SPSolution -Identity $i.Name -Force -Confirm:$false
        Write-Host "Remove Completed!"
    } 
    else
    { 
        Write-Host "WSP Package was not previously installed, moving to next step"
    } 

# Add each solution .wsp file at the web application and activate features
foreach ($i in $packages)
    Write-Host "Deploying: " $i.Name
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    if ($solution -eq $null
    { 
        Write-Host "Adding Solution: " $i.Name
        $solution = Add-SpSolution -LiteralPath ($currentDir + "\" + $i.Name) 
        WaitForJobToFinish $solution.Name
        Write-Host "Deployment Completed!"
        Write-Host "Installing Solution: " $i.Name
        try
        {
            if ($solution.ContainsWebApplicationResource)
            {
                Write-Host "Installing for web application " $urlWebApplication
                Install-SPSolution -Identity $solution.Name -WebApplication $urlWebApplication -GACDeployment -force
            }
            else
            {
                Install-SPSolution -Identity $solution.Name -GACDeployment -force
            }
        }
        catch
        {
            Install-SPSolution -Identity $solution.Name -GACDeployment -force
        }
        do
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        until ($solution.Deployed -eq $true)
        WaitForJobToFinish $i.Name
        Start-Sleep -Seconds 5
        Write-Host "Install Completed!"
        # Activate features
        $features = Get-SPFeature -Limit ALL | where {$_.solutionId -eq $solution.SolutionId}
        foreach($feature in $features)
        {
            Write-Host  "Activating Feature:" $feature.Name
            Enable-SPFeature -identity $feature.Id -URL $urlWebApplication
        }
    } 
}
Stop-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.



Also there is a related script to activate web application, site collection or web level features based on a CSV file available to download at http://www.sharepointpals.com/post/Activating-Feature-in-Web-SiteCollection-WebApplication-Scopes-using-PowerShell-in-SharePoint-2013

This is silly, but I need to enable view file extentions option on folders frequently and I always forget it, so thought better make a note of it &-)

  1. Open Folder Options by clicking the Start button Picture of the Start button, clicking Control Panel, clicking Appearance and Personalization, and then clicking Folder Options.
    1. Click the View tab, and then, under Advanced settings, do one of the following:
    2. To hide file extensions, select the Hide extensions for known file types check box, and then click OK.
    • To display file extensions, clear the Hide extensions for known file types check box, and then click OK.
  • Monday, March 4, 2013

    Friday, March 1, 2013

    The method Roles.GetRolesForUser throws an error

    Exception of type 'System.ArgumentException' was thrown.
    Parameter name: encodedValue


    Below post helped me understand cause of the error, it was resolved after attaching 0#
    .w|<Domain Name>\\ to the user name
     
    string userIdentityName = "0#.w|<domain name>\\" + username;


    Reference

    http://go4answers.webhost4life.com/Example/custom-role-provider-138386.aspx


    Ironically Roles.AddUserToRole(userName, roleName) does not need this modification and it can work only with the user name, thus finally though first statement is working, second is still failing because user in GetRolesForUser is different that the user in AddUserToRole method.

    After some efforts I found out that solution is to use

    string[] usersinrole = Roles.GetUsersInRole(roleName);
    or
     bool isuserinrole = Roles.IsUserInRole(userName, roleName);
     
    these two methods return the same user as AddUserToRole method.

    I need to check if below solution works.
    http://blog.mastykarz.nl/notetoself-custom-membership-role-providers-sharepoint-2010-claims/

    Secure micro services using jwt and ocelot

      Secure Microservices Using JWT With Ocelot in .NET Core (code-maze.com)