Set unique permissions to a SharePoint Group in SharePoint list using CSOM PowerShell

A SharePoint list when created inherits site’s permission, but as we move ahead in project and realise to maintain some unique permissions on the list, then below PowerShell function which uses client object model can be very helpful

For example:

There is a List called SUGGESTIONS and you would want even users from VISITORS group to contribute in it, then with the help of this function we can give contributors role to the users from VISITORS group.

function Set-PermissionsOnList()
{
param(
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$true)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$listName,
[Parameter(Mandatory=$true)][string]$GroupName,
[Parameter(Mandatory=$true)][string]$Roletype
)
begin{
try
{
#get Client Object
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $credentials
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
# get root web
$RootWeb = $context.Site.RootWeb
$context.Load($RootWeb)
$context.ExecuteQuery()
# get list
#$lists = $web.Lists
#$context.Load($lists)
#$context.ExecuteQuery()
#$list = $lists | where {$_.Title -eq $listName}
#Retrieve List
$List = $context.Web.Lists.GetByTitle($listName)
$context.Load($List)
$context.ExecuteQuery()
$list.BreakRoleInheritance($true, $false)
$roleType = [Microsoft.SharePoint.Client.RoleType]$Roletype
# get group/principal
$groups = $web.SiteGroups
$context.Load($groups)
$context.ExecuteQuery()
$group = $groups | where {$_.Title -eq $RootWeb.Title + " " + $GroupName}
# get role definition
$roleDefs = $web.RoleDefinitions
$context.Load($roleDefs)
$context.ExecuteQuery()
$roleDef = $roleDefs | where {$_.RoleTypeKind -eq $Roletype}
}
catch
{
Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red
}
}
process{
try
{
# Assign permissions
$collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
$collRdb.Add($roleDef)
$collRoleAssign = $list.RoleAssignments
$rollAssign = $collRoleAssign.Add($group, $collRdb)
$context.ExecuteQuery()
Write-Host "Permissions assigned successfully." -ForegroundColor Green
}
catch
{
Write-Host "Error while setting permissions. Error -->> " + $_.Exception.Message -ForegroundColor Red
}
}
end{
$context.Dispose()
}
}

The above function can be called this way

$credentials = Get-Credential
$ListTitle = "Your List Name"
Set-PermissionsOnList "http://YourSite" $credentials $ListTitle "Visitors" "Contributor"

Customize SharePoint 2013 suite bar

The unbranded SharePoint 2013 site contains a SuiteBar which again contains a branding text on top left corner and number of links on top right corner.
Once we start branding our site we would like to get rid of the branding text, and may be add one more link on top right corner.

Customizing Branding Text using jQuery:

OOB branding text looks something like below screen shot.

UnBarndedSuiteBar

And if we study the source of it using developer tool we can see an html structure like below screen shot.

SourceOfUnbrandedSuiteBar

So to change the branding text below jQuery code can work wonders.

If we just need a simple text change then

jQuery('div#suiteBarLeft .ms-core-brandingText').text("Hello World");

Or if we need to add some html then

jQuery('div#suiteBarLeft .ms-core-brandingText').html("<span>Hello World</span> <img src='your img path'>");

The result of above JavaScript code will be

CustomizedSuitBar

Customizing SuiteBar Links:

OOB branded site has 3 links as shown in below screen shot.

UnBrandedSuiteBarLinks

The html source of these links looks something like below.

SourceOfUnBrandedSuiteBarLinks

So to add one more link below code can be helpful.

var customLi = "<li class='ms-core-suiteLink'><a class='ms-core-suiteLink-a' target='_blank' href='www.yourlink.com'>Your Custom Link</a></li>";
if(jQuery("div#suiteLinksBox").children("ul").length > 0){
      	jQuery("div#suiteLinksBox").children("ul").append(customLi);
     	
}
else {
      	jQuery("div#suiteLinksBox").html('<ul class="ms-core-suiteLinkList">' + customLi + '</ul>')
}

The result of above code will be

CustomizedSuiteBarLink