PageRenderTime 118ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/CKS Developer Edition-VS2012/CKS.Dev.Core.Cmd.Imp.v5/WebTemplateSharePointCommands.cs

#
C# | 210 lines | 158 code | 26 blank | 26 comment | 7 complexity | d143d9b650e8540920aaa56c41ea6061 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.SharePoint.Commands;
  6. using Microsoft.SharePoint;
  7. using CKS.Dev.Core.Cmd.Imp.v5.Properties;
  8. using Microsoft.SharePoint.Administration;
  9. #if VS2012Build_SYMBOL
  10. using CKS.Dev11.VisualStudio.SharePoint.Commands.Info;
  11. using CKS.Dev11.VisualStudio.SharePoint.Commands.Common.ExtensionMethods;
  12. #elif VS2013Build_SYMBOL
  13. using CKS.Dev12.VisualStudio.SharePoint.Commands.Info;
  14. using CKS.Dev12.VisualStudio.SharePoint.Commands.Common.ExtensionMethods;
  15. #elif VS2014Build_SYMBOL
  16. using CKS.Dev13.VisualStudio.SharePoint.Commands.Info;
  17. using CKS.Dev13.VisualStudio.SharePoint.Commands.Common.ExtensionMethods;
  18. #else
  19. using CKS.Dev.VisualStudio.SharePoint.Commands.Info;
  20. using CKS.Dev.VisualStudio.SharePoint.Commands.Common.ExtensionMethods;
  21. #endif
  22. #if VS2012Build_SYMBOL
  23. namespace CKS.Dev11.VisualStudio.SharePoint.Commands
  24. #elif VS2013Build_SYMBOL
  25. namespace CKS.Dev12.VisualStudio.SharePoint.Commands
  26. #elif VS2014Build_SYMBOL
  27. namespace CKS.Dev13.VisualStudio.SharePoint.Commands
  28. #else
  29. namespace CKS.Dev.VisualStudio.SharePoint.Commands
  30. #endif
  31. {
  32. class WebTemplateSharePointCommands
  33. {
  34. #region Methods
  35. /// <summary>
  36. /// Gets data for each Web template categories on the SharePoint site, and returns an array of
  37. /// serializable objects that contain the data.
  38. /// </summary>
  39. /// <param name="context">The command context</param>
  40. /// <returns>The categories</returns>
  41. [SharePointCommand(WebTemplateCollectionSharePointCommandIds.GetWebTemplateCategories)]
  42. private static string[] GetWebTemplateCategories(ISharePointCommandContext context)
  43. {
  44. context.Logger.WriteLine(Resources.WebTemplateSharePointCommands_TryingToRetrieveAvailableCategories, LogCategory.Status);
  45. List<string> categories = new List<string>();
  46. try
  47. {
  48. SPWebTemplateCollection webTemplates = context.Web.GetAvailableWebTemplates((uint)context.Web.Locale.LCID, true);
  49. foreach (SPWebTemplate item in webTemplates)
  50. {
  51. if(!categories.Contains(item.DisplayCategory))
  52. {
  53. categories.Add(item.DisplayCategory);
  54. }
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. context.Logger.WriteLine(String.Format(Resources.WebTemplateSharePointCommands_CategoriesRetrievingException,
  60. ex.Message,
  61. Environment.NewLine,
  62. ex.StackTrace), LogCategory.Error);
  63. }
  64. return categories.ToArray();
  65. }
  66. /// <summary>
  67. /// Gets data for each Web templates on the SharePoint site, and returns an array of
  68. /// serializable objects that contain the data.
  69. /// </summary>
  70. /// <param name="context">The command context</param>
  71. /// <param name="category">The category</param>
  72. /// <returns>The web template infos</returns>
  73. [SharePointCommand(WebTemplateCollectionSharePointCommandIds.GetAvailableWebTemplatesByCategory)]
  74. private static WebTemplateInfo[] GetAvailableWebTemplatesByCategory(ISharePointCommandContext context, string category)
  75. {
  76. context.Logger.WriteLine(Resources.WebTemplateSharePointCommands_TryingToRetrieveAvailableWebTemplates, LogCategory.Status);
  77. List<WebTemplateInfo> webTemplateInfos = new List<WebTemplateInfo>();
  78. try
  79. {
  80. SPWebTemplateCollection webTemplates = context.Web.GetAvailableWebTemplates((uint)context.Web.Locale.LCID, true);
  81. foreach (SPWebTemplate item in webTemplates)
  82. {
  83. if (item.DisplayCategory == category)
  84. {
  85. WebTemplateInfo info = new WebTemplateInfo();
  86. info.Id = item.ID;
  87. info.ImageUrl = item.ImageUrl;
  88. info.Name = item.Name;
  89. info.Description = item.Description;
  90. info.DisplayCategory = item.DisplayCategory;
  91. info.IsCustomTemplate = item.IsCustomTemplate;
  92. info.IsHidden = item.IsHidden;
  93. info.IsRootWebOnly = item.IsRootWebOnly;
  94. info.IsSubWebOnly = item.IsSubWebOnly;
  95. info.Lcid = item.Lcid;
  96. info.Title = item.Title;
  97. info.SetupPath = typeof(SPWebTemplate).InvokeMember("SetupPath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty, null, item, null) as string;
  98. webTemplateInfos.Add(info);
  99. }
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. context.Logger.WriteLine(String.Format(Resources.WebTemplateSharePointCommands_RetrievingException,
  105. ex.Message,
  106. Environment.NewLine,
  107. ex.StackTrace), LogCategory.Error);
  108. }
  109. return webTemplateInfos.ToArray();
  110. }
  111. /// <summary>
  112. /// Gets additional property data for a specific web template.
  113. /// </summary>
  114. /// <param name="context">The context</param>
  115. /// <param name="nodeInfo">The node info</param>
  116. /// <returns>The properties</returns>
  117. [SharePointCommand(WebTemplateSharePointCommandIds.GetWebTemplateProperties)]
  118. private static Dictionary<string, string> GetWebTemplateProperties(ISharePointCommandContext context,
  119. WebTemplateInfo nodeInfo)
  120. {
  121. SPWebTemplate template = context.Web.GetAvailableWebTemplates((uint)context.Web.Locale.LCID, true)[nodeInfo.Name];
  122. return SharePointCommandServices.GetProperties(template);
  123. }
  124. /// <summary>
  125. /// Gets the web templates.
  126. /// </summary>
  127. /// <param name="context">The context.</param>
  128. /// <returns>The web template infos</returns>
  129. [SharePointCommand(WebTemplateCollectionSharePointCommandIds.GetWebTemplates)]
  130. private static WebTemplateInfo[] GetWebTemplates(ISharePointCommandContext context)
  131. {
  132. context.Logger.WriteLine(Resources.WebTemplateSharePointCommands_TryingToRetrieveAvailableWebTemplates, LogCategory.Status);
  133. List<WebTemplateInfo> webTemplateInfos = new List<WebTemplateInfo>();
  134. try
  135. {
  136. SPSite caSite = null;
  137. SPWeb rootWeb = null;
  138. if (context.Site == null)
  139. {
  140. //Do this as from item templates the site is always null
  141. SPAdministrationWebApplication caWebApp = SPAdministrationWebApplication.Local;
  142. caSite = caWebApp.Sites[0];
  143. rootWeb = caSite.RootWeb;
  144. }
  145. else
  146. {
  147. caSite = context.Site;
  148. rootWeb = context.Web;
  149. }
  150. SPWebTemplateCollection webTemplates = caSite.GetWebTemplates((uint)rootWeb.Locale.LCID);
  151. foreach (SPWebTemplate item in webTemplates)
  152. {
  153. //Check the temaplate is a site defintion and has a display category
  154. if (!String.IsNullOrEmpty(item.DisplayCategory) && !item.IsCustomTemplate)
  155. {
  156. WebTemplateInfo info = new WebTemplateInfo();
  157. info.Id = item.ID;
  158. info.ImageUrl = item.ImageUrl;
  159. info.Name = item.Name;
  160. info.Description = item.Description;
  161. info.DisplayCategory = item.DisplayCategory;
  162. info.IsCustomTemplate = item.IsCustomTemplate;
  163. info.IsHidden = item.IsHidden;
  164. info.IsRootWebOnly = item.IsRootWebOnly;
  165. info.IsSubWebOnly = item.IsSubWebOnly;
  166. info.Lcid = item.Lcid;
  167. info.Title = item.Title;
  168. info.SetupPath = typeof(SPWebTemplate).InvokeMember("SetupPath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty, null, item, null) as string;
  169. webTemplateInfos.Add(info);
  170. }
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. context.Logger.WriteLine(String.Format(Resources.WebTemplateSharePointCommands_RetrievingException,
  176. ex.Message,
  177. Environment.NewLine,
  178. ex.StackTrace), LogCategory.Error);
  179. }
  180. return webTemplateInfos.ToArray();
  181. }
  182. #endregion
  183. }
  184. }