Accessing an SPItem via full URL

Accessing an SPItem via full encoded URL

One of the most common tasks is to access a document; heck isn’t that half of what SharePoint is about? Well in PowerShell it’s actually not obvious how to do it. I’ve used embarrassingly iterative hacks in the past, but my friend Brett Parker figured it out. Here’s the solution:

$docurl = "http ://sharepoint.com/sites/mySite/myWeb/myList/SpecificFileName.xlsx"
$site = New-Object Microsoft.SharePoint.SPSite($docurl)
$web = $site.OpenWeb()
$item = $web.GetListItem($docurl)
$list = $item.ParentList

$item now has the SPListItem, and you’re good to go.

Note that the SPDocumentLibrary is derived from the parentList property of the item.

Interestingly, the $site.openWeb() doesn’t open the RootWeb of the SPSite, but the actual web of the URL, which is great.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *