Tuesday, 19 November 2013

Javascript: Get the current root url

This may not be the easiest way, but is certainly worked for me.

var url = $(location).attr('href').replace(window.location.pathname, '').replace(window.location.search, '');

Monday, 4 November 2013

SharePoint: How do I add and remove unique permissions in Powershell?

Here are two useful scripts to help you on your way:

function SetPermission($url, $list, $group, $permission)
{

$spWeb = Get-SPWeb $url
$selectedList = $spWeb.Lists[$list]

# Assign the "Contribute" RoleDefition to the site's visitors group
$visitorsSPGroup = $spWeb.Groups[$group]
If (! $selectedList.HasUniqueRoleAssignments) {
$selectedList.BreakRoleInheritance($true)
}
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($visitorsSPGroup)
$assignment.RoleDefinitionBindings.Add(($spWeb.RoleDefinitions | Where-Object { $_.Type -eq $permission }))
$selectedList.RoleAssignments.Add($assignment)

$selectedList.Update()

$spWeb.Dispose()
}

function RemovePermission($url, $list, $group)
{
$spWeb = Get-SPWeb $url
$selectedList = $spWeb.Lists[$list]

$visitorsSPGroup = $spWeb.Groups[$group]

If (!$selectedList.HasUniqueRoleAssignments) {
$selectedList.BreakRoleInheritance($true)
}

$web.AllowUnsafeUpdates = $true;
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments = $selectedList.RoleAssignments

for ([int] $a = $spRoleAssignments.Count - 1; $a -ge 0; $a--)
{
if ($spRoleAssignments[$a].Member.Name -eq $group)
{
$spRoleAssignments.Remove($a);
}
}
$web.Dispose()
}

Sunday, 3 November 2013

PowerShell: Loop through all the sites in a site collection (not recursive)

$site = Get-SPSite "http://mysitetosearch.com"
$topWeb = Get-SPWeb $site.Url
$site | Get-SPWeb -limit all | ForEach-Object { Write-Host "Found $_.Name"; }