PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/CKS Developer Edition-VS2012/CKS.Dev/Deployment/QuickDeployment/ISharePointProjectItemExtensions.cs

#
C# | 165 lines | 82 code | 16 blank | 67 comment | 4 complexity | 9ed224c3d73ca49ca06af718da0bc90b 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;
  6. using EnvDTE;
  7. using VSLangProj;
  8. namespace CKS.Dev.VisualStudio.SharePoint.Deployment.QuickDeployment
  9. {
  10. /// <summary>
  11. /// Extensions to the ISharePointProjectItem object.
  12. /// </summary>
  13. public static class ISharePointProjectItemExtensions
  14. {
  15. #region Methods
  16. /// <summary>
  17. /// Gets all features in the given project whether packaged or not that contain this SPI.
  18. /// </summary>
  19. /// <param name="item">The ISharePointProjectItem being extended.</param>
  20. /// <param name="project">The current project.</param>
  21. /// <returns>Returns all features in the given project.</returns>
  22. public static List<ISharePointProjectFeature> ParentFeaturesInProject(this ISharePointProjectItem item, ISharePointProject project)
  23. {
  24. List<ISharePointProjectFeature> features = new List<ISharePointProjectFeature>();
  25. features.AddRange(
  26. from feature
  27. in project.Features
  28. where feature.ProjectItems.Contains(item)
  29. select feature
  30. );
  31. return features;
  32. }
  33. /// <summary>
  34. /// Determines if this SPI is packaged in the given project directly (i.e. not as part of a feature).
  35. /// </summary>
  36. /// <param name="item">The ISharePointProjectItem being extended.</param>
  37. /// <param name="project">The current project.</param>
  38. /// <returns>Returns true if this SPI is packaged in the given project directly (i.e. not as part of a feature).</returns>
  39. public static bool IsDirectPartOfProjectPackage(this ISharePointProjectItem item, ISharePointProject project)
  40. {
  41. return project.Package.ProjectItems.Contains(item);
  42. }
  43. /// <summary>
  44. /// Determines if this SPI is packaged in any project in the solution directly (i.e. not as part of a feature).
  45. /// </summary>
  46. /// <param name="item">The ISharePointProjectItem being extended.</param>
  47. /// <param name="service">The sharepoint project service.</param>
  48. /// <returns>Returns true if this SPI is packaged in any project in the solution directly (i.e. not as part of a feature).</returns>
  49. public static bool IsDirectPartOfAnyProjectPackage(this ISharePointProjectItem item, ISharePointProjectService service)
  50. {
  51. return service.Projects.Any(p => p.Package.ProjectItems.Contains(item));
  52. }
  53. /// <summary>
  54. /// Determines if this SPI is included inside a feature that is itself packaged in the given project.
  55. /// </summary>
  56. /// <param name="item">The ISharePointProjectItem being extended.</param>
  57. /// <param name="project">The current project.</param>
  58. /// <returns>Returns true if this SPI is included inside a feature that is itself packaged in the given project.</returns>
  59. public static bool IsPartOfPackagedProjectFeature(this ISharePointProjectItem item, ISharePointProject project)
  60. {
  61. return item.ParentFeaturesInProject(project).Any(feature => feature.IsPartOfProjectPackage(project));
  62. }
  63. /// <summary>
  64. /// Determines if this SPI is included inside a feature that is packaged in any project in the solution.
  65. /// </summary>
  66. /// <param name="item">The ISharePointProjectItem being extended.</param>
  67. /// <param name="service">The sharepoint project service.</param>
  68. /// <returns>Returns true if this SPI is included inside a feature that is packaged in any project in the solution.</returns>
  69. public static bool IsPartOfAnyPackagedProjectFeature(this ISharePointProjectItem item, ISharePointProjectService service)
  70. {
  71. foreach (ISharePointProject project in service.Projects)
  72. {
  73. if (item.IsPartOfPackagedProjectFeature(project))
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. /// <summary>
  81. /// Determines if this SPI is packaged in the given project whether by feature or direct (e.g. mapped folder).
  82. /// </summary>
  83. /// <param name="item">The ISharePointProjectItem being extended.</param>
  84. /// <param name="project">The current project.</param>
  85. /// <returns>Returns true if this SPI is packaged in the given project whether by feature or direct (e.g. mapped folder).</returns>
  86. public static bool IsPartOfProjectPackage(this ISharePointProjectItem item, ISharePointProject project)
  87. {
  88. return item.IsDirectPartOfProjectPackage(project) || item.IsPartOfPackagedProjectFeature(project);
  89. }
  90. /// <summary>
  91. /// Determines if this SPI is packaged in any project in the solution whether by feature or direct (e.g. mapped folder).
  92. /// </summary>
  93. /// <param name="item">The ISharePointProjectItem being extended.</param>
  94. /// <param name="service">The sharepoint project service.</param>
  95. /// <returns>Returns true if this SPI is packaged in any project in the solution whether by feature or direct (e.g. mapped folder).</returns>
  96. public static bool IsPartOfAnyProjectPackage(this ISharePointProjectItem item, ISharePointProjectService service)
  97. {
  98. return item.IsDirectPartOfAnyProjectPackage(service) || item.IsPartOfAnyPackagedProjectFeature(service);
  99. }
  100. /// <summary>
  101. /// Retrieves all projects in the solution where this SPI is packaged.
  102. /// </summary>
  103. /// <param name="item">The ISharePointProjectItem being extended.</param>
  104. /// <param name="service">The sharepoint project service.</param>
  105. /// <returns>Returns all projects in the solution where this SPI is packaged.</returns>
  106. public static IEnumerable<ISharePointProject> GetProjectsWhereInPackage(this ISharePointProjectItem item, ISharePointProjectService service)
  107. {
  108. List<ISharePointProject> pkgProjects = new List<ISharePointProject>();
  109. foreach (ISharePointProject project in service.Projects)
  110. {
  111. if (item.IsPartOfProjectPackage(project))
  112. {
  113. pkgProjects.Add(project);
  114. }
  115. }
  116. return pkgProjects;
  117. }
  118. /// <summary>
  119. /// Retrieves all packaged features this SPI is a part of within a specific project.
  120. /// </summary>
  121. /// <param name="item">The ISharePointProjectItem being extended.</param>
  122. /// <param name="project">The current project.</param>
  123. /// <returns>Returns all packaged features this SPI is a part of within a specific project.</returns>
  124. public static IEnumerable<ISharePointProjectFeature> GetFeaturesWhereInPackage(this ISharePointProjectItem item, ISharePointProject project)
  125. {
  126. return item.ParentFeaturesInProject(project).Where(feature => feature.IsPartOfProjectPackage(project));
  127. }
  128. /// <summary>
  129. /// Converts to project item.
  130. /// </summary>
  131. /// <param name="item">The item.</param>
  132. /// <returns></returns>
  133. public static ProjectItem ConvertToProjectItem(this ISharePointProjectItem item)
  134. {
  135. return item.DefaultFile.ProjectItem.Project.ProjectService.Convert<ISharePointProjectItemFile, ProjectItem>(item.DefaultFile);
  136. }
  137. /// <summary>
  138. /// Converts to VS project item.
  139. /// </summary>
  140. /// <param name="item">The item.</param>
  141. /// <returns></returns>
  142. public static VSProjectItem ConvertToVSProjectItem(this ISharePointProjectItem item)
  143. {
  144. ProjectItem dteItem = item.DefaultFile.ProjectItem.Project.ProjectService.Convert<ISharePointProjectItemFile, ProjectItem>(item.DefaultFile);
  145. return dteItem.Object as VSProjectItem;
  146. }
  147. #endregion
  148. }
  149. }