Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Last active January 5, 2018 13:03
Show Gist options
  • Save jonathanread/d87035639d6b12918536a765809310fc to your computer and use it in GitHub Desktop.
Save jonathanread/d87035639d6b12918536a765809310fc to your computer and use it in GitHub Desktop.
Static helper class to retrieve provider name for current site and to get current site id
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Multisite;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace SitefinityWebApp.Helpers
{
public static class MultisiteHelpers
{
/// <summary>
/// Returns the provider name for the current site.
/// </summary>
/// <param name="moduleName">Name of the Sitefinity module you want to know the prover name.</param>
/// <returns>The sites provider name of empty if not found or in single site mode.</returns>
public static string GetCurrentProvider(string moduleName)
{
var context = SystemManager.CurrentContext;
string providerName = string.Empty;
if (moduleName.Contains("DynamicTypes"))
{
moduleName = TypeResolutionService.ResolveType(moduleName).FullName;
}
try
{
if (context.IsMultisiteMode)
{
providerName = context.MultisiteContext.CurrentSite.GetProviders(moduleName).Select(p => p.ProviderName).FirstOrDefault();
}
}
catch (Exception ex)
{
Log.Write("Provider not found for " + moduleName, System.Diagnostics.TraceEventType.Error);
Log.Write(ex, System.Diagnostics.TraceEventType.Error);
}
return providerName;
}
/// <summary>
/// Gets current site Id
/// </summary>
/// <returns>Guid or null</returns>
public static Guid? GetCurrentSiteId()
{
var context = SystemManager.CurrentContext;
Guid? siteId = null;
try
{
if (context.IsMultisiteMode)
{
siteId = context.MultisiteContext.CurrentSite.Id;
}
else
{
siteId = context.CurrentSite.Id;
}
}
catch (Exception ex)
{
Log.Write("Error retrieving site Id", System.Diagnostics.TraceEventType.Error);
Log.Write(ex, System.Diagnostics.TraceEventType.Error);
}
return siteId;
}
}
}
@Html.Raw("Site Id: <b>" + SitefinityWebApp.Helpers.MultisiteHelpers.GetCurrentSiteId() + "</b>")<br />
@Html.Raw("Slider Provider name: <b>" + SitefinityWebApp.Helpers.MultisiteHelpers.GetCurrentProvider("Sliders") + "</b>")
@*
Sliders in the name of a module from module builder.
This name can have spaces so be aware or see this knowledge base:
https://knowledgebase.progress.com/articles/Article/get-module-providers-for-site-in-multisite
Also note when accessing prebuilt modules use typeof(LibrariesManager).FullName to pass into the GetCurrentProvider() method
*@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment