【问题标题】:How to modify an item's value in a SharePoint list with PowerShell如何使用 PowerShell 修改 SharePoint 列表中的项目值
【发布时间】:2012-06-23 02:38:45
【问题描述】:

如何使用 PowerShell 修改 SharePoint 列表中的项目值?当我尝试以下操作时:

$splist.GetItems() | ForEach-Object{
 #Write-Host $_["Item"]

 if($_["Item"] -eq $null){
  $SPFileCollection = $folder.Files
  $upFile=Get-ChildItem D:\Tools\SP-scripts\MDNSO-template.aspx
  $tmpValue = $_["Title"]
  $filename = "EM_Doc_Library\$tmpValue"
  $SPFileCollection.Add("EM_Doc_Library\$tmpValue",$upFile.OpenRead(),$false)
  Write-Host "creating the file"
  Write-Host $_["Title"]
  $_["Item"] = "MDNSO-<$tmpValue>"
 }
}

上面写着unable to index into an object of type SPlistItem

【问题讨论】:

  • 哪一行报错?
  • 函数Add(),它说异常调用Add with 3 arguments
  • 你能帮帮我吗?我可以弄清楚几个小时!
  • 试试$SPFileCollection.Folders["EM_Doc_Library"].Add("$tmpValue",$upFile.OpenRead(),$false)
  • 是说后面缺少表达式,

标签: sharepoint powershell


【解决方案1】:

以下是您可以尝试做的总结:

$spWeb = Get-SPWeb -Identity http://yourdomain/sites/config
$spList = $spWeb.Lists["AppSettings"]
$spItem = $spList.GetItemById(10013) //or another way that you prefer.
$spItem["Name"] = "MyName"
$spItem.Update()

有关更多信息,您应该查看此博客文章。它包含如何使用 PowerShell 添加、更新和删除列表项的示例:http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html

【讨论】:

  • 请提供链接包含的内容摘要。
  • 别忘了结束它:$spItem.Update()
  • 另外最好在最后调用 $spWeb.Dispose() 来释放内存。但是,如果你在运行脚本后关闭 PS 窗口并且没有循环打开网页,你可以保持原样。
【解决方案2】:

使用 powershell,我使用来自的脚本非常完美地做到了 social.msdn.microsoft.com/Forums

这里是示例代码,一旦出现提示,输入“YES”继续。 并替换唯一的两个变量: (urlToTheYourSite & YourListNameToDelete)

write-host "This will delete data, type YES to continue"
$retval = read-host 
if ($retval -ne "YES") 
{
    write-host "exiting - you did not type yes" -foregroundcolor green
    exit
}
write-host "continuing"

$web = get-spweb https://urlToTheYourSite
$list = $web.lists | where {$_.title -eq "YourListNameToDelete"}
Write-host "List $($list.title) has $($list.items.count) entries"
$items = $list.items

foreach ($item in $items)

{
    Write-host "  Say Goodbye to $($item.id)" -foregroundcolor red

    $list.getitembyid($Item.id).Delete()
}

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2014-01-06
    相关资源
    最近更新 更多