0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でDataverseのWebリソースを取得してみた

Posted at

はじめに

PowerAppsのモデル駆動の設定が面倒なのでC#からDataverseを操作してみたくなった。
今回はdataverseのHTMLのWebリソースを一覧取得するC#のコードを動かしてみる。

前提

参照ライブラリやCreateCrmService関数の中身は以前の以下の記事を参考に

C#でDataverseに接続してテーブル一覧を表示してみた
https://qiita.com/akihiroe_/items/2db8e2634d7962ef0df7

コード

Webリソースはwebresourceテーブル上に保持されています。実際のコンテンツはcontent属性にBASE64エンコードされています。HTMLはテキストですがJPEGなどの画像もありバイナリデータの可能性があります。
個々のコンテンツがはどのようなものかはwebresourcetype属性で確認できます。

var organizationService = CreateCrmService(user, password, url);

var tableName = "webresource";
var columns = new string[] { "name", "content", "webresourcetype" };
QueryExpression query = new QueryExpression(tableName);
query.ColumnSet.AddColumns(columns);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "webresourcetype";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(1); //	Webpage (HTML)
FilterExpression filter = new FilterExpression();
filter.Conditions.Add(condition);
query.Criteria.AddFilter(filter);

var entities = organizationService.RetrieveMultiple(query);
foreach (var entity in entities.Entities)
{
    var name = entity.GetAttributeValue<string>("name");
    var content = entity.GetAttributeValue<string>("content");
    var contentAsText = Encoding.UTF8.GetString(Convert.FromBase64String(content));

    Console.WriteLine(name);
    Console.WriteLine(contentAsText.Substring(0, Math.Min(100, contentAsText.Length)));
    Console.WriteLine();
}

*webresourcetypeの値とコンテンツの種類

webresourcetype値 コンテンツ種類
1 Webpage (HTML)
2 Style Sheet (CSS)
3 Script (JScript)
4 Data (XML)
5 PNG format
6 JPG format
7 GIF format
8 Silverlight (XAP)
9 Style Sheet (XSL)
10 ICO format
11 Vector format (SVG)
12 String (RESX)

おわりに

ちょっとした文言の追加などはWebリソースのHTMLを使うことがありますが、コンテンツがBase64にエンコードされているのでリソースを一括検索しにくいんですよね。

参照情報

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?