Power Shell Command using CSOM

Hi,

We have prepared a Power Shell command using Client Side scripts as per client requirements. The client did not had access to the SharePoint Server. So, PS commands with server side scripts was out of scope. 

The client requirement was to delete all the permissions on the list that was being inherited from site. Since the number of users were huge, so deleting all the users using ribbon was not possible.

Please have a look at the scripts below and let me know your feedback. If you have better suggestion than this, please let me know.

#################################################

#location of client dlls on users' local system

$loc = "C:\SharePoint\ClientDLL" 

//SharePoint Site URL

$siteUrl = "" 

#Write your Login ID

$loginname = "" 

#Write the Name of the List

$listname = "" 

Set-Location $loc 

Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")

Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll") 

Write-Host "Please enter password for $($siteUrl):"

$pwd = Read-Host -AsSecureString

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 

Try

{

 

# Remove or replace line below to change authentication method

$ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd) 

$web = $ctx.Web

$ctx.Load($web)

$ctx.ExecuteQuery() 

#Write-Host "Current web title is '$($web.Title)', $($web.Url)"  

$list = $Web.Lists.GetByTitle($listname); 

$ctx.Load($list); 

$ctx.ExecuteQuery(); 

Write-Host "Deleting Permissions on List : $($list.Title)" 

if($list.HasUniquePermissions)

{

                #Write-Host "Has Unique Permissions"

                $list.ResetRoleInheritance()

                $list.BreakRoleInheritance($false, $true)

                Write-Host "Permissions deleted" -foregroundcolor Green

} 

if(-not $list.HasUniquePermissions)

{

               

                #Write-Host "Does not has Unique Permissions"

                $list.ResetRoleInheritance()

                $list.BreakRoleInheritance($false, $true)

                Write-Host "Permissions deleted" -foregroundcolor Green

}

               

}

Catch

{

                Write-Host "Some Error, Please Contact Administrator" -foregroundcolor Red

                return

} 

$list.Update() 

$ctx.ExecuteQuery();

Regards,

S

March 17th, 2015 9:19am

You don't need to load web and call execute query. So, you can get rid if these two statements:
$ctx.Load($web)
$ctx.ExecuteQuery() 

Also, if you want to break permissions in any case, you don't need to use if statements. You can use:
#Write-Host $list.HasUniquePermissions
$list.ResetRoleInheritance()
$list.BreakRoleInheritance($false, $true)
Write-Host "Permissions deleted" -foregroundcolor Green
Free Windows Admin Tool Kit Click here and download it now
March 17th, 2015 10:00am

Hi Sudheer,

In Client-Side, we can also use .NET Client Object Model to achieve your requirement.

Remove permission in Sharepoint List using Client Object Model

https://rambabualaparthi.wordpress.com/2013/12/30/remove-permission-in-sharepoint-list-using-client-object-model/

In order to better manage SharePoint in Client side, I suggest develop a Winform or WPF application and use .Net Client Object Model to achieve some requirements.

Best Regards

March 19th, 2015 2:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics