private void DeploySearchConfiguration(object modelHost, SPSite site, SearchConfigurationDefinition definition)
        {
            var conf  = new SearchConfigurationPortability(site);
            var owner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = conf,
                ObjectType       = typeof(SearchConfigurationPortability),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            conf.ImportSearchConfiguration(owner, definition.SearchConfiguration);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = conf,
                ObjectType       = typeof(SearchConfigurationPortability),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });
        }
        protected override void ExecuteCmdlet()
        {
            switch (Scope)
            {
                case SearchConfigurationScope.Web:
                    {
                        WriteObject(this.SelectedWeb.GetSearchConfiguration());
                        break;
                    }
                case SearchConfigurationScope.Site:
                    {
                        WriteObject(ClientContext.Site.GetSearchConfiguration());
                        break;
                    }
                case SearchConfigurationScope.Subscription:
                {
                    if (!ClientContext.Url.ToLower().Contains("-admin"))
                    {
                        throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
                    }

                    SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
                    var config = new SearchConfigurationPortability(ClientContext);
                    ClientResult<string> configuration = config.ExportSearchConfiguration(owningScope);
                    ClientContext.ExecuteQueryRetry(10, 60*5*1000);
                    WriteObject(configuration.Value);
                    break;
                }
            }
        }
Example #3
0
        protected override void ExecuteCmdlet()
        {
            switch (Scope)
            {
            case SearchConfigurationScope.Web:
            {
                WriteObject(this.SelectedWeb.GetSearchConfiguration());
                break;
            }

            case SearchConfigurationScope.Site:
            {
                WriteObject(ClientContext.Site.GetSearchConfiguration());
                break;
            }

            case SearchConfigurationScope.Subscription:
            {
                if (!ClientContext.Url.ToLower().Contains("-admin"))
                {
                    throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
                }

                SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
                var config = new SearchConfigurationPortability(ClientContext);
                ClientResult <string> configuration = config.ExportSearchConfiguration(owningScope);
                ClientContext.ExecuteQueryRetry(10, 60 * 5 * 1000);
                WriteObject(configuration.Value);
                break;
            }
            }
        }
        private void DeploySearchConfiguration(object modelHost, Site site, SearchConfigurationDefinition definition)
        {
            var context = site.Context;

            var conf = new SearchConfigurationPortability(context);
            var owner = new SearchObjectOwner(context, SearchObjectLevel.SPSite);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = conf,
                ObjectType = typeof(SearchConfigurationPortability),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            conf.ImportSearchConfiguration(owner, definition.SearchConfiguration);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = conf,
                ObjectType = typeof(SearchConfigurationPortability),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });

            context.ExecuteQueryWithTrace();
        }
Example #5
0
        private static void ImportSearchSettings(ClientContext context, string settingsFile)
        {
            ConsoleColor defaultForeground = Console.ForegroundColor;

            /*
             * SearchConfigurationPortability Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.search.portability.searchconfigurationportability(v=office.15).aspx
             *
             * SearchObjectOwner Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.office.server.search.administration.searchobjectowner(v=office.15).aspx
             */
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPWeb);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Importing search configuration settings to: " + context.Web.Title);
            Console.ForegroundColor = defaultForeground;

            sconfig.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(settingsFile));
            context.ExecuteQuery();

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Search settings have been imported. Press any key to continue");
            Console.ForegroundColor = defaultForeground;
            Console.Read();
        }
        /// <summary>
        /// Returns the current search configuration for the specified object level
        /// </summary>
        /// <param name="context"></param>
        /// <param name="searchSettingsObjectLevel"></param>
        /// <returns></returns>
        private static string GetSearchConfigurationImplementation(ClientRuntimeContext context, SearchObjectLevel searchSettingsObjectLevel)
        {
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsObjectLevel);

            ClientResult<string> configresults = sconfig.ExportSearchConfiguration(owner);
            context.ExecuteQueryRetry();

            return configresults.Value;
        }
Example #7
0
        /// <summary>
        /// Delete the search configuration at the specified object level - does not apply to managed properties.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="searchObjectLevel"></param>
        /// <param name="searchConfiguration"></param>
        private static void DeleteSearchConfigurationImplementation(ClientRuntimeContext context, SearchObjectLevel searchObjectLevel, string searchConfiguration)
        {
            SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchObjectLevel);

            // Delete search configuration
            searchConfig.DeleteSearchConfiguration(owner, searchConfiguration);
            context.Load(searchConfig);
            context.ExecuteQueryRetry();
        }
