如何使用PowerShell修改SharePoint列表中的项目值

时间:2012-06-20 21:20:06

标签: sharepoint powershell

如何使用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

2 个答案:

答案 0 :(得分:9)

以下是您可以尝试执行的操作的摘要:

$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

答案 1 :(得分:0)

使用powershell我完全使用来自的脚本 social.msdn.microsoft.com/Forums

以下是示例代码,Tyepe&#34; YES&#34;一旦提示就继续。 并替换唯一的两个变量:(urlToTheYourSite&amp; YourListNameToDelete)

i got the solution.successfully done.guys no need to serialize the list objects again.I can post the codes here.No need to return as string.
//controller





  public List<getallaccountDto> getallcustomer()
            {
                DataTable dt = new DataTable();
                CustomerService _customerService = new CustomerService();
                dt = _customerService.Getallcustomer();
                return _customerService.GetTableRows(dt);
            }

    //model dto

 public class GetAllCustomersDto
    {
        public List<getallaccountDto> accounts { get; set; }
    }
    //business layer

    //modified prince


   public List<getallaccountDto> GetTableRows(DataTable dtData)
        {
            List<getallaccountDto> customerList = new List<getallaccountDto>();
            getallaccountDto customer = null;        
                foreach (DataRow dr in dtData.Rows)
                {
                customer = new getallaccountDto();
                customer.Name = dr["FirstName"].ToString();
                customer.Phone = dr["PhoneNumber"].ToString();
                customer.Balance = dr["Balance"].ToString();
                customer.company = dr["CompanyName"].ToString();
                customer.Email = dr["Email"].ToString();
                customer.CustomerID = dr["CustomerID"].ToString();
                customerList.Add(customer);
            }                

            return customerList;
        }

   // this will return correct json responses.i have tested it with postman and swagger...successfully done;






 [
      {
        "CustomerID": "1",
        "Name": "gateway",
        "Phone": "",
        "Balance": "-10.0000",
        "company": "gateway",
        "Email": "1gateway@Sipkernel.com"
      },
      {
        "CustomerID": "2",
        "Name": "IPsmarx-Office",
        "Phone": "",
        "Balance": "20.0000",
        "company": "IPsmarx-Office",
        "Email": "office@ipsmarx.com"
      },
      {
        "CustomerID": "3",
        "Name": "khan",
        "Phone": "",
        "Balance": "23.0000",
        "company": "Test",
        "Email": "srk@king.com"
      }]