1

I have a SharePoint 2010 documents library that contains about 50 .aspx pages. I want to break inheritance on about 6 pages and set them to specific permissions.

I am able to get a page by numerical index but not by indexing with the page name. I know I can iterate through all the pages and look for a matching name/title field, but I was wondering if I was missing a way to use the known page name directly as an indexer.

For example:

$site = Get-SPSite http://website
$web  = $site.RootWeb
$library = $web.Lists["MyPages"]

$page1 = $library.items[0]
$page2 = $library.items[12]
$page3 = $library.items[15]
etc.

If page1 is MyPage1.aspx I was looking for a way to get it like this:

$page1 = $library.items["MyPage1.aspx"] 
(or something similar).
2
  • I did try that, and it didn't work, but now that I look at it closer, all the Titles are empty/null. I tried to set the title to the value in the DisplayName field (which is MyPage1 for the file MyPage1.aspx) but it says Title is a read-only field. Commented Dec 16, 2013 at 23:14
  • Forget my comment. Check my answer below. Commented Dec 17, 2013 at 6:13

2 Answers 2

1

If you want to get only one item using the file name without looping all the items you can use CAML.

Here is the script for your case:

$query = New-Object Microsoft.SharePoint.SPQuery
$caml ='<Where><Eq><FieldRef Name="FileLeafRef"/><Value Type="File">MyPage1.aspx</Value></Eq></Where>'
$query.Query = $caml
$query.RowLimit = 1
$items = $list.GetItems($query)
$item = $items[0]
0

The CAML option above works, but I ended up using the Where-Object Operator. I pipe the collection of all pages and pick out the one that matches.

$site = Get-SPSite http://website
$web  = $site.RootWeb
$library = $web.Lists["MyPages"]
$pages = $library.items

$page1 = $pages | where { $_.Name -match "MyPage1.aspx"}
$page4 = $pages | where { $_.Name -match "MyPage4.aspx"}

$page1.BreakRoleInheritance($true)
$page4.BreakRoleInheritance($true)

etc.
etc.
2
  • 2
    Very expensive compared to the CAML, but a bit easier markup :) Commented Dec 17, 2013 at 14:34
  • I figured it was probably expensive. The other option I considered for longer term use, was to wrap that CAML into a function in order to improve the mark up. Commented Dec 17, 2013 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.