Sunday, January 17, 2016

SharePoint Wakeup script Configuration in Task Scheduler

This script will access your SharePoint site specified in URL (in script) after every few minutes and will keep the application pool warm thus making it quicker for users to access the site.

1) Log on to SharePoint Server

2) Copy Script below in a  file and save that file as spwakeup.ps1

function get-webpage([string]$url,[System.Net.NetworkCredential]$cred=$null)
{
    $error = $false
    $webRequest = [System.Net.HttpWebRequest]::Create($url)
    $webRequest.Timeout = 300000
    if($cred -eq $null)
    {
        $webRequest.Credentials = [system.Net.CredentialCache]::DefaultCredentials
    }
    try {
    $res = $webRequest.getresponse()
    }catch{
    $error = $true
    }
}
   
Function Load-SharePoint-Powershell
{
     If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
     {
              Write-Host -ForegroundColor White " - Loading SharePoint Powershell Snapin"
          Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
     }
}
Load-SharePoint-Powershell
$webapplications = Get-SPWebApplication [SITEURL]
$AllSites = Get-SPSite -limit all
$Array = @()
$i=0
foreach ($wa in $webapplications)
{
    foreach ($windowsauth in $wa.AlternateUrls)
    {
        $authenticationprovider= Get-SPAuthenticationProvider -webapplication $wa -zone $windowsauth.Zone
        If ($authenticationprovider.UseWindowsIntegratedAuthentication)
        {
            $accessableURL = $windowsauth.IncomingUrl
        }
    }
    if (!$AccessableURL) {$accessableURL = $wa.url -replace ".$"}
    foreach ($site in $AllSites)
    {
        if ($Site.Url -and $Site.Url+"/" -match $wa.url)
        {
            $subsites = Get-SPSite $Site.Url | Get-SPWeb -Limit All
            Foreach ($subsite in $subsites)
            {
                $i++
                Write-Progress -activity "Looking up all sites" -status "Please Wait..." -PercentComplete (($i / 500) * 100)
                $PlainWaUrl = $wa.Url -replace ".$"
                $WakeUpSite = $Subsite.Url.replace($PlainWaUrl, $accessableURL)
                $Array = $Array + $WakeUpSite
                #$html=get-webpage -url "$WakeUpSite" -cred $cred;
                if ($i -eq 500){$i=0}
            }
        }
    }
    Remove-Variable accessableURL
}

$i=0
Foreach ($Website in $Array)
{
    $i++
    Write-Progress -activity "Waking up sites" -status "Waking: $Website" -PercentComplete (($i / $Array.Count) * 100)
    $html=get-webpage -url "$Website" -cred $cred;
}


3) Click Start-> Administrative Tools - > Task Scheduler

2) Expand the left hand tree and right click on Task Schedule Library to create a new Task.


3) Give your task a friend name.

4) In General section come to  Security Options select the following options
a) Run whether user is logged on or not
b) Run with highest privileges
c)  Hidden

5) In Triggers section , add a new trigger and specify the following.

a) Begin the task -> on a schedule
b) Settings:
 One Time
Start date and time should be today's date and time
c) Advanced Settings - > Select Enabled.

6) Select Actions and add a new Action

a) Action -> Start a Program
b) Program/script - > PowerShell.exe
c) add a arguments (optional) -> Execution Policy bypass "Path of script file which you have saved in step 2"


7) Select the Conditions tab and in power section select
a) Start the task only in the computer is on AC Power

8) Select the Settings tab and select

a) Allows task to be run on demand
b) Stop the tasks if it runs longer than 5 days
c) If the running task does not end when requested, force it to stop.
d) Do not start a new instance


9) Click Ok.



Build trust relationship with local domain of SharePoint server and Other Domain so that users of trusted domain can be used in SharePoint

Build trust relationship with local domain of SharePoint server and Other Domain so that users of trusted domain can be used in SharePoint

Scenario: 

  • SharePoint Farm installed in domain1.com 
  • All company users are registered in domaininternal.com 
  • Requirement: Add users from domaininternal.com to SharePoint site collection which is running in domain1.com 


Run this on Sharepoint PowerShell:

stsadm.exe -o setproperty -url -pn "peoplepicker-searchadforests" -pv "", ,,


Important: LoginName is the login name of user who is domain administrator or added to domain admin group, the command will run otherwise but the results will be be as desired.


Good References for this:
https://blog.tallan.com/2013/04/18/setting-up-an-external-domain-trust-in-a-simple-sharepoint-2010-extranet-with-separate-ad-domains-and-completing-a-profile-sync/

http://www.boostsolutions.com/blog/how-to-add-trust-domains-in-a-sharepoint-farm/

http://blog.sharepoint-voodoo.net/?p=17

Lock/Unlock FBA Account

If you have FBA packed installed , it can be used to unlock an account but if you want to unlock many accounts SQL statement below can be used to do that.

Un-Lock FBA Account
Update [DBNAME]
set IsLockedOut=0 ,FailedPasswordAttemptCount=0

where  IsLockedOut=1 and   LastLockoutDate=DateAdd(Minute,30,LastLockoutDate)


P.S: where clause can be modified to add different criteria.  

SharePoint 2013 - Get User Title and Email

var context = new SP.ClientContext.get_current();
web = context.get_web()
 var currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(web);
context.executeQueryAsync(
var userObject = web.get_currentUser();
console.log(userObject.get_email());
console.log(userObject.get_loginName());
console.log(userObject.get_title());
);

SharePoint 2013 - Set and Get Value for Hyperlink Field in JSOM

Set and Get Value for Hyperlink Field in JSOM


Set Value
var urlvalue = new SP.FieldUrlValue();
urlvalue.set_url('www.google.com');
urlvalue.set_description('Title');
oListItem.set_item("URL", urlvalue);



Get Value
console.log(fvalue.get_url());
console.log(fvalue.get_description());