LoginSignup
1
3

More than 5 years have passed since last update.

PowerShell で CSOM (Client Side Object Model) を使ったメモ

Last updated at Posted at 2018-08-27

色々と忘れるので個人的メモ

前提条件

  • ClientContext は $ctx として取得済みとする

PowerShell で直接とれない情報をラムダを使って頑張る

$clientAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$clientRuntimeAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$assemblies = ($clientAssembly.FullName, $clientRuntimeAssembly.FullName)
Add-Type -Language CSharp -ReferencedAssemblies $assemblies -TypeDefinition "
using Microsoft.SharePoint.Client;

public static class Helper
{
    public static void LoadFieldValueAsText(ClientContext ctx, ListItemCollection listItems)
    {
        ctx.Load(listItems, items => items.Include(item => item.FieldValuesAsText));
    }
    public static void LoadFieldValueAsHtml(ClientContext ctx, ListItemCollection listItems)
    {
        ctx.Load(listItems, items => items.Include(item => item.FieldValuesAsHtml));
    }
    public static void LoadListDisplayFormUrl(ClientContext ctx, List list)
    {
        ctx.Load(list, v => v.DefaultDisplayFormUrl);
    }
}
"
# リストの DisplayFormUrl を取得
$list = $ctx.Web.Lists.GetByTitle('リスト名')
[Helper]::LoadListDisplayFormUrl($ctx, $list)

# アイテムコレクションの FieldValueAsText を取得
$caml = New-Object Microsoft.SharePoint.Client.CamlQuery
$caml.ViewXml = '<View><Query></Query></View>'
$items =  $list.getItems($caml)
[Helper]::LoadFieldValueAsText($ctx, $items)

# 実行
$ctx.ExecuteQuery()

SharePoint Online の調整機能を回避する

SharePoint Online では、HTTP アクセスの一部をブロックして負荷調整する機能があるらしく、
一括移行のテストを行っていたら見事に引っかかったので対策した。

参考: https://blogs.technet.microsoft.com/sharepoint_support/2018/07/23/sharepoint-online-http-throttling/

.NETを呼んで頑張る
function Add-UserAgent($ctx)
{
    $clientAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $clientRuntimeAssembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    $assemblies = ($clientAssembly.FullName, $clientRuntimeAssembly.FullName)
    Add-Type -Language CSharp -ReferencedAssemblies $assemblies -TypeDefinition @"
    using System; 
    using Microsoft.SharePoint.Client;

    public static class SPContextHelper
    {
        public static void AddUserAgent(ClientContext ctx)
        {
            ctx.ExecutingWebRequest += delegate (object sender, WebRequestEventArgs e)
            {
                e.WebRequestExecutor.WebRequest.UserAgent = "NONISV|Artisan|DummyScript/1.0";
            };
        }
    }
"@
    [SPContextHelper]::AddUserAgent($ctx);
}

# 調整またはブロックを回避するために User-Agent を付与
Add-UserAgent $ctx
PowerShellだけで頑張る
$ctx.add_ExecutingWebRequest({
    param($Source, $EventArgs)
    $request = $EventArgs.WebRequestExecutor.WebRequest
    $request.UserAgent = "NONISV|Artisan|DummyScript/1.0"
})
$ctx.ExecuteQuery()
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3