トップ回答者
PowerShellで、SharePoint Online のアイテムを削除したい!!

質問
-
前提・実現したい事
---
・PowerShellを使い、SharePoint Online上にあるリストのアイテムを削除したいです。
【例】
リスト名:都道府県
列名1:東日本 or 西日本
列名2:地方
列名3:都道府県【実データ例】
リスト名:都道府県
列名1:東日本,東日本,西日本・・・
列名2:関東,関東,四国・・・
列名3:東京,千葉,香川・・・等、全47都道府県が入力された「都道府県リスト」があるとします。
【ビュー】*全ビュー、"ID"も表示させる。
1.すべてのアイテム(既定のビュー)
2.東日本
3.西日本
4.北海道地方
5.東北地方
6.関東地方
7.中部地方
・
・
・
11.九州・沖縄地方
★ここで、「中部地方」のみ削除したいとします。★PowerShellで作成したバッチを起動し、
①ビューを「中部地方」に切り替える。
②ビュー上の中部地方データのみ削除。
を行いたいですが、どの様にコードを記述するか分からず、ご質問させて頂きました。発生している問題・Error Message
---
・ビューの内部データは取得出来ていると思うのですが、ビューの切替(変更)の仕方が分かりません。
実行結果は、「すべてのアイテム」の物になります。★★そもそも、PowerShellを使いビューの変更は出来ますでしょうか?
★★ビューを変更して削除しなくても、「ビュー:すべてのアイテム」 から、
対象の項目・値だけを指定して削除は出来ますでしょうか。よろしくお願い致します。
実際のソースコード
---
途中まで記述したコードになります。############################################### # ビューを変更し、アイテム削除 ############################################### # SDK の読み込み [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null # 取得する SharePoint Online の URL $url = "SharePoint Online のURL" $accountName = "アカウント名" $passWord = ConvertTo-SecureString -AsPlainText -Force "パスワード" # SharePoint Online 認証情報 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($accountName, $passWord) # SharePoint Client Context インスタンスを生成 $context = New-Object Microsoft.SharePoint.Client.ClientContext($url) $context.Credentials = $credentials # リストを取得 $listName = "都道府県" $list = $context.Web.Lists.GetByTitle($listName) # ビュー取得 $viewName = "すべてのアイテム" $view.DefaultView = $true $view = $list.Views.GetByTitle($viewName) #-----------------------------------------------# ①ビューを「中部地方」に切り替えるコードを書く。 ・ ・ ・ #-----------------------------------------------# ②ビュー上の中部地方データのみ削除。 ・ ・ ・ #-----------------------------------------------# # ビュー情報更新 $context.Load($view) $context.ExecuteQuery() # ビューフィールド更新 $viewFields = $view.ViewFields $context.Load($viewFields) # 更新 $context.ExecuteQuery() $context.Dispose()
試した事
---
様々なキーワードで検索し、似たようなページを参考にしましたが上手く動作しませんでした。
【検索キーワード】
「PowerShell SharePoint ビュー」,「PowerShell SharePoint view」,「PowerShell SharePoint ビュー 変更」
「PowerShell SharePoint ビュー 編集」・・・。
補足情報(言語/FW/ツール等のVersion)
---
【動作環境】
・Window 7
・PowerShell Version 5.0
・SharePoint Online(Office365)
上記、コードを実行し取得した「$view」と「$context」【$view】
Aggregations :
AggregationsStatus : Off
BaseViewId : 1
ContentTypeId : 0x
DefaultView : True
DefaultViewForContentType : False
EditorModified : False
Formats :
Hidden : False
HtmlSchemaXml : <View Name="{XXXX-XXXX-XXXX-XXXX-XXXX}" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" Type="HTML" DisplayName="すべてのアイテム" Url="○○・・・/AllItems.aspx" Level="1" BaseViewID="1" ~・・・
Id : XXXX-XXXX-XXXX-XXXX-XXXX
ImageUrl : ○○・・・
IncludeRootFolder : False
ViewJoins :
JSLink : clienttemplates.js
ListViewXml : <View Name="{XXXX-XXXX-XXXX-XXXX-XXXX}" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" Type="HTML" ~・・・
Method :
MobileDefaultView : True
MobileView : True
ModerationType :
OrderedView : False
Paged : True
PersonalView : False
ViewProjectedFields :
ViewQuery : <OrderBy><FieldRef Name="ID" /></OrderBy>
ReadOnlyView : False
RequiresClientIntegration : False
RowLimit : 30
Scope : DefaultValue
ServerRelativePath : Microsoft.SharePoint.Client.ResourcePath
ServerRelativeUrl : /○○/AllItems.aspx.
StyleId :
TabularView : True
Threaded : False
Title : すべてのアイテム
Toolbar :
ToolbarTemplateName :
ViewType : HTML
ViewData :
ViewFields : {ID, LinkTitle, Text, name...}
VisualizationInfo :
Context : Microsoft.SharePoint.Client.ClientContext
Tag :
Path : Microsoft.SharePoint.Client.ObjectPathIdentity
ObjectVersion :
ServerObjectIsNull : False
TypedObject : Microsoft.SharePoint.Client.View【$context】
Web : Microsoft.SharePoint.Client.Web
Site : Microsoft.SharePoint.Client.Site
RequestResources : Microsoft.SharePoint.Client.RequestResources
FormDigestHandlingEnabled : True
ServerVersion : ○.○.○.○
Url : サイトのURL
ApplicationName : .NET Library
ClientTag :
DisableReturnValueCache : False
ValidateOnClient : True
AuthenticationMode : Default
FormsAuthenticationLoginInfo :
Credentials : Microsoft.SharePoint.Client.SharePointOnlineCredentials
WebRequestExecutorFactory : Microsoft.SharePoint.Client.DefaultWebRequestExecutorFactory
PendingRequest : Microsoft.SharePoint.Client.ClientRequest
HasPendingRequest : True
Tag :
RequestTimeout : 180000
StaticObjects : {[Microsoft$SharePoint$SPContext$Current, Microsoft.SharePoint.Client.RequestContext]}
ServerSchemaVersion : ○.○.○.○
ServerLibraryVersion : ○.○.○.○
RequestSchemaVersion : ○.○.○.○
TraceCorrelationId : XXXX-XXXX-XXXX-XXXX-XXXX- 編集済み mie.8 2017年5月17日 0:09 修正1
回答
-
リストにはすでに「中部地方」というビューが作成されているということでしょうか?
そういうことであれば、下記のような手順でビューで表示されるアイテムのみを取得できるかと思います。# リストを取得 $listName = "都道府県" $list = $context.Web.Lists.GetByTitle($listName) # ビューを取得 $viewName = "中部地方" $view = $list.Views.GetByTitle($viewName) $context.load($view) $context.ExecuteQuery() # ビューを基にアイテムを取得 $query = New-Object Microsoft.SharePoint.Client.CamlQuery $query.ViewXml = $view.ListView $items = $list.GetItems($query) $context.load($items) $context.ExecuteQuery()
- 編集済み OTA HirofumiMVP 2017年5月16日 8:31 コード修正
- 回答としてマーク mie.8 2017年5月17日 1:40
すべての返信
-
リストにはすでに「中部地方」というビューが作成されているということでしょうか?
そういうことであれば、下記のような手順でビューで表示されるアイテムのみを取得できるかと思います。# リストを取得 $listName = "都道府県" $list = $context.Web.Lists.GetByTitle($listName) # ビューを取得 $viewName = "中部地方" $view = $list.Views.GetByTitle($viewName) $context.load($view) $context.ExecuteQuery() # ビューを基にアイテムを取得 $query = New-Object Microsoft.SharePoint.Client.CamlQuery $query.ViewXml = $view.ListView $items = $list.GetItems($query) $context.load($items) $context.ExecuteQuery()
- 編集済み OTA HirofumiMVP 2017年5月16日 8:31 コード修正
- 回答としてマーク mie.8 2017年5月17日 1:40