/// <summary> /// Sets the search results page URL on current web (Site Settings -> Search --> Search Settings) /// </summary> /// <param name="web">SharePoint current web</param> /// <param name="searchCenterUrl">Search results page URL</param> public static void SetWebSearchCenterUrl(this Web web, string searchCenterUrl) { if (searchCenterUrl == null) { throw new ArgumentNullException(nameof(searchCenterUrl)); } // Currently there is no direct API available to set the search center URL on web. // Set search setting at web level #region Enable scripting if needed and context has access Tenant tenant = null; Site site = null; ClientContext adminContext = null; if (web.IsNoScriptSite() && TenantExtensions.IsCurrentUserTenantAdmin(web.Context as ClientContext)) { site = ((ClientContext)web.Context).Site; site.EnsureProperty(s => s.Url); var adminSiteUrl = web.GetTenantAdministrationUrl(); adminContext = web.Context.Clone(adminSiteUrl); tenant = new Tenant(adminContext); tenant.SetSiteProperties(site.Url, noScriptSite: false); } #endregion try { string keyName = web.IsSubSite() ? "SRCH_SB_SET_WEB" : "SRCH_SB_SET_SITE"; if (!string.IsNullOrEmpty(searchCenterUrl)) { // Set search results page URL web.SetPropertyBagValue(keyName, "{\"Inherit\":false,\"ResultsPageAddress\":\"" + searchCenterUrl + "\",\"ShowNavigation\":false}"); } else { // When search results page URL is blank remove the property (like the SharePoint UI does) web.RemovePropertyBagValue(keyName); } } catch (ServerUnauthorizedAccessException e) { const string errorMsg = "For modern sites you need to be a SharePoint admin when setting the search redirect URL programatically.\n\nPlease use the classic UI at '/_layouts/15/enhancedSearch.aspx?level=sitecol'."; Log.Error(e, Constants.LOGGING_SOURCE, errorMsg); throw new ApplicationException(errorMsg, e); } finally { #region Disable scripting if previously enabled if (adminContext != null) { // Reset disabling setting the property bag if needed tenant.SetSiteProperties(site.Url, noScriptSite: true); adminContext.Dispose(); } #endregion } }
/// <summary> /// Sets the search results page URL on current web (Site Settings -> Search --> Search Settings) /// </summary> /// <param name="web">SharePoint current web</param> /// <param name="searchCenterUrl">Search results page URL</param> public static void SetWebSearchCenterUrl(this Web web, string searchCenterUrl) { if (searchCenterUrl == null) { throw new ArgumentNullException(nameof(searchCenterUrl)); } // Currently there is no direct API available to set the search center URL on web. // Set search setting at web level #if !ONPREMISES #region Enable scripting if needed and context has access Tenant tenant = null; Site site = null; ClientContext adminContext = null; if (web.IsNoScriptSite() && TenantExtensions.IsCurrentUserTenantAdmin(web.Context as ClientContext)) { site = ((ClientContext)web.Context).Site; site.EnsureProperty(s => s.Url); var adminSiteUrl = web.GetTenantAdministrationUrl(); adminContext = web.Context.Clone(adminSiteUrl); tenant = new Tenant(adminContext); tenant.SetSiteProperties(site.Url, noScriptSite: false); } #endregion #endif try { string keyName = web.IsSubSite() ? "SRCH_SB_SET_WEB" : "SRCH_SB_SET_SITE"; if (!string.IsNullOrEmpty(searchCenterUrl)) { // Set search results page URL web.SetPropertyBagValue(keyName, "{\"Inherit\":false,\"ResultsPageAddress\":\"" + searchCenterUrl + "\",\"ShowNavigation\":false}"); } else { // When search results page URL is blank remove the property (like the SharePoint UI does) web.RemovePropertyBagValue(keyName); } } finally { #if !ONPREMISES #region Disable scripting if previously enabled if (adminContext != null) { // Reset disabling setting the property bag if needed tenant.SetSiteProperties(site.Url, noScriptSite: true); adminContext.Dispose(); } #endregion #endif } }
private static void SetSearchBoxPlaceholderTextImpl(Web web, string placeholderText, bool siteScope) { if (placeholderText == null) { throw new ArgumentNullException(nameof(placeholderText)); } ClientContext adminContext = null; var ctx = ((ClientContext)web.Context); ctx.Load(ctx.Web, w => w.EffectiveBasePermissions); // reload permissions ctx.ExecuteQueryRetry(); Site site = ctx.Site; #region Enable scripting if needed and context has access Tenant tenant = null; if (ctx.Web.IsNoScriptSite() && TenantExtensions.IsCurrentUserTenantAdmin(ctx)) { site.EnsureProperty(s => s.Url); var adminSiteUrl = web.GetTenantAdministrationUrl(); adminContext = ctx.Clone(adminSiteUrl); tenant = new Tenant(adminContext); tenant.SetSiteProperties(site.Url, noScriptSite: false); } #endregion try { if (siteScope) { site.SearchBoxPlaceholderText = placeholderText; } else { web.SearchBoxPlaceholderText = placeholderText; web.Update(); } ctx.ExecuteQueryRetry(); } finally { #region Disable scripting if previously enabled if (adminContext != null) { // Reset disabling setting the property bag if needed tenant.SetSiteProperties(site.Url, noScriptSite: true); adminContext.Dispose(); } #endregion } }
/// <summary> /// Sets the search center URL on site collection (Site Settings -> Site collection administration --> Search Settings) /// </summary> /// <param name="web">SharePoint site - root web</param> /// <param name="searchCenterUrl">Search center URL</param> public static void SetSiteCollectionSearchCenterUrl(this Web web, string searchCenterUrl) { if (searchCenterUrl == null) { throw new ArgumentNullException(nameof(searchCenterUrl)); } // Currently there is no direct API available to set the search center URL on web. // Set search setting at site level #if !ONPREMISES #region Enable scripting if needed and context has access Tenant tenant = null; Site site = null; ClientContext adminContext = null; var ctx = ((ClientContext)web.Context); ctx.Load(ctx.Web, w => w.EffectiveBasePermissions); // reload permissions in case changed after connect ctx.ExecuteQueryRetry(); if (ctx.Web.IsNoScriptSite() && TenantExtensions.IsCurrentUserTenantAdmin(web.Context as ClientContext)) { site = ((ClientContext)web.Context).Site; site.EnsureProperty(s => s.Url); var adminSiteUrl = web.GetTenantAdministrationUrl(); adminContext = web.Context.Clone(adminSiteUrl); tenant = new Tenant(adminContext); tenant.SetSiteProperties(site.Url, noScriptSite: false); } #endregion #endif try { // if another value was set then respect that if (String.IsNullOrEmpty(web.GetPropertyBagValueString("SRCH_SB_SET_SITE", string.Empty))) { web.SetPropertyBagValue("SRCH_SB_SET_SITE", "{'Inherit':false,'ResultsPageAddress':null,'ShowNavigation':true}"); } if (!string.IsNullOrEmpty(searchCenterUrl)) { // Set search center URL web.SetPropertyBagValue("SRCH_ENH_FTR_URL_SITE", searchCenterUrl); } else { // When search center URL is blank remove the property (like the SharePoint UI does) web.RemovePropertyBagValue("SRCH_ENH_FTR_URL_SITE"); } } finally { #if !ONPREMISES #region Disable scripting if previously enabled if (adminContext != null) { // Reset disabling setting the property bag if needed tenant.SetSiteProperties(site.Url, noScriptSite: true); adminContext.Dispose(); } #endregion #endif } }