Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Last active August 29, 2015 14:06
Show Gist options
  • Save devendrasv/2f77fb340bffd0bcb291 to your computer and use it in GitHub Desktop.
Save devendrasv/2f77fb340bffd0bcb291 to your computer and use it in GitHub Desktop.
#Credentials to connect to office 365 site collection url
$url ="https://velegandla.sharepoint.com"
$username="[email protected]"
$password="yourpassword"
$Password = $password |ConvertTo-SecureString -AsPlainText -force
Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow
Set-Location $PSScriptRoot
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
Write-Host "CSOM libraries loaded successfully" -foregroundcolor black -backgroundcolor Green
Write-Host "authenticate to SharePoint Online site collection $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$Context.Credentials = $credentials
$context.RequestTimeOut = 5000 * 60 * 10;
$web = $context.Web
$site = $context.Site
$context.Load($web)
$context.Load($site)
try
{
$context.ExecuteQuery()
Write-Host "authenticateed to SharePoint Online site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Not able to authenticateed to SharePoint Online site collection $url $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
return
}
#Define Custom permission level
$permissionlevel = "addListItems, editListItems, viewListItems"
#function create Custom Role Definitions
function CreateRoleDefinitions($permName, $permDescription, $permissionString)
{
$roleDefinitionCol = $web.RoleDefinitions
$Context.Load($roleDefinitionCol)
$Context.ExecuteQuery()
$permExists = $false
#Check if the permission level is exists or not
foreach($role in $roleDefinitionCol)
{
if($role.Name -eq $permName)
{
$permExists = $True
}
}
Write-Host Creating Pemission level with the name $permName -foregroundcolor black -backgroundcolor Yellow
if($permExists -ne $True)
{
try
{
$spRoleDef = New-Object Microsoft.SharePoint.Client.RoleDefinitionCreationInformation
$spBasePerm = New-Object Microsoft.SharePoint.Client.BasePermissions
$permissions = $permissionString.split(",");
foreach($perm in $permissions)
{
$spBasePerm.Set($perm)
}
$spRoleDef.Name = $permName
$spRoleDef.Description = $permDescription
$spRoleDef.BasePermissions = $spBasePerm
$roleDefinition = $web.RoleDefinitions.Add($spRoleDef)
$Context.ExecuteQuery()
Write-Host Pemission level with the name $permName created -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host There was an error creating Permission Level $permName : Error details $_.Exception.Message -foregroundcolor black -backgroundcolor Red
}
}
else
{
Write-Host Pemission level with the name $permName already exists -foregroundcolor black -backgroundcolor Red
}
}
#calling role definition function
CreateRoleDefinitions -permName "SPJ Custom Permissions" -permDescription "This permission level is created for training site and contains add/edit/View item permissions" -permissionString $permissionlevel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment