2013-07-11 83 views
0

我需要的是使用Powershell SharePoint.Client更新每個第二個項目。 我用這裏的一些代碼http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/15/using-powershell-to-get-data-from-a-sharepoint-2010-list.aspx 並添加了所需的功能。我在Powershell中很新,所以我可能犯了一些愚蠢的錯誤。你會檢查我的代碼,並指出那個錯誤。SharePoint 2010使用Powershell SP.Client更新每個Socond列表項目

Add-Type -path C:\Microsoft.SharePoint.Client.dll 
Add-Type -path C:\Microsoft.SharePoint.Client.Runtime.dll 

Function Get-SPClientList { 



$cSharp = @" 

using System; 
using System.Collections.Generic; 
using Microsoft.SharePoint.Client; 

namespace SPClient 
{ 
    public class SharePointList 
    { 
     public static ListItemCollection GetList() 
     { 
      ClientContext clientContext = new ClientContext("http://sp10sl1:55555/mychuldesite"); 
      List list = clientContext.Web.Lists.GetByTitle("CustomList"); 
      CamlQuery camlQuery = new CamlQuery(); 
      camlQuery.ViewXml = "<View/>"; 
      ListItemCollection listItems = list.GetItems(camlQuery); 
      clientContext.Load(list); 
      clientContext.Load(listItems); 
      clientContext.ExecuteQuery(); 
      return listItems; 
     } 
    } 
} 
"@ 
$assemblies = @( 
    "$PSSCriptRoot\Microsoft.SharePoint.Client.dll", 
    "$PSSCriptRoot\Microsoft.SharePoint.Client.Runtime.dll" 
    "System.Core" 
    ) 
Add-Type -TypeDefinition $cSharp -ReferencedAssemblies $assemblies 

$items = [SPClient.SharepointList]::GetList() 

$out = @() 
$i=0 
foreach ($item in $items) 
{ 
    if($i % 2 -eq 0) 
    { 
    $item["Title"] = "ClientTitle"; 
    $item["UserData"] = "ClientData"; 
    $item.Update(); 
    } 
    $i += 1 
} 
$out 
} 

在此先感謝您。

回答

0

好吧,我已經設法解決它。 我們可以簡單地在C#塊中添加更新邏輯,然後調用這個函數。

它應該看起來像這樣

Add-Type -path C:\Microsoft.SharePoint.Client.dll 
Add-Type -path C:\Microsoft.SharePoint.Client.Runtime.dll 



$cSharp = @" 


using System; 
using System.Collections.Generic; 
using Microsoft.SharePoint.Client; 
using Microsoft.SharePoint; 
namespace SPClient 
{ 
public class SharePointList 
{ 
public static ListItemCollection GetList() 
{ 
ClientContext clientContext = new ClientContext ("http://*****:****/*****"); 
List list = clientContext.Web.Lists.GetByTitle("CustomList"); 

CamlQuery camlQuery = new CamlQuery(); 
      camlQuery.ViewXml = "<View/>"; 
      ListItemCollection listItems = list.GetItems(camlQuery); 


clientContext.Load(list); 
clientContext.Load(listItems); 
clientContext.ExecuteQuery(); 
int i=0; 
foreach (ListItem item in listItems) 
{ 
if(i % 2 == 0) 
    { 
item["Title"] = "This is client Powershell"; 
item["UserData"] = "Yeap it is"; 
item.Update(); 
clientContext.ExecuteQuery(); 
} 
i++; 
} 

return listItems; 
} 
} 
} 
"@ 

$assemblies = @( 
    "$PSSCriptRoot\Microsoft.SharePoint.Client.dll", 
    "$PSSCriptRoot\Microsoft.SharePoint.Client.Runtime.dll" 
    "System.Core" 
    ) 

Add-Type -TypeDefinition $cSharp -ReferencedAssemblies $assemblies 

$its = [SPClient.SharepointList]::GetList() 

不幸的是它需要很多的時間,我完全理解這種代碼沒有任何教程,所以如果你知道這樣,請通知我。

相關問題