Example #8
0
        public void ImportSearchConfiguration(ClientContext context, string pathToSearchXml)
        {
            var searchConfigurationPortability = new SearchConfigurationPortability(context);
            var owningScope = new SearchObjectOwner(context, SearchObjectLevel.SPSite);

            var configurationXml = new XmlDocument();
            configurationXml.Load(pathToSearchXml);

            searchConfigurationPortability.ImportSearchConfiguration(owningScope, configurationXml.OuterXml);
            context.ExecuteQuery();
        }
        /// <summary>
        /// Returns the current search configuration for the specified object level
        /// </summary>
        /// <param name="context">ClinetRuntimeContext for SharePoint objects and operations</param>
        /// <param name="searchSettingsObjectLevel">A site server level value. i.e, SPWeb/SPSite/SPSiteSubscription/Ssa</param>
        /// <returns>Returns search configuration</returns>
        private static string GetSearchConfigurationImplementation(ClientRuntimeContext context, SearchObjectLevel searchSettingsObjectLevel)
        {
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsObjectLevel);

            ClientResult <string> configresults = sconfig.ExportSearchConfiguration(owner);

            context.ExecuteQueryRetry();

            return(configresults.Value);
        }
Example #10
0
        public void ImportSearchConfiguration(ClientContext context, string pathToSearchXml)
        {
            var searchConfigurationPortability = new SearchConfigurationPortability(context);
            var owningScope = new SearchObjectOwner(context, SearchObjectLevel.SPSite);

            var configurationXml = new XmlDocument();

            configurationXml.Load(pathToSearchXml);

            searchConfigurationPortability.ImportSearchConfiguration(owningScope, configurationXml.OuterXml);
            context.ExecuteQuery();
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var ctx = base.Context;

            SearchObjectLevel configScope = ConfigScope;
            var searchConfigurationPortability = new SearchConfigurationPortability(ctx);
            var owner = new SearchObjectOwner(ctx, configScope);
            var result = searchConfigurationPortability.ExportSearchConfiguration(owner);
            ctx.ExecuteQuery();
            System.IO.File.WriteAllText(Path, result.Value);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var ctx = base.Context;

            SearchObjectLevel configScope = ConfigScope;
            var searchConfigurationPortability = new SearchConfigurationPortability(ctx);
            var owner = new SearchObjectOwner(ctx, configScope);
            var searchConfigXml = InputObject.Read();
            searchConfigurationPortability.ImportSearchConfiguration(owner, searchConfigXml.OuterXml);
            ctx.ExecuteQuery();
        }
        protected string GetCurrentSearchConfiguration(Site site)
        {
            var context = site.Context;

            var conf  = new SearchConfigurationPortability(context);
            var owner = new SearchObjectOwner(context, SearchObjectLevel.SPSite);

            var result = conf.ExportSearchConfiguration(owner);

            context.ExecuteQueryWithTrace();

            return(result.Value);
        }
