private void SetDeploymentPath(DTE dte, Project project, ProjectItem CreatedProjectItem, SPFileType sPFileType, string evaluatedDeploymentPath) { //set to content if ((sPFileType != SPFileType.CustomCode)) { CreatedProjectItem.Properties.Item("BuildAction").Value = 2; } //ok, file is placed, but we need set the deployment path ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(dte); ISharePointProject sharePointProject = projectService.Convert <EnvDTE.Project, ISharePointProject>(project); sharePointProject.Synchronize(); if (CreatedProjectItem.Collection.Parent is ProjectItem) { ProjectItem parentItem = CreatedProjectItem.Collection.Parent as ProjectItem; string name = parentItem.Name; //is the parent element a feature? try { ISharePointProjectFeature parentIsFeature = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectFeature>(parentItem); if (parentIsFeature != null) { ISharePointProjectItem addedSharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItem>(CreatedProjectItem); if (addedSharePointItem != null) { parentIsFeature.ProjectItems.Add(addedSharePointItem); } } } catch { } } try { //sometimes property deploymentpath is readonly //1. new added items need to be validated before ISharePointProjectItemFile newaddedSharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItemFile>(CreatedProjectItem); newaddedSharePointItem.DeploymentType = Helpers2.GetDeploymentTypeFromFileType(this.DeploymentType); if (!string.IsNullOrEmpty(evaluatedDeploymentPath)) { newaddedSharePointItem.DeploymentPath = evaluatedDeploymentPath; } } catch { } }
/// <summary> /// Detects if a project is a SharePoint project based on a list of project type GUIDs. /// </summary> public static bool IsSharePointProject(EnvDTE.Project project, bool requireFarm) { //TODO: move this into the project utilities try { ISharePointProjectService projectService = DTEManager.ProjectService; if (projectService != null) { // Convert the DTE project into a SharePoint project. If the conversion fails, this is not a SP project. ISharePointProject p = projectService.Convert <EnvDTE.Project, Microsoft.VisualStudio.SharePoint.ISharePointProject>(project); if (p != null && requireFarm) { return(!p.IsSandboxedSolution); } else { return(p != null); } } } catch { // Must be VERY careful not to throw exceptions in here, since this is called on every click of the context menu. } return(false); }
internal static string GetStartupUrl(DTE dte, Project project) { try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(dte); if (project != null) { try { //set url for current project ISharePointProject sharePointProject = projectService.Convert <EnvDTE.Project, ISharePointProject>(project); if (sharePointProject.StartupUrl != null) { return(sharePointProject.StartupUrl.ToString()); } return(sharePointProject.SiteUrl.ToString()); } catch { } } } catch { } return(Helpers.GetApplicationConfigValue(dte, "StartupUrl", Helpers.GetApplicationConfigValue(dte, "DebuggingWebApp", "").ToString()).ToString()); }
public override void Execute() { if (string.IsNullOrEmpty(SiteUrl)) { return; } EnvDTE.DTE service = this.GetService <EnvDTE.DTE>(true); if (!string.IsNullOrEmpty(CurrentProjectName)) { CurrentProject = Helpers.GetProjectByName(service, CurrentProjectName); } try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(service); if (CurrentProject != null) { try { //set url for current project ISharePointProject sharePointProject = projectService.Convert <EnvDTE.Project, ISharePointProject>(CurrentProject); sharePointProject.SiteUrl = new Uri(SiteUrl); } catch { } } else { //set url for all projects foreach (ISharePointProject sharePointProject in projectService.Projects) { try { if (sharePointProject.SiteUrl == null) { sharePointProject.SiteUrl = new Uri(SiteUrl); Helpers.LogMessage(service, this, "Settings site url of project " + sharePointProject.Name + " to " + new Uri(SiteUrl).ToString()); } } catch (Exception) { Helpers.LogMessage(service, this, "Could not set site url of project " + sharePointProject.Name + " to " + new Uri(SiteUrl).ToString()); } } } } catch { } }
public override void Execute() { if (string.IsNullOrEmpty(DeploymentTarget)) { return; } var service = this.GetService <DTE>(true); if (!string.IsNullOrEmpty(CurrentProjectName)) { CurrentProject = Helpers.GetProjectByName(service, CurrentProjectName); } try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(service); if (CurrentProject != null) { try { //set url for current project ISharePointProject sharePointProject = projectService.Convert <Project, ISharePointProject>(CurrentProject); ChangeDeploymentTarget(sharePointProject, DeploymentTarget); } catch { } } else { foreach (ISharePointProject sharePointProject in projectService.Projects) { try { ChangeDeploymentTarget(sharePointProject, DeploymentTarget); } catch { } } } } catch { } }
private bool ItemIsDeployable(ProjectItem pitem) { string sourcefilename = Helpers.GetFullPathOfProjectItem(pitem); if (!Helpers2.IsFileDeployableToHive(sourcefilename)) { return(false); } try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(pitem.DTE); try { ISharePointProjectItemFile selectedSharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItemFile>(pitem); if (selectedSharePointItem != null && selectedSharePointItem.DeploymentPath != "") { return(true); } } catch { } } catch { } //check if the parent is folder 12 try { string itemPath = Helpers.GetFullPathOfProjectItem(pitem); string projectPath = Helpers.GetProjectFolder(pitem.ContainingProject); itemPath = itemPath.Substring(projectPath.Length + 1); if (itemPath.StartsWith(@"12") || itemPath.StartsWith(@"14") || itemPath.StartsWith(@"15") || itemPath.StartsWith(@"SharePointRoot", StringComparison.InvariantCultureIgnoreCase)) { return(true); } } catch { } return(false); }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private async void Execute(object sender, EventArgs e) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); DTE dte = await this.ServiceProvider.GetServiceAsync(typeof(DTE)) as DTE; ISharePointProjectService sharePointProjectService = await this.ServiceProvider.GetServiceAsync(typeof(ISharePointProjectService)) as ISharePointProjectService; var selectedProjects = dte.ActiveSolutionProjects as Array; if (selectedProjects != null && selectedProjects.Length > 0) { var currentProject = sharePointProjectService.Convert <Project, ISharePointProject>((Project)selectedProjects.GetValue(0)) as ISharePointProject; var extService = new SPProjectExtService(currentProject); var assembliesToDeploy = extService.GetDeployableAssemblies(out string[] missing); if (assembliesToDeploy.Any()) { var gacService = new System.EnterpriseServices.Internal.Publish(); currentProject.ProjectService.Logger.ActivateOutputWindow(); currentProject.ProjectService.Logger.WriteLine("========= Copy to GAC ===========", LogCategory.Status); foreach (var assembly in assembliesToDeploy) { currentProject.ProjectService.Logger.WriteLine(Path.GetFileName(assembly) + "...", LogCategory.Status); gacService.GacInstall(assembly); } if (missing.Any()) { currentProject.ProjectService.Logger.WriteLine("Missing:", LogCategory.Status); foreach (var missingAssembly in missing) { currentProject.ProjectService.Logger.WriteLine(Path.GetFileName(missingAssembly), LogCategory.Status); } } currentProject.ProjectService.Logger.WriteLine($"===== Copy to GAC done at {DateTime.Now:hh:mm:ss} =====", LogCategory.Status); extService.RecyclePools(); } } }
public void OnItemSave(ProjectItem projectItem) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); try { EnvDTE.Project dteProject = projectItem.ContainingProject; ISharePointProject spProject = _spProjService.Convert <EnvDTE.Project, ISharePointProject>(dteProject); if (spProject != null) { bool isAutoCopyToRoot = new ProjectProperties.PropertySource(spProject).AutoCopy; if (isAutoCopyToRoot) { var service = new CopyToRootService(_spProjService); service.CopyProjectItem(projectItem); } } } catch (Exception ex) { _spProjService.Logger.WriteLine($"Error copy file {ex}", LogCategory.Warning); } }
public override void Execute() { DTE service = (DTE)this.GetService(typeof(DTE)); try { if (projectItem != null) { Helpers.LogMessage(service, this, "*********** SharePointService Properties ********************"); try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(service); ISharePointProject sharePointProject = projectService.Convert <Project, ISharePointProject>(project); try { //is it a feature ISharePointProjectFeature sharePointFeature = projectService.Convert <ProjectItem, ISharePointProjectFeature>(projectItem); } catch { } try { //is it a feature ISharePointProjectItem sharePointItem = projectService.Convert <ProjectItem, ISharePointProjectItem>(projectItem); } catch { } try { //is it a feature ISharePointProjectItemFile sharePointItemFile = projectService.Convert <ProjectItem, ISharePointProjectItemFile>(projectItem); } catch { } try { //is it a mapped folder IMappedFolder sharePointMappedFolder = projectService.Convert <ProjectItem, IMappedFolder>(projectItem); } catch { } try { //is it a mapped folder ISharePointProjectPackage sharePointProjectPackage = projectService.Convert <ProjectItem, ISharePointProjectPackage>(projectItem); } catch { } } catch {} //codelements Helpers.LogMessage(service, this, "*********** EnvDTE CodeElements ********************"); foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements) { try { Helpers.LogMessage(service, this, "codeElement.FullName: " + codeElement.FullName); Helpers.LogMessage(service, this, "codeElement.Type: " + codeElement.GetType().ToString()); Helpers.LogMessage(service, this, "codeElement.Kind: " + codeElement.Kind.ToString()); } catch {} } Helpers.LogMessage(service, this, "*********** EnvDTE Properties ********************"); for (int i = 0; i < projectItem.Properties.Count; i++) { try { string name = projectItem.Properties.Item(i).Name; string value = ""; try { value = projectItem.Properties.Item(i).Value.ToString(); } catch (Exception) { } Helpers.LogMessage(service, this, name + "=" + value); } catch (Exception) { } } } else if (project != null) { for (int i = 0; i < project.Properties.Count; i++) { try { string name = project.Properties.Item(i).Name; string value = ""; try { value = project.Properties.Item(i).Value.ToString(); } catch (Exception) { } Helpers.LogMessage(service, this, name + "=" + value); if (project.Properties.Item(i).Collection.Count > 0) { } } catch (Exception) { } } } } catch (Exception ex) { Helpers.LogMessage(service, this, ex.ToString()); } }
public override void Execute() { DTE dte = (DTE)this.GetService(typeof(DTE)); Project Project = this.GetTargetProject(dte); string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName); string evaluatedParentFeatureName = EvaluateParameterAsString(dte, ParentFeatureName); string evaluatedAssemblyName = EvaluateParameterAsString(dte, AssemblyName); string evaluatedNamespace = EvaluateParameterAsString(dte, Namespace); string xmlContent = GenerateContent(evaluatedTemplateFileName, "ReceiverCode.xml"); try { //feature name consists of application name + SPSFConstants.NameSeparator + featurename, but we need to strip off the feature name from that string appName = Helpers.GetSaveApplicationName(dte); string featureName = evaluatedParentFeatureName; if (featureName.StartsWith(appName, StringComparison.InvariantCultureIgnoreCase)) { featureName = featureName.Substring(appName.Length + 1); } string FeatureReceiverClass = featureName + "EventReceiver"; if (Helpers2.IsSharePointVSTemplate(dte, Project)) { //can we cast the project to ISharePointSolution ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(Project.DTE); ISharePointProject sharePointProject = projectService.Convert <EnvDTE.Project, ISharePointProject>(Project); sharePointProject.Synchronize(); ISharePointProjectFeature parentfeature = null; foreach (ISharePointProjectFeature feature in sharePointProject.Features) { if (feature.Name == evaluatedParentFeatureName) { parentfeature = feature; } } if (parentfeature != null) { if (string.IsNullOrEmpty(parentfeature.Model.ReceiverClass)) { if (dte.SuppressUI || (MessageBox.Show("Feature '" + evaluatedParentFeatureName + "' contains no feature receiver. Add feature receiver?", "Add Feature Receiver?", MessageBoxButtons.YesNo) == DialogResult.Yes)) { ProjectItem projectItem = projectService.Convert <ISharePointProjectFeature, EnvDTE.ProjectItem>(parentfeature); ProjectItem featureXmlFile = projectService.Convert <ISharePointProjectMember, EnvDTE.ProjectItem>(parentfeature.FeatureFile); //no feature receiver there, we create one ProjectItem createdFeatureReceiverItem = CreateNewFeatureReceiver(featureName, projectItem, featureXmlFile); parentfeature.Model.ReceiverAssembly = evaluatedAssemblyName; parentfeature.Model.ReceiverClass = evaluatedNamespace + "." + FeatureReceiverClass; this.MergeNewCodeWithClass(createdFeatureReceiverItem, FeatureReceiverClass, xmlContent); sharePointProject.Synchronize(); //is test run save file if (dte.SuppressUI) { featureXmlFile.Save(); //createdFeatureReceiverItem.Save(); } } else { return; } } else { //1. we check if there is a filename with .generated in it //we need the class name of the featurereceiver, but in VS-Feature the name contains a ID string className = parentfeature.Model.ReceiverClass; if (className.StartsWith("$SharePoint.Type")) { //not supported??? if (parentfeature.EventReceiverFile != null) { ProjectItem existingCode = projectService.Convert <ISharePointProjectMember, EnvDTE.ProjectItem>(parentfeature.EventReceiverFile); this.MergeNewCodeWithClass(existingCode, "", xmlContent); } } else { string foundReceiverClassName = parentfeature.Model.ReceiverClass; string receiverNamespace = foundReceiverClassName.Substring(0, foundReceiverClassName.LastIndexOf(".") - 1); string receiverClassName = foundReceiverClassName.Substring(foundReceiverClassName.LastIndexOf(".") + 1); //ok, class name found //yes, there is a feature receiver, but we need to find the class in the project //find any .cs item which contains the name of the featureclass and namespace List <string> patterns = new List <string>(); patterns.Add(receiverNamespace); patterns.Add(receiverClassName); patterns.Add(" FeatureActivated("); //to ensure that we get the correct partial class ProjectItem existingCode = Helpers.FindItem(Project, ".cs", patterns); if (existingCode != null) { this.MergeNewCodeWithClass(existingCode, receiverClassName, xmlContent); } } } } } else { //find the feature receiver code ProjectItem featureXMLFile = Helpers.GetFeatureXML(Project, evaluatedParentFeatureName); ProjectItem featureFolder = Helpers2.GetFeature(Project.DTE, Project, evaluatedParentFeatureName); //check if feature already contains feature receiver if (featureXMLFile != null) { string receiverClassName = Helpers.GetFeatureReceiverClass(featureXMLFile); string receiverNamespace = Helpers.GetFeatureReceiverNamespace(featureXMLFile); if (receiverClassName == "") { if (dte.SuppressUI || (MessageBox.Show("Feature '" + evaluatedParentFeatureName + "' contains no feature receiver. Add feature receiver?", "Add Feature Receiver?", MessageBoxButtons.YesNo) == DialogResult.Yes)) { //there is no feature receiver, we need to create one :-( string path = Helpers.GetFullPathOfProjectItem(featureXMLFile); XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/"); XmlNode node = doc.SelectSingleNode("/ns:Feature", nsmgr); if (node != null) { ProjectItem createdFeatureReceiverItem = CreateNewFeatureReceiver(featureName, featureFolder, featureXMLFile); XmlAttribute receiverAssemblyAttrib = doc.CreateAttribute("ReceiverAssembly"); receiverAssemblyAttrib.Value = evaluatedAssemblyName; node.Attributes.Append(receiverAssemblyAttrib); XmlAttribute receiverClassAttrib = doc.CreateAttribute("ReceiverClass"); receiverClassAttrib.Value = evaluatedNamespace + "." + FeatureReceiverClass; node.Attributes.Append(receiverClassAttrib); Helpers.EnsureCheckout(dte, path); XmlWriter xw = XmlWriter.Create(path, Helpers.GetXmlWriterSettings(path)); doc.Save(xw); xw.Flush(); xw.Close(); this.MergeNewCodeWithClass(createdFeatureReceiverItem, FeatureReceiverClass, xmlContent); } } else { return; } } else { //yes, there is a feature receiver, but we need to find the class in the project //find any .cs item which contains the name of the featureclass and namespace List <string> patterns = new List <string>(); patterns.Add(receiverClassName); patterns.Add(receiverNamespace); patterns.Add(" FeatureActivated("); //to ensure that we get the correct partial class ProjectItem existingCode = Helpers.FindItem(Project, ".cs", patterns); if (existingCode != null) { //find the activated Function string featureFilename = Helpers.GetFullPathOfProjectItem(existingCode); this.MergeNewCodeWithClass(existingCode, receiverClassName, xmlContent); } } } if (featureXMLFile != null) { CreatedProjectItem = featureXMLFile; } else { CreatedProjectItem = Helpers.GetFeatureXML(Project, evaluatedParentFeatureName); } if (featureFolder != null) { CreatedProjectFolder = featureFolder; } else { CreatedProjectFolder = Helpers2.GetFeature(Project.DTE, Project, evaluatedParentFeatureName); } } } catch (Exception ex) { if (dte.SuppressUI) { throw new Exception(ex.ToString()); } else { MessageBox.Show(ex.ToString()); } } }
/// <summary> /// returns the relative path of the item in the final sharepoint folder /// </summary> /// <param name="dte"></param> /// <param name="selectedItem"></param> /// <returns></returns> public static string GetDeploymentPathOfItem(DTE dte, ProjectItem selectedItem) { //1. trying to use VS 2010 try { ISharePointProjectService projectService = Helpers2.GetSharePointProjectService(dte); try { ISharePointProjectItemFile selectedSharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItemFile>(selectedItem); string path = selectedSharePointItem.DeploymentRoot + selectedSharePointItem.DeploymentPath + selectedSharePointItem.Name; //= {SharePointRoot}\\Template\\ if (path.StartsWith("{SharePointRoot}")) { path = path.Replace("{SharePointRoot}", ""); } if (path.Contains("{FeatureName}")) { //ok, element is part of a feature, need to find the feature where the element is located string parentFeatureName = ""; ISharePointProjectItem sharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItem>(selectedItem); if (sharePointItem == null) { if (selectedItem.Collection.Parent is ProjectItem) { sharePointItem = projectService.Convert <EnvDTE.ProjectItem, ISharePointProjectItem>(selectedItem.Collection.Parent as ProjectItem); } } foreach (ISharePointProject project in projectService.Projects) { foreach (ISharePointProjectFeature feature in project.Features) { if (feature.ProjectItems.Contains(sharePointItem)) { parentFeatureName = feature.Name; } } } path = path.Replace("{FeatureName}", parentFeatureName); } return(path); } catch { } } catch { } //ok, we have HIVE format! //structure could be 12, 14, oder SharePointRoot string projectfolder = Path.GetDirectoryName(Helpers.GetFullPathOfProjectItem(selectedItem.ContainingProject)); string itemFullPath = Helpers.GetFullPathOfProjectItem(selectedItem); string itemRelativePath = itemFullPath.Substring(projectfolder.Length + 1); if (itemRelativePath.StartsWith("12")) { return(itemRelativePath.Substring(2)); } else if (itemRelativePath.StartsWith("14")) { return(itemRelativePath.Substring(2)); } else if (itemRelativePath.StartsWith("15")) { return(itemRelativePath.Substring(2)); } else if (itemRelativePath.StartsWith("SharePointRoot", StringComparison.InvariantCultureIgnoreCase)) { return(itemRelativePath.Substring(14)); } return(""); }
/// <summary> /// Returns all SharePoint artefacts for a given ProjectItem. Multiple items may be returned, for example, if the /// selection is a folder. Note that this list is not pruned in the event of multiple selections from the same sub-tree. /// </summary> /// <param name="projectService"></param> /// <param name="item"></param> /// <returns></returns> public static List <QuickCopyableSharePointArtefact> ResolveProjectItemToArtefacts(ISharePointProjectService projectService, ProjectItem item) { // Ensure we have a SharePoint service. if (projectService == null) { return(null); } // Prepare list for return. List <QuickCopyableSharePointArtefact> artefacts = new List <QuickCopyableSharePointArtefact>(); // See if this item is a SPI file. try { ISharePointProjectItemFile spFile = projectService.Convert <ProjectItem, ISharePointProjectItemFile>(item); if (spFile != null) { if (spFile.DeploymentType != DeploymentType.NoDeployment) { artefacts.Add(new SharePointProjectItemFileArtefact(spFile)); return(artefacts); } } } catch { } // See if this item is an SPI. try { ISharePointProjectItem spItem = projectService.Convert <ProjectItem, ISharePointProjectItem>(item); if (spItem != null) { artefacts.Add(new SharePointProjectItemArtefact(spItem)); return(artefacts); } } catch { } // See if this item is a Feature. try { ISharePointProjectFeature spFeature = projectService.Convert <ProjectItem, ISharePointProjectFeature>(item); if (spFeature != null) { artefacts.Add(new SharePointProjectFeatureArtefact(spFeature)); return(artefacts); } } catch { } // See if we have a Folder, and recursively find SharePoint items. try { if (item.ProjectItems.Count > 0) { for (int i = 1; i <= item.ProjectItems.Count; i++) { ProjectItem childItem = item.ProjectItems.Item(i); artefacts.AddRange(ResolveProjectItemToArtefacts(projectService, childItem)); } } } catch { } // TODO: Also, these items are potential types for conversion. // ISharePointProjectFeatureResourceFile // ISharePointProjectPackage return(artefacts); }
public void CopyProjectItem(ProjectItem item) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); var logger = _spProjService.Logger; logger.ActivateOutputWindow(); try { var spFile = _spProjService.Convert <ProjectItem, ISharePointProjectItemFile>(item); if (spFile != null) { if (spFile.DeploymentType == DeploymentType.TemplateFile || spFile.DeploymentType == DeploymentType.RootFile) { var targetPath = Path.Combine( Path.Combine(spFile.DeploymentRoot, spFile.DeploymentPath).Replace("{SharePointRoot}", _spProjService.SharePointInstallPath), Path.GetFileName(spFile.FullPath) ); var sourcePath = spFile.FullPath; File.Copy(sourcePath, targetPath, true); logger.WriteLine($"Copy {sourcePath} -> {targetPath}", LogCategory.Status); } } } catch { } // See if this item is an SPI. try { var spItem = _spProjService.Convert <ProjectItem, ISharePointProjectItem>(item); if (spItem != null) { } } catch { } // See if this item is a Feature. try { var spFeature = _spProjService.Convert <ProjectItem, ISharePointProjectFeature>(item); if (spFeature != null) { } } catch { } try { if (item.ProjectItems.Count > 0) { for (int i = 1; i <= item.ProjectItems.Count; i++) { ProjectItem childItem = item.ProjectItems.Item(i); CopyProjectItem(childItem); } } } catch { } }