前段時間做了個winform程序,去管理kentico網站的content,包括content節點的增刪改查,以及相應節點內容的修改。現在將對content的操作方法簡單的介紹一下。
我們想要操作kentico網站的content(如下圖),操作步驟如下。
一、引入kentico相關的dll文件,配置數據庫鏈接環境
不直接使用sql查詢語句去操作,而是使用kentico cms的方法去直接操作,我們需要引入kentico cms系統中的dll文件,這些文件在kentico cms網站的/bin目錄下面可以找到,就是一些前綴是CMS.的文件,如下圖,下圖只是一部分:
接下來就是需要在配置數據庫連接,在app.config中添加配置代碼如下:
1 <connectionStrings> 2 <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=expert_fi_7;server=.\SQLEXPRESS;user id=sa;password=******;Current Language=English;Connection Timeout=240;" /> 3 </connectionStrings>
該配置連接的name必須為CMSConnectionString,這樣才能有cms類庫自動的識別。
二、初始化kentico CMS環境
引入命名空間:using CMS.CMSHelper
在需要使用content操作的代碼前加上
1 CMSContext.Init();
三、操作content節點
對節點的操作無非就是增、刪、改、查這幾種操作,下面一一介紹
- 查詢操作
1 TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);//獲取當前content樹對象 2 3 var node = tree.SelectSingleNode(CMSContext.CurrentSiteName 4 , "/Tuotteet" //該處為Alias path,見下圖 5 , "en-us",true); //獲取節點Tuotteet 6 7 value = node.GetValue(columnNames["propertyName"]).ToString(); //獲取某一屬性值 8 //【注】該node不能直接用node.Children去獲取,node.Children獲取的只是最基本的信息 9 10 //獲取指定節點所有屬性名 11 DataClassInfo dci = DataClassInfoProvider.GetDataClass(node.NodeClassName); 12 var columnNames=new List<string>(); //創建屬性名對象 13 if (dci != null) 14 { 15 CMS.FormEngine.FormInfo fi = new CMS.FormEngine.FormInfo(dci.ClassFormDefinition); 16 columnNames = fi.GetColumnNames(); 17 } 18 19 //獲取所有子節點 20 var childrenNodes=node.Children
如果你想獲取所有子節點的屬性,你可以遍歷childrenNodes,然后使用SelectSingleNode(……)的方法去獲取指定子節點的對象,然后去獲取它的屬性。
對於上面提到的Alias path,見下圖示例: - 添加節點
1 public void AddNode(string nodeAliasPath,List<string[]> categorys ) 2 { 3 var parentNodeAliasPath = nodeAliasPath.Substring(0, nodeAliasPath.LastIndexOf('/')); 4 var tree = new TreeProvider(CMSContext.CurrentUser); 5 var parentNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, parentNodeAliasPath, "fi-FI", true); 6 var className = categorys[1][1]; 7 TreeNode newNode = TreeNode.New(className, tree); 8 newNode.DocumentName = categorys[1][0];//設置節點名 9 newNode.DocumentCulture = "fi-FI";//設置節點的document環境 10 for (int i = 2; i < categorys[0].Length - 2; i++) 11 { 12 newNode.SetValue(categorys[0][i], categorys[1][i]);//為節點添加新的屬性,SetValue(屬性名,值) 13 } 14 DocumentHelper.InsertDocument(newNode, parentNode, tree); 15 }
這里要注意的是必須給節點新節點添加屬性,不然該節點在添加后不會顯示出來
- 刪除節點
1 public void DeleteNode(string nodeAliasPath) 2 { 3 var tree = new TreeProvider(CMSContext.CurrentUser); 4 var deleteNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, nodeAliasPath, "fi-FI", true); 5 if (deleteNode != null) 6 { 7 DocumentHelper.DeleteDocument(deleteNode, tree, true, true, true); 8 } 9 }
關於DeleteDocument的說明,如下圖
- 修改節點內容
1 public void UpdateNode(string nodeAliasPath,List<string[]> categorys ) 2 { 3 var tree = new TreeProvider(CMSContext.CurrentUser); 4 var updateNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, nodeAliasPath, "fi-FI", true); 5 updateNode.DocumentName = categorys[1][0]; 6 for (int i = 1; i < categorys[0].Length-1;i++ ) 7 { 8 updateNode.SetValue(categorys[0][i], categorys[1][i]); 9 } 10 DocumentHelper.UpdateDocument(updateNode, tree); 11 }
當然,還有很多節點的屬性你可以修改,例子代碼沒有列出來。
就以上的這些內容,基本可以去簡單操作kentico cms中的content內容了,歡迎大家討論。
參考:http://devnet.kentico.com/Documentation.aspx
珍惜作者勞動成果,如須轉載,請說明出處。