/// <summary> /// Imports the type of the content. /// </summary> /// <param name="contentTypeInfo">The content type info.</param> public static void ImportContentType(ContentTypeInfo contentTypeInfo) { if (contentTypeInfo != null) { //Create the encoding definition XDeclaration declaration = new XDeclaration("1.0", Encoding.UTF8.WebName, null); XNamespace sharepointToolsNamespace = @"http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel"; XNamespace sharepointNamespace = @"http://schemas.microsoft.com/sharepoint/"; XElement elementsFileContents = XElement.Parse(BuildElements(contentTypeInfo, declaration, sharepointNamespace)); EnvDTE.Project activeProject = DTEManager.ActiveProject; if (activeProject != null) { ISharePointProjectService projectService = DTEManager.ProjectService; ISharePointProject activeSharePointProject = DTEManager.ActiveSharePointProject; if (activeSharePointProject != null) { ISharePointProjectItem contentTypeProjectItem = activeSharePointProject.ProjectItems.Add(contentTypeInfo.Name, "Microsoft.VisualStudio.SharePoint.ContentType"); System.IO.File.WriteAllText(Path.Combine(contentTypeProjectItem.FullPath, "Elements.xml"), elementsFileContents.ToString().Replace("xmlns=\"\"", String.Empty)); ISharePointProjectItemFile elementsXml = contentTypeProjectItem.Files.AddFromFile("Elements.xml"); elementsXml.DeploymentType = DeploymentType.ElementManifest; elementsXml.DeploymentPath = String.Format(@"{0}\", contentTypeInfo.Name); contentTypeProjectItem.DefaultFile = elementsXml; } } } }
private bool IsFileWithTokenReplacements(ISharePointProjectItemFile projectItemFile) { var fileExtension = Path.GetExtension(projectItemFile.Name).Replace(".", string.Empty); var fileExtensionsWithTokens = this.GetTokenReplacementFileExtensions(projectItemFile.Project); return(fileExtensionsWithTokens.Contains(fileExtension, StringComparer.OrdinalIgnoreCase)); }
void ImportListInstanceExplorerExtension_Click(object sender, MenuItemEventArgs e) { IExplorerNode listNode = e.Owner as IExplorerNode; if (listNode != null) { try { IListNodeInfo listInfo = listNode.Annotations.GetValue <IListNodeInfo>(); string listInstanceContents = listNode.Context.SharePointConnection.ExecuteCommand <Guid, string>(CommandIds.GetListInstanceXmlCommandId, listInfo.Id); XNamespace xn = "http://schemas.microsoft.com/sharepoint/"; string moduleContents = new XElement(xn + "Elements", XElement.Parse(listInstanceContents)).ToString().Replace(" xmlns=\"\"", String.Empty); EnvDTE.Project activeProject = Utils.GetActiveProject(); if (activeProject != null) { ISharePointProjectService projectService = listNode.ServiceProvider.GetService(typeof(ISharePointProjectService)) as ISharePointProjectService; ISharePointProject activeSharePointProject = projectService.Projects[activeProject.FullName]; if (activeSharePointProject != null) { string spiName = listInfo.Title; ISharePointProjectItem listInstanceProjectItem = null; bool itemCreated = false; int counter = 0; do { try { listInstanceProjectItem = activeSharePointProject.ProjectItems.Add(spiName, "Microsoft.VisualStudio.SharePoint.ListInstance"); itemCreated = true; } catch (ArgumentException) { spiName = String.Format("{0}{1}", listInfo.Title, ++counter); } }while (!itemCreated); string elementsXmlFullPath = Path.Combine(listInstanceProjectItem.FullPath, "Elements.xml"); System.IO.File.WriteAllText(elementsXmlFullPath, moduleContents); ISharePointProjectItemFile elementsXml = listInstanceProjectItem.Files.AddFromFile("Elements.xml"); elementsXml.DeploymentType = DeploymentType.ElementManifest; elementsXml.DeploymentPath = String.Format(@"{0}\", spiName); listInstanceProjectItem.DefaultFile = elementsXml; Utils.OpenFile(Path.Combine(elementsXmlFullPath)); } } } catch (Exception ex) { listNode.Context.ShowMessageBox(String.Format("The following exception occured while exporting List Instance: {0}", ex.Message), MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Imports the field. /// </summary> /// <param name="fieldNode">The field node.</param> /// <exception cref="System.ArgumentNullException"></exception> public static void ImportField(IExplorerNode fieldNode) { if (fieldNode == null) { throw new ArgumentNullException("fieldNode"); } Microsoft.VisualStudio.SharePoint.Explorer.Extensions.IFieldNodeInfo nodeInfo = fieldNode.Annotations.GetValue <Microsoft.VisualStudio.SharePoint.Explorer.Extensions.IFieldNodeInfo>(); if (nodeInfo != null) { FieldNodeInfo fieldNodeInfo = new FieldNodeInfo { ContentTypeName = nodeInfo.ContentTypeName, Id = nodeInfo.Id, IsHidden = nodeInfo.IsHidden, ListId = nodeInfo.ListId, Title = nodeInfo.Title }; Dictionary <string, string> fieldProperties = null; if (String.IsNullOrEmpty(fieldNodeInfo.ContentTypeName) && fieldNodeInfo.ListId == Guid.Empty) { fieldProperties = fieldNode.Context.SharePointConnection.ExecuteCommand <FieldNodeInfo, Dictionary <string, string> >(SiteColumnsSharePointCommandIds.GetProperties, fieldNodeInfo); } else { fieldProperties = fieldNode.Context.SharePointConnection.ExecuteCommand <FieldNodeInfo, Dictionary <string, string> >(FieldSharePointCommandIds.GetProperties, fieldNodeInfo); } if (fieldProperties != null) { XNamespace xn = XNamespace.Get("http://schemas.microsoft.com/sharepoint/"); XElement xElements = new XElement(xn + "Elements", XElement.Parse(fieldProperties["SchemaXml"])); EnvDTE.Project activeProject = DTEManager.ActiveProject; if (activeProject != null) { ISharePointProjectService projectService = fieldNode.ServiceProvider.GetService(typeof(ISharePointProjectService)) as ISharePointProjectService; ISharePointProject activeSharePointProject = projectService.Projects[activeProject.FullName]; if (activeSharePointProject != null) { ISharePointProjectItem fieldProjectItem = activeSharePointProject.ProjectItems.Add(fieldProperties["InternalName"], "Microsoft.VisualStudio.SharePoint.Field"); System.IO.File.WriteAllText(Path.Combine(fieldProjectItem.FullPath, "Elements.xml"), xElements.ToString().Replace("xmlns=\"\"", String.Empty)); ISharePointProjectItemFile elementsXml = fieldProjectItem.Files.AddFromFile("Elements.xml"); elementsXml.DeploymentType = DeploymentType.ElementManifest; elementsXml.DeploymentPath = String.Format(@"{0}\", fieldProperties["InternalName"]); fieldProjectItem.DefaultFile = elementsXml; } } } } }
private string ReplaceSharePointFileTokens(ISharePointProjectItemFile projectItemFile) { var tokens = this.GetSharePointFileTokenReplacements(projectItemFile.Project); var fileContent = File.ReadAllText(projectItemFile.FullPath); foreach (var token in tokens) { fileContent = fileContent.Replace(token.Key, token.Value); } return(fileContent); }
private ISharePointProjectItemFile GetProjectItemFile(string fileFullPath) { ISharePointProjectItemFile projectItem = null; foreach (var project in this.ProjectService.Projects) { projectItem = project.ProjectItems .SelectMany(folder => folder.Files) .FirstOrDefault(file => file.FullPath.Equals(fileFullPath, StringComparison.OrdinalIgnoreCase)); } return(projectItem); }
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> /// Handles the FileAdded event of the TypeDefinition control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Microsoft.VisualStudio.SharePoint.SharePointProjectItemFileEventArgs"/> instance containing the event data.</param> static void TypeDefinition_FileAdded(object sender, SharePointProjectItemFileEventArgs e) { ISharePointProjectItemFile file = e.ProjectItemFile; if (file.DeploymentType == DeploymentType.NoDeployment) { ProjectItem dteItem = file.ProjectItem.Project.ProjectService.Convert <ISharePointProjectItemFile, ProjectItem>(file); if (dteItem.Kind == "Compile" && dteItem.FileCodeModel != null) { foreach (EnvDTE.CodeClass codeClass in FindClasses(dteItem.FileCodeModel)) { if (IsOperationClass(codeClass)) { } } } } }
// Generates an external data list for each Entity in the BDC model. This event handler is called // when the developer clicks the shortcut menu item that the extension adds to the BDC project item. private void GenerateExternalDataLists_Execute(object sender, MenuItemEventArgs e) { ISharePointProjectItem projectItem = (ISharePointProjectItem)e.Owner; ISharePointProjectItemFile bdcmFile = GetModelFile(projectItem); XDocument doc = XDocument.Load(bdcmFile.FullPath); List <XElement> skippedEntities = new List <XElement>(); // Try to generate an external data list for each entity defined in the BDC model file. foreach (XElement entity in doc.Root.Elements(BdcNamespace + "LobSystems").Elements( BdcNamespace + "LobSystem").Elements(BdcNamespace + "Entities").Elements(BdcNamespace + "Entity")) { if (!GenerateExternalDataList(projectItem, entity)) { skippedEntities.Add(entity); } } // Report skipped entities. if (skippedEntities.Count != 0) { StringBuilder entityNameList = null; skippedEntities.ForEach(delegate(XElement entity) { if (entityNameList == null) { entityNameList = new StringBuilder(); } else { entityNameList.AppendLine(","); } entityNameList.Append(entity.Attribute("Name").Value); }); string message = string.Format("The following Entities were skipped because either a LobSystemInstance, " + "SpecificFinder, or Finder was not found for them. \r\n{0}", entityNameList); projectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Warning); } }
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); }
// Tries to generate an external data list for the specified BDC model project item and entity. private bool GenerateExternalDataList(ISharePointProjectItem projectItem, XElement entity) { string lobSystemInstanceName = GetLobSystemInstanceName(entity); string specificFinderName = GetSpecificFinderName(entity); string finderName = GetFinderName(entity); string entityName = entity.Attribute("Name").Value; if (string.IsNullOrEmpty(lobSystemInstanceName) || string.IsNullOrEmpty(specificFinderName) || string.IsNullOrEmpty(finderName)) { return(false); } string newExternalDataListName = entityName + "DataList"; ISharePointProjectItem existingProjectItem = (from ISharePointProjectItem existingItem in projectItem.Project.ProjectItems where existingItem.Name == newExternalDataListName select existingItem).FirstOrDefault(); // Add a new list instance and populate it with data from the BDC model. if (existingProjectItem == null) { ISharePointProjectItem newExternalDataList = projectItem.Project.ProjectItems.Add(newExternalDataListName, "Microsoft.VisualStudio.SharePoint.ListInstance"); string newExternalDataListString = externalDataListContent; newExternalDataListString = newExternalDataListString.Replace("$EntityName$", entityName); newExternalDataListString = newExternalDataListString.Replace("$LobSystemInstance$", lobSystemInstanceName); newExternalDataListString = newExternalDataListString.Replace("$EntityNamespace$", entity.Attribute("Namespace").Value); newExternalDataListString = newExternalDataListString.Replace("$SpecificFinder$", specificFinderName); newExternalDataListString = newExternalDataListString.Replace("$Finder$", finderName); string elementsXmlPath = Path.Combine(newExternalDataList.FullPath, "Elements.xml"); File.WriteAllText(elementsXmlPath, newExternalDataListString); ISharePointProjectItemFile elementsFile = newExternalDataList.Files.AddFromFile(elementsXmlPath); elementsFile.DeploymentType = DeploymentType.ElementManifest; } return(true); }
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()); } }
/// <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); }
/// <summary> /// Create a new instance of the SharePointProjectItemFileArtefact object. /// </summary> /// <param name="file">The project file.</param> public SharePointProjectItemFileArtefact(ISharePointProjectItemFile file) { this.file = file; }