Example #14
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var ctx = base.Context;

            SearchObjectLevel configScope      = ConfigScope;
            var searchConfigurationPortability = new SearchConfigurationPortability(ctx);
            var owner           = new SearchObjectOwner(ctx, configScope);
            var searchConfigXml = InputObject.Read();

            searchConfigurationPortability.ImportSearchConfiguration(owner, searchConfigXml.OuterXml);
            ctx.ExecuteQuery();
        }
        protected string GetCurrentSearchConfiguration(Site site)
        {
            var context = site.Context;

            var conf = new SearchConfigurationPortability(context);
            var owner = new SearchObjectOwner(context, SearchObjectLevel.SPSite);

            var result = conf.ExportSearchConfiguration(owner);

            context.ExecuteQueryWithTrace();

            return result.Value;
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var ctx = base.Context;

            SearchObjectLevel configScope      = ConfigScope;
            var searchConfigurationPortability = new SearchConfigurationPortability(ctx);
            var owner  = new SearchObjectOwner(ctx, configScope);
            var result = searchConfigurationPortability.ExportSearchConfiguration(owner);

            ctx.ExecuteQuery();
            System.IO.File.WriteAllText(Path, result.Value);
        }
        protected override void ExecuteCmdlet()
        {
            string configoutput = string.Empty;

            switch (Scope)
            {
            case SearchConfigurationScope.Web:
            {
                configoutput = SelectedWeb.GetSearchConfiguration();
                break;
            }

            case SearchConfigurationScope.Site:
            {
                configoutput = ClientContext.Site.GetSearchConfiguration();
                break;
            }

            case SearchConfigurationScope.Subscription:
            {
                if (!ClientContext.Url.ToLower().Contains("-admin"))
                {
                    throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
                }

                SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
                var config = new SearchConfigurationPortability(ClientContext);
                ClientResult <string> configuration = config.ExportSearchConfiguration(owningScope);
                ClientContext.ExecuteQueryRetry(10, 60 * 5 * 1000);

                configoutput = configuration.Value;
            }
            break;
            }

            if (Path != null)
            {
                if (!System.IO.Path.IsPathRooted(Path))
                {
                    Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
                }
                System.IO.File.WriteAllText(Path, configoutput);
            }
            else
            {
                WriteObject(configoutput);
            }
        }
        public override void Export()
        {
            var ctx = base.AcquireContext();
            //var site = base.GetSite();
            SearchConfigurationPortability conf = new SearchConfigurationPortability(ctx);
            SearchObjectOwner owner             = new SearchObjectOwner(ctx, SearchObjectLevel.SPSiteSubscription);
            var buff = conf.ExportSearchConfiguration(owner);

            ctx.ExecuteQuery();
            var dir  = Path.Combine(Provider.Connector.Parameters["ConnectionString"].ToString(), $"{ExportFolderName}");
            var path = Path.Combine($"{dir}\\SearchSettings.xml");

            System.IO.Directory.CreateDirectory(dir);

            System.IO.File.WriteAllText(path, buff.Value);
        }
Example #19
0
        /// <summary>
        /// Delete the search configuration at the specified object level - does not apply to managed properties.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="searchObjectLevel"></param>
        /// <param name="searchConfiguration"></param>
        private static void DeleteSearchConfigurationImplementation(ClientRuntimeContext context, SearchObjectLevel searchObjectLevel, string searchConfiguration)
        {
#if ONPREMISES
            if (searchObjectLevel == SearchObjectLevel.Ssa)
            {
                // Reference: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.portability.searchconfigurationportability_members.aspx
                throw new Exception("You cannot import customized search configuration settings to a Search service application (SSA).");
            }
#endif
            SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchObjectLevel);

            // Delete search configuration
            searchConfig.DeleteSearchConfiguration(owner, searchConfiguration);
            context.Load(searchConfig);
            context.ExecuteQueryRetry();
        }
        protected override void ExecuteCmdlet()
        {
            string configoutput = string.Empty;

            switch (Scope)
            {
                case SearchConfigurationScope.Web:
                    {
                        configoutput = SelectedWeb.GetSearchConfiguration();
                        break;
                    }
                case SearchConfigurationScope.Site:
                    {
                        configoutput = ClientContext.Site.GetSearchConfiguration();
                        break;
                    }
                case SearchConfigurationScope.Subscription:
                    {
                        if (!ClientContext.Url.ToLower().Contains("-admin"))
                        {
                            throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
                        }

                        SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
                        var config = new SearchConfigurationPortability(ClientContext);
                        ClientResult<string> configuration = config.ExportSearchConfiguration(owningScope);
                        ClientContext.ExecuteQueryRetry(10, 60 * 5 * 1000);

                        configoutput = configuration.Value;
                    }
                    break;
            }

            if (Path != null)
            {
                if (!System.IO.Path.IsPathRooted(Path))
                {
                    Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
                }
                System.IO.File.WriteAllText(Path, configoutput);
            }
            else
            {
                WriteObject(configoutput);
            }
        }
Example #21
0
        private static void ExportSearchSettings(ClientContext context, string settingsFile)
        {
            ConsoleColor defaultForeground = Console.ForegroundColor;

            /*
             * SearchConfigurationPortability Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.search.portability.searchconfigurationportability(v=office.15).aspx
             *
             * SearchObjectOwner Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.office.server.search.administration.searchobjectowner(v=office.15).aspx
             */
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPWeb);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Writing out search configuration settings from: " + context.Web.Title);
            Console.ForegroundColor = defaultForeground;

            ClientResult <string> configresults = sconfig.ExportSearchConfiguration(owner);

            context.ExecuteQuery();

            if (configresults.Value != null)
            {
                string results = configresults.Value;
                System.IO.File.WriteAllText(settingsFile, results, Encoding.ASCII);

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Search settings have been exported. Press any key to continue");
                Console.ForegroundColor = defaultForeground;
                Console.Read();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("No search settings configuration results were returned. Press any key to continue");
                Console.ForegroundColor = defaultForeground;
                Console.Read();
            }
        }
Example #22
0
        /// <summary>
        /// Imports search settings from file.
        /// </summary>
        /// <param name="context">Context for SharePoint objects and operations</param>
        /// <param name="searchSchemaImportFilePath">Search schema xml file path</param>
        /// <param name="searchSettingsImportLevel">Search settings import level
        /// Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.administration.searchobjectlevel(v=office.15).aspx
        /// </param>
        public static void ImportSearchSettings(this ClientContext context, string searchSchemaImportFilePath, SearchObjectLevel searchSettingsImportLevel)
        {
            if (string.IsNullOrEmpty(searchSchemaImportFilePath))
            {
                throw new ArgumentNullException("searchSchemaImportFilePath");
            }

#if CLIENTSDKV15
            if (searchSettingsImportLevel == SearchObjectLevel.Ssa)
            {
                // Reference: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.portability.searchconfigurationportability_members.aspx
                throw new Exception("You cannot import customized search configuration settings to a Search service application (SSA).");
            }
#endif
            SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsImportLevel);

            // Import search configuration
            searchConfig.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(searchSchemaImportFilePath));
            context.Load(searchConfig);
            context.ExecuteQueryRetry();
        }
Example #23
0
        public void ApplySearchSchema(string templatePath, string SPUrl, string tenantAdminUser, string tenantAdminPassword, SearchObjectLevel level)
        {
            try
            {
                using (ClientContext ctx = new ClientContext(SPUrl))
                {
                    ctx.Credentials =
                        new SharePointOnlineCredentials(
                            tenantAdminUser,
                            tenantAdminPassword.GetSecureString());

                    SearchConfigurationPortability conf = new SearchConfigurationPortability(ctx);
                    SearchObjectOwner owner             = new SearchObjectOwner(ctx, level);
                    conf.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(templatePath));
                    ctx.ExecuteQuery();
                }
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine($"##vso[task.logissue type=error] Failed to apply search settings with error {e.Message}");
            }
        }
Example #24
0
        /// <summary>
        /// Imports search settings from file.
        /// </summary>
        /// <param name="context">Context for SharePoint objects and operations</param>
        /// <param name="searchSchemaImportFilePath">Search schema xml file path</param>
        /// <param name="searchSettingsImportLevel">Search settings import level
        /// Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.administration.searchobjectlevel(v=office.15).aspx
        /// </param>
        public static void ImportSearchSettings(this ClientContext context, string searchSchemaImportFilePath, SearchObjectLevel searchSettingsImportLevel)
        {
            if (string.IsNullOrEmpty(searchSchemaImportFilePath))
            {
                throw new ArgumentNullException("searchSchemaImportFilePath");
            }

            #if CLIENTSDKV15
            if (searchSettingsImportLevel == SearchObjectLevel.Ssa)
            {
                // Reference: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.portability.searchconfigurationportability_members.aspx
                throw new Exception("You cannot import customized search configuration settings to a Search service application (SSA).");
            }
            #endif
            SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsImportLevel);

            // Import search configuration
            searchConfig.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(searchSchemaImportFilePath));
            context.Load(searchConfig);
            context.ExecuteQuery();
        }
Example #25
0
        /// <summary>
        /// Exports the search settings to file.
        /// </summary>
        /// <param name="context">Context for SharePoint objects and operations</param>
        /// <param name="exportFilePath">Path where to export the search settings</param>
        /// <param name="searchSettingsExportLevel">Search settings export level
        /// Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.administration.searchobjectlevel(v=office.15).aspx
        /// </param>
        public static void ExportSearchSettings(this ClientContext context, string exportFilePath, SearchObjectLevel searchSettingsExportLevel)
        {
            if (string.IsNullOrEmpty(exportFilePath))
            {
                throw new ArgumentNullException("exportFilePath");
            }

            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsExportLevel);

            ClientResult<string> configresults = sconfig.ExportSearchConfiguration(owner);
            context.ExecuteQuery();

            if (configresults.Value != null)
            {
                string results = configresults.Value;
                System.IO.File.WriteAllText(exportFilePath, results, Encoding.ASCII);
            }
            else
            {
                throw new Exception("No search settings to export.");
            }
        }
Example #26
0
        /// <summary>
        /// Exports the search settings to file.
        /// </summary>
        /// <param name="context">Context for SharePoint objects and operations</param>
        /// <param name="exportFilePath">Path where to export the search settings</param>
        /// <param name="searchSettingsExportLevel">Search settings export level
        /// Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.administration.searchobjectlevel(v=office.15).aspx
        /// </param>
        public static void ExportSearchSettings(this ClientContext context, string exportFilePath, SearchObjectLevel searchSettingsExportLevel)
        {
            if (string.IsNullOrEmpty(exportFilePath))
            {
                throw new ArgumentNullException("exportFilePath");
            }

            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchSettingsExportLevel);

            ClientResult <string> configresults = sconfig.ExportSearchConfiguration(owner);

            context.ExecuteQuery();

            if (configresults.Value != null)
            {
                string results = configresults.Value;
                System.IO.File.WriteAllText(exportFilePath, results, Encoding.ASCII);
            }
            else
            {
                throw new Exception("No search settings to export.");
            }
        }
Example #27
0
        private static void ImportSearchSettings(ClientContext context, string settingsFile)
        {
            ConsoleColor defaultForeground = Console.ForegroundColor;

            /*
            * SearchConfigurationPortability Class
            * http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.search.portability.searchconfigurationportability(v=office.15).aspx
            * 
            * SearchObjectOwner Class
            * http://msdn.microsoft.com/en-us/library/office/microsoft.office.server.search.administration.searchobjectowner(v=office.15).aspx
           */
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPWeb);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Importing search configuration settings to: " + context.Web.Title);
            Console.ForegroundColor = defaultForeground;

            sconfig.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(settingsFile));
            context.ExecuteQuery();            

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Search settings have been imported. Press any key to continue");
            Console.ForegroundColor = defaultForeground;
            Console.Read();            
        }
Example #28
0
        /// <summary>
        /// Sets the search configuration at the specified object level
        /// </summary>
        /// <param name="context"></param>
        /// <param name="searchObjectLevel"></param>
        /// <param name="searchConfiguration"></param>
        private static void SetSearchConfigurationImplementation(ClientRuntimeContext context, SearchObjectLevel searchObjectLevel, string searchConfiguration)
        {
#if CLIENTSDKV15
            if (searchObjectLevel == SearchObjectLevel.Ssa)
            {
                // Reference: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.portability.searchconfigurationportability_members.aspx
                throw new Exception("You cannot import customized search configuration settings to a Search service application (SSA).");
            }
#endif
            SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, searchObjectLevel);

            // Import search configuration
            searchConfig.ImportSearchConfiguration(owner, searchConfiguration);
            context.Load(searchConfig);
            context.ExecuteQueryRetry();
        }
Example #29
0
        protected override void ExecuteCmdlet()
        {
            string configOutput = string.Empty;

            switch (Scope)
            {
            case SearchConfigurationScope.Web:
            {
                configOutput = SelectedWeb.GetSearchConfiguration();
                break;
            }

            case SearchConfigurationScope.Site:
            {
                configOutput = ClientContext.Site.GetSearchConfiguration();
                break;
            }

            case SearchConfigurationScope.Subscription:
            {
                if (!ClientContext.Url.ToLower().Contains("-admin"))
                {
                    throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
                }

                SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
                var config = new SearchConfigurationPortability(ClientContext);
                ClientResult <string> configuration = config.ExportSearchConfiguration(owningScope);
                ClientContext.ExecuteQueryRetry(10, 60 * 5 * 1000);

                configOutput = configuration.Value;
            }
            break;
            }

            if (Path != null)
            {
                if (!System.IO.Path.IsPathRooted(Path))
                {
                    Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
                }
                System.IO.File.WriteAllText(Path, configOutput);
            }
            else
            {
                if (OutputFormat == OutputFormat.CompleteXml)
                {
                    WriteObject(configOutput);
                }
                else if (OutputFormat == OutputFormat.ManagedPropertyMappings)
                {
                    StringReader sr  = new StringReader(configOutput);
                    var          doc = XDocument.Load(sr);
                    var          mps = GetCustomManagedProperties(doc);

                    foreach (var mp in mps)
                    {
                        mp.Aliases  = new List <string>();
                        mp.Mappings = new List <string>();

                        var mappings = GetCpMappingsFromPid(doc, mp.Pid);
                        mp.Mappings = mappings;
                        var aliases = GetAliasesFromPid(doc, mp.Pid);
                        mp.Aliases = aliases;
                    }
                    WriteObject(mps);
                }
            }
        }
