Example #1
0
        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());
        }
Example #2
0
        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 { }
        }
        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 { }
        }
        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);
        }
Example #6
0
        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());
            }
        }
Example #7
0
        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());
                }
            }
        }
Example #8
0
        /// <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("");
        }