💻

CSOM の WebTemplate には Path が入ってこない

2022/01/01に公開

どういうことかというと、以下のコードを実行します。

public static class Program
{

    private static void Main(string[] args)
    {
        var siteUrl = "{{site-url}}";
        var userName = "{{user-name}}";
        var rawPassword = "{{password}}";
        var securePassword = new SecureString();
        foreach (var c in rawPassword)
        {
            securePassword.AppendChar(c);
        }
        var credentials = new SharePointOnlineCredentials(userName, securePassword);
        var clientContext = new ClientContext(siteUrl)
        {
            Credentials = credentials
        };
        var webTemplates = clientContext.Web.GetAvailableWebTemplates(1041, true);
        clientContext.Load(webTemplates);
        clientContext.ExecuteQuery();
        var webTemplate = webTemplates.FirstOrDefault(x => x.Name == "STS#0");
        Console.ReadLine();
    }

}

はい、そうですね。Path プロパティの値が null になります。

どういうことだってばよ

CSOM では自らのオブジェクトの場所を特定するために、オブジェクトが Load された後、識別情報を Path プロパティに保持します。たとえばリスト アイテムだと以下のような値が入っています。

5e66049e-800e-4000-b805-a573a858fe86|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:217cc400-7454-40e2-8f0d-546625dfb9fb:web:564072f0-9feb-48d6-b7bc-f77e17623c87:list:00077cba-a4e8-460f-a2b3-3a96a2da523f:item:1,1

site:217c... とか web:5640... となっているのがそれぞれの親オブジェクトの ID です。Load メソッドを呼び出すときは常にこの値を見るため、WebTemplateLoad しようとすると常に例外が発生します。

Discussion