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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我在powershell中只有URL时,如何获取SPItem对象相关的知识,希望对你有一定的参考价值。

因此,我在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
}

我该怎么取代“什么?”实现我的目标,让项目作为一个对象,然后我可以调用输出项目的标题?

答案

我会去那样的事情:

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
}

而你得到了你的名字:)

另一答案

这是我解决它的方式:

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

以上是关于当我在powershell中只有URL时,如何获取SPItem对象的主要内容,如果未能解决你的问题,请参考以下文章

C# 中未获取 Powershell 结果集

Ruby on Rails 如何获取当前 URL?

c# - 如何在运行时从模块代码中获取 PowerShell 模块版本

当我从实时数据库读取数据时,它只获取用户名并将 imageurl 获取为“null”但是我在实时数据库中有 url

如何在反应中从http url获取id

当我在 url 中手动添加参数时,角度路由不起作用