Example #30
0
        /// <summary>
        /// Returns result source id for Matter Center result source
        /// </summary>
        /// <param name="login">Login detail</param>
        /// <param name="password">Password</param>
        /// <returns>Result source id</returns>
        private static string GetResultSourceId(string login, string password)
        {
            string filePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + ConstantStrings.BACKSLASH + ConfigurationManager.AppSettings["filename"];
            string configSheet = ConfigurationManager.AppSettings["manifestSheetname"];
            Dictionary<string, string> ConfigDetails = ExcelOperations.ReadFromExcel(filePath, configSheet);
            string url = ConfigDetails[ConfigurationManager.AppSettings["TenantSiteUrlKey"]].TrimEnd(ConstantStrings.FRONTSLASH);
            string resultSourceID = null;

            try
            {
                using (ClientContext clientContext = ConfigureSharePointContext.ConfigureClientContext(url, login, password))
                {
                    ClientRuntimeContext context = clientContext;
                    SearchConfigurationPortability searchConfigPortability = new SearchConfigurationPortability(context);
                    SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPSite);
                    ClientResult<string> searchConfigXML = searchConfigPortability.ExportSearchConfiguration(owner);
                    context.ExecuteQuery();

                    using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(searchConfigXML.Value)))
                    {
                        string id = ConfigurationManager.AppSettings["XMLNodeId"], resultSourceName = ConfigurationManager.AppSettings["ResultSourceName"];

                        if (0 < stream.Length)
                        {
                            XDocument xmlDoc = XDocument.Load(stream);
                            XElement element = null;
                            GetResultSource(resultSourceName, xmlDoc, ref element);
                            if (null != element)
                            {
                                resultSourceID = element.Descendants().Where(x => x.Name.LocalName.Equals(id)).FirstOrDefault().Value;
                            }
                            else
                            {
                                string errorMessage = "Result Source ID not found.";
                                ErrorLogger.DisplayErrorMessage(errorMessage);
                                throw new Exception(errorMessage);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorPath, ConstantStrings.MESSAGE + exception.Message + ConstantStrings.STACKTRACE + exception.StackTrace);
            }
            return resultSourceID;
        }
Example #31
0
        /// <summary>
        /// Update Search Configuration file and set new Search Term for restricting search scope
        /// </summary>
        /// <param name="filePath">path of the configuration Excel</param>
        /// <param name="login">Login detail</param>
        /// <param name="password">Password</param>
        public static void UpdateSearchConfig(string filePath, string login, string password)
        {
            // 1. Read Configuration Excel and form Search Term            
            string clientSheetName = ConfigurationManager.AppSettings["manifestSheetname"], xmlQueryTemplate = ConfigurationManager.AppSettings["XMLNodeQueryTemplate"];
            Dictionary<string, string> constantsList = ExcelOperations.ReadFromExcel(filePath, clientSheetName);

            string updatedSearchTerm;
            using (ClientContext context = ConfigureSharePointContext.ConfigureClientContext(constantsList[ConstantStrings.TENANT_ADMIN_URL], login, password))
            {
                string groupName = ConfigurationManager.AppSettings["PracticeGroupName"];
                string termSetName = ConfigurationManager.AppSettings["TermSetName"];
                string clientIdProperty = ConfigurationManager.AppSettings["ClientIDProperty"];
                string clientUrlProperty = ConfigurationManager.AppSettings["ClientUrlProperty"];
                ClientTermSets clientTermSets = TermStoreOperations.GetClientDetails(context, groupName, termSetName, clientIdProperty, clientUrlProperty);
                updatedSearchTerm = GetSearchTerm(clientTermSets);
            }

            // 2. Read Search Configuration XML file
            string resultSourceSearchConfigFilename = ConfigurationManager.AppSettings["ResultSourceXML"], resultSourceName = ConfigurationManager.AppSettings["ResultSourceName"],
            mangedPropertySearchConfigFileName = ConfigurationManager.AppSettings["SearchConfigXML"];
            string xmlFolderPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.GetDirectories(ConfigurationManager.AppSettings["StaticContentFolder"]).FirstOrDefault().GetDirectories(ConfigurationManager.AppSettings["XMLFolderName"]).FirstOrDefault().FullName;
            string resultSourceSearchConfigPath = string.Concat(xmlFolderPath, ConstantStrings.BACKSLASH, resultSourceSearchConfigFilename);
            string managedPropertySearchConfigPath = string.Concat(xmlFolderPath, ConstantStrings.BACKSLASH, mangedPropertySearchConfigFileName);
            XDocument resultSourceXml = XDocument.Load(resultSourceSearchConfigPath);
            XDocument managedPropertyXml = XDocument.Load(managedPropertySearchConfigPath);
            XElement resultSource = null;

            GetResultSource(resultSourceName, resultSourceXml, ref resultSource); // Existing result source

            if (null != resultSource)
            {
                XElement queryTemplate = resultSource.Descendants().Where(item => item.Name.LocalName.Equals(xmlQueryTemplate)).FirstOrDefault();
                // 3. Update XML file and set updated search term
                if (null != queryTemplate)
                {
                    queryTemplate.Value = updatedSearchTerm;
                    resultSourceXml.Save(resultSourceSearchConfigPath);
                }
                else
                {
                    string errorMessage = "Query Template not found in Search Configuration";
                    ErrorLogger.DisplayErrorMessage(errorMessage);
                    ErrorLogger.LogErrorToTextFile(errorPath, errorMessage);
                }
            }

            try
            {
                // 4. Upload Search Configuration to SharePoint, Import search configuration For creating Result Source at Catalog Level
                string configSheet = ConfigurationManager.AppSettings["manifestSheetname"];
                Dictionary<string, string> configDetails = ExcelOperations.ReadFromExcel(filePath, configSheet);
                string url = configDetails[ConfigurationManager.AppSettings["TenantSiteUrlKey"]].TrimEnd();
                using (ClientContext clientContext = ConfigureSharePointContext.ConfigureClientContext(url, login, password))
                {
                    ClientRuntimeContext context = clientContext;
                    SearchConfigurationPortability searchConfigPortability = new SearchConfigurationPortability(context);
                    SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPSite);
                    searchConfigPortability.ImportSearchConfiguration(owner, resultSourceXml.ToString());
                    context.ExecuteQuery();
                    Console.WriteLine("Imported search configuration and created result source.");
                }

                // 5. Upload Search Configuration tenant level and create managed properties
                url = configDetails[ConstantStrings.TENANT_ADMIN_URL].Trim();
                using (ClientContext clientContext = ConfigureSharePointContext.ConfigureClientContext(url, login, password))
                {
                    ClientRuntimeContext context = clientContext;
                    SearchConfigurationPortability searchConfigPortability = new SearchConfigurationPortability(context);
                    SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPSiteSubscription);
                    searchConfigPortability.ImportSearchConfiguration(owner, managedPropertyXml.ToString());
                    context.ExecuteQuery();
                    Console.WriteLine("Imported search configuration and created search schema.");
                }
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorPath, ConstantStrings.MESSAGE + exception.Message + ConstantStrings.STACKTRACE + exception.StackTrace);
            }
        }
Example #32
0
        private static void ExportSearchSettings(ClientContext context, string settingsFile)
        {
            ConsoleColor defaultForeground = Console.ForegroundColor;
            /*
             * SearchConfigurationPortability Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.search.portability.searchconfigurationportability(v=office.15).aspx
             * 
             * SearchObjectOwner Class
             * http://msdn.microsoft.com/en-us/library/office/microsoft.office.server.search.administration.searchobjectowner(v=office.15).aspx
            */
            SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context);
            SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPWeb);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Writing out search configuration settings from: " + context.Web.Title);
            Console.ForegroundColor = defaultForeground;

            ClientResult<string> configresults = sconfig.ExportSearchConfiguration(owner);
            context.ExecuteQuery();

            if (configresults.Value != null)
            {
                string results = configresults.Value;
                System.IO.File.WriteAllText(settingsFile, results, Encoding.ASCII);
                
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Search settings have been exported. Press any key to continue");
                Console.ForegroundColor = defaultForeground;
                Console.Read();                
            }
            else
            {                
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("No search settings configuration results were returned. Press any key to continue");
                Console.ForegroundColor = defaultForeground;
                Console.Read();
            }            
        }