当我在powershell中只有URL时,如何获取SPItem对象

时间:2016-02-15 02:21:49

标签: powershell sharepoint

因此,我在SharePoint 2010中有多个列表/网站的URL列表。

保持这个基本,假设我有以下

$urlslist is a list of URLS eg:      
Http://my.intranet.com/site/list/document1.extention
Http://my.intranet.com/site/list/document2.extention

foreach($url in $urlslist)
{
    #here is where i need help
    $item = What?  #(GetSPItem($url) or something?)
    Write-Host $item.Title
}

我该怎样取代“什么?”实现我的目标是将项目作为对象,然后我可以调用它来输出项目的标题?

2 个答案:

答案 0 :(得分:1)

我会选择类似的东西:

foreach($url in $urlslist)
{
    $urlWeb = $url... //Manage the url to get a valid web url 
    $web = Get-SPWeb $urlWeb
    $item = $web.GetListItem($url);
    Write-Host $item.Title
}

你得到了你的名字:)

答案 1 :(得分:0)

这是我解决问题的方法:

Function Get_SP_Item_From_URL {

    Param(
        [String]$item_url
    )

    #Get the Site Collection URL:
    [string]$site_collection_url = "{0}//{1}{2}" -f $str.Split('/')

    # Get the SharePoint Site Collection Object:
    $sp_site = Get-SPSite $site_collection_url

    # Get the Relative URL from the Absolute URL,
    # then add one to the $site_collection_url length to remove the first slash from the relative url:
    [int]$site_collection_url_length = $site_collection_url.Length+1
    [string]$relative_url = $item_url.Remove(0,$site_collection_url_length)

    # Split the relative URL string into an array contacting each path segment:
    [array]$url_segments_array = $relative_URL.split("/");
    # Count the number of segments in the relative URL:
    [int]$number_of_url_segments = $url_segments_array.Count

    # Create an array of all the possible SharePoint sp-web URLs for
    [array]$url_array = @()
    [string]$url = ""
    foreach ($folder in $url_segments_array) {
        $url = $url + "/" + $folder
        $url_array += $url 
    }

    # We need to count backwards from the longest possible site url to the shortest, stopping when we get to the first sp-web.
    # The last two url segments are always the list name and then the item name, so we can skip those two:
    [int]$starting_url_segment_num = $number_of_url_segments-2

    #Start at the outermost possible site path and work backwards until a SP Web is found
    for ( $i = $starting_url_segment_num; $i -gt 0; $i--) {
        $sp_web = Get-SPWeb -Site $sp_site -Identity $url_array[$i]
        if ($sp_web.Count -eq 1) {
            Break #Exit the loop
        }
    }

    #Now get the item:
    $sp_item = $sp_web.GetListItem($item_url)
    Return $sp_item

}

$item_url = "https://sharepoint.domain.local/site/site/library/folder/File Name.docx"

$sp_item = Get_SP_Item_From_URL -item_url $item_url
# Show the item's title as a test:
Write-Host `$sp_item.Title $sp_item.Title
相关问题