Share via


How to BreakRoleInheritance for an list item in a list using Powershell

Question

Tuesday, October 20, 2015 6:55 PM

Hi I would like to know how I can breakRoleInheritance for a list item using Powershell. Please see below. Thanks in advance.

$mysitename = "http://xxx.com/"  
$web = get-spweb -identity $mysitename # Get the site collection
$list = $web.Lists["mylist"]

$item =$list["myfile.xml"] ?????

$item.BreakRoleInheritance($false)

All replies (9)

Friday, October 23, 2015 1:31 AM ✅Answered | 1 vote

Hi wkpli,

I have done a test again in SharePoint 2010 and I reproduced the  issue.

The command  $Item.BreakRoleInheritance($true) is working in SharePoint 2013 while not working in SharePoint 2010.

You said that "used C# instead Powershell and the BreakRoleInheritance works",  have you successfully stop inheriting permissions from list items in SharePoint 2010?

Check the following PowerShell:

http://www.sharepointdiary.com/2013/03/break-inheritance-set-item-level-list-permission-powershell.html

Best regards,

Lisa Chen

TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected].


Tuesday, October 20, 2015 7:28 PM | 1 vote

You would simply run:

$web = Get-SPWeb http://webUrl
$list = $web.Lists["List_Title"]
$item = $list.Items[Item_Index] #or however you want to get it
$item.BreakRoleInheritance($false) #or $true to copy permissions

Trevor Seward

        

This post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.


Tuesday, October 20, 2015 7:56 PM

Thanks, how can I get the item_index?


Tuesday, October 20, 2015 8:02 PM | 1 vote

Probably an easier way to do this:

$item = $list.GetItems("Title of item")

Trevor Seward

        

This post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.


Tuesday, October 20, 2015 11:23 PM

Trevor, I tried what you suggested, but I got the following error when I run BreakRoleInteritance. Please advice.

$item = $list.GetItems("Title of item")

$item.BreakRoleInheritance($false)

Error:

Method invocation failed because [Microsoft.SharePoint.SPListItemCollection] doesn't contain a method named 'BreakRoleInheritance'.
At line:1 char:27

  • $item.BreakRoleInheritance <<<< ($false)

Wednesday, October 21, 2015 9:11 AM

Hi,

Per my test, the following command is working:

$web = Get-SPWeb http://webUrl
$list = $web.Lists["List_Title"]
$item = $list.GetItems("Item Title")
$item.BreakRoleInheritance($true)

eg:

$web = Get-SPWeb "http://sp"
$list = $web.Lists["ct1"]
$item = $list.GetItems("aa")
$Item.BreakRoleInheritance($true) 

Best Regards,

Lisa Chen

TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected].


Wednesday, October 21, 2015 12:53 PM

I copied exactly what you have except I have "$false" instead of "$true", but I still get the same error message.

I used C# instead Powershell and the BreakRoleInheritance works. Any idea?

Thanks in advance.


Wednesday, October 21, 2015 5:16 PM

You are trying to use a filename to get a list item. But there is a difference between the SPListItem object and the SPFile object. Try to get the list item with the URL, using the SPWeb.GetListItem method.

$filename = "myfile.xml"

$listname = "myList"

$url = $web.serverrelativeurl + "/" + $listname + "/" + $filename
$web.GetListItem($url)

If there are subfolders you will need to adjust but, this will give you the list item based on the file name.

Lance


Thursday, October 22, 2015 2:32 AM

Hi wkpli,

I can't reproduce your issue.

$web = Get-SPWeb http://webUrl
$list = $web.Lists["List_Title"]
$item = $list.GetItems("Item Title")
$item.BreakRoleInheritance($true)

1. Change the http://webUrl to the url of your site.

2. Change the "List_Title" to the title of your list.

3. Change the "Item Title" to the title of a item in the list.

4. You can use both "$true" or "false".

eg: I have a site: "http://sp" which contains a list named "ct2", there is a item which is "aa", I run the following command, the "aa" will stop inheriting permissions from the "ct2":

If the issue still exists, Please offer a screenshot about your list and the command in SharePoint 2013 Management Shell.

Best Regards,

Lisa Chen

TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected].