Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to parse any given SharePoint URL and get back the site, web and list references using the code below. I would like to achieve this even if I provide the URL of an item. The code below works if i provide the URL of the list/library. If i provide the URL of an item in a list/library it throws an exception but also appears to work.

I do not understand why I am getting an exception when using an Item URL. What else do i need to do to make this work for any URL?

Code -

$listURL = "http://portal.contoso.com/sites/TEST/Shared%20Documents/Forms/AllItems.aspx"
$listURLWithItem= "http://portal.contoso.com/sites/TEST/Shared%20Documents/Test2.docx"
$site = New-Object Microsoft.SharePoint.SPSite($listURL)
$web = $site.OpenWeb()
$list = $web.GetListFromWebPartPageUrl($listURL)

"Output with list url-"
$site.url
$web.url
$list.Title

"Output when using item url-"
$site = New-Object Microsoft.SharePoint.SPSite($listURLWithItem)
$web = $site.OpenWeb()
$list = $web.GetListFromWebPartPageUrl($listURLWithItem)
$site.url
$web.url
$list.Title

The resulting output - with the second instance generating an exception

Output with list url-
http://portal.contoso.com/sites/TEST
http://portal.contoso.com/sites/TEST
Shared Documents

Output when using item url-
Exception calling "GetListFromWebPartPageUrl" with "1" argument(s): "Cannot complete this action.

Please try again."
At line:15 char:39
+ $list = $web.GetListFromWebPartPageUrl <<<< ($listURLWithItem)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

http://portal.contoso.com/sites/TEST
http://portal.contoso.com/sites/TEST
Shared Documents
share|improve this question
    
After exception you are getting correct result? –  Aanchal Jun 30 '14 at 4:43
    
The reason you are getting the correct value is because the list object is still holding the value from the previous computation. The second part where you are trying get list name with item url has not got evaluated. –  Mp Arvind Oct 28 '14 at 5:08

3 Answers 3

The page url you must provide is the url to the webpart page. The url you provide is the url to de actual document.

For example an url like

http://portal.contoso.com/sites/TEST/Shared%20Documents/Forms/DispForm.aspx?ID=1

should work.

SPList GetListFromWebPartPageUrl(string pageUrl)

pageUrlType: System.String
A string that contains the site-relative URL of a Web Part page, for example, /Lists/Announcements/AllItems.aspx.

See MSDN documentation for more information

share|improve this answer
    
The input URL should ideally be that of the list or library however i am trying to make it more generic. It seems i have to write some more logic to parse the URL to accept any URL. I do get a correct result even after the exception. try{$list = $web.GetListFromWebPartPageUrl($listURLWithItem)} catch{"Unable to get list."} $site.url $web.url $list.Title –  Ron Jul 20 '14 at 6:08

Please try the below script. I have tried to get the listitem object out of the url. A check is necessary if your Urls contain relative urls as well.

$site = New-Object Microsoft.SharePoint.SPSite($listURL)
$web = $site.OpenWeb()
$listitem=$web.GetListItem($listURL);
"Output with list url-"
$site.url
$web.url
$listitem.ParentList.Title

$site = New-Object Microsoft.SharePoint.SPSite($listURLWithItem)
$web = $site.OpenWeb()
$listitem=$web.GetListItem($listURLWithItem);
"Output with list url-"
$site.url
$web.url
$listitem.ParentList.Title
share|improve this answer

I had to check the URL and then call the appropriate method. Below is the script I finally put together and it is working for me so far.

if( $listURL.ToLower().Contains( "AllItems.aspx".ToLower() ) -or $listURL.ToLower().Contains( "DispForm.aspx".ToLower() ) )
{
    $site = New-Object Microsoft.SharePoint.SPSite($listURL)
    $web = $site.OpenWeb()

    $list=$web.GetListFromWebPartPageUrl($listURL);
    "Output with list url-"
    $site.url
    $web.url
    $list.Title
}
else
{
    $site = New-Object Microsoft.SharePoint.SPSite($listURL)
    $web = $site.OpenWeb()

    # Use GetListItem when URL contains list item, example - http://portal.contoso.com/Shared%20Documents/test.docx
    $listItem=$web.GetListItem($listURL);
    "Output with list item url-"
    $site.url
    $web.url
    $listItem.ParentList.Title
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.