I created a SharePoint Publishing helper class that can retrieve values for the following PublishingWeb properties and functions.
- GetAvailablePageLayouts
- GetIncludeInNavigation
- IncludeInCurrentNavigation
- IncludeInGlobalNavigation
- IncludePagesInNavigation
- IncludeSubSitesInNavigation
- InheritAlternateCssUrl
- InheritCurrentNavigation
- InheritCustomMasterUrl
- InheritGlobalNavigation
- IsInheritingAvailablePageLayouts
- IsInheritingAvailableWebTemplates
- IsPublishingWeb
- NavigationAutomaticSortingMethod
- NavigationShowSiblings
- NavigationSortAscending
- OrderingMethod
- PagesListId
public static class SharePointPublishingHelper
{
public static bool IsPublishingWeb(SPWeb web)
{
return GetBooleanValueFromPropertyBag(web, "__PublishingFeatureActivated", false);
}
public static bool IncludeInGlobalNavigation(SPWeb web)
{
return GetIncludeInNavigation(web, "__GlobalNavigationExcludes");
}
public static bool IncludeInCurrentNavigation(SPWeb web)
{
return GetIncludeInNavigation(web, "__CurrentNavigationExcludes");
}
private static bool GetIncludeInNavigation(SPWeb web, string propertyKey)
{
if(web == null)
{
throw new ArgumentNullException("web");
}
if (string.IsNullOrEmpty(propertyKey))
{
throw new ArgumentException("The argument can't be null or a empty string.", "propertyKey");
}
SPWeb parentWeb = web.ParentWeb;
if (!web.IsRootWeb && parentWeb != null)
{
string globalNavigationExcludes = GetValueFromPropertyBag(parentWeb, propertyKey) as string;
if (!string.IsNullOrEmpty(globalNavigationExcludes))
{
string[] list = globalNavigationExcludes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
bool found = list.Contains(web.ID.ToString("D"));
return !found;
}
}
return true;
}
public static bool InheritGlobalNavigation(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
if (web.IsRootWeb)
{
return false;
}
return web.Navigation.UseShared;
}
public static bool InheritCurrentNavigation(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
if (web.IsRootWeb)
{
return false;
}
return GetBooleanValueFromPropertyBag(web, "__InheritCurrentNavigation", true);
}
public static bool IncludeSubSitesInNavigation(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__IncludeSubSitesInNavigation", true);
}
public static bool IncludePagesInNavigation(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__IncludePagesInNavigation", true);
}
public static string OrderingMethod(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
string value = GetValueFromPropertyBag(web, "__NavigationOrderingMethod").ToString();
switch (value)
{
case "0":
return "Automatic";
case "1":
return "ManualWithAutomaticPageSorting";
case "2":
return "Manual";
default:
break;
}
return "Manual";
}
public static bool NavigationShowSiblings(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
if (web.IsRootWeb)
{
return false;
}
return GetBooleanValueFromPropertyBag(web, "__NavigationShowSiblings", true);
}
public static string NavigationAutomaticSortingMethod(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
string value = GetValueFromPropertyBag(web, "__NavigationAutomaticSortingMethod").ToString();
switch (value)
{
case "0":
return "Title";
case "1":
return "CreatedDate";
case "2":
return "LastModifiedDate";
default:
break;
}
return "Title";
}
public static bool NavigationSortAscending(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__NavigationSortAscending", true);
}
public static Guid PagesListId(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
string value = GetValueFromPropertyBag(web, "__PagesListId").ToString();
if (string.IsNullOrEmpty(value))
{
return Guid.Empty;
}
return new Guid(value);
}
public static bool InheritCustomMasterUrl(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__InheritsCustomMasterUrl", false);
}
public static bool InheritAlternateCssUrl(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__InheritsAlternateCssUrl", false);
}
public static bool IsInheritingAvailableWebTemplates(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
return GetBooleanValueFromPropertyBag(web, "__InheritWebTemplates", false);
}
public static bool IsInheritingAvailablePageLayouts(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
string value = GetValueFromPropertyBag(web, "__PageLayouts").ToString();
if (!string.IsNullOrEmpty(value))
{
return value.Equals("__inherit", StringComparison.InvariantCultureIgnoreCase);
}
return false;
}
public static string[] GetAvailablePageLayouts(SPWeb web)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
List list = new List();
string value = GetValueFromPropertyBag(web, "__PageLayouts").ToString();
if (!string.IsNullOrEmpty(value))
{
SPWeb rootWeb = web.Site.RootWeb;
var pageLayouts = XElement.Parse(value).Elements("layout");
foreach (var pageLayout in pageLayouts)
{
string itemUrl = null;
string id = null;
XAttribute guidAttribute = pageLayout.Attribute("guid");
if (guidAttribute != null)
{
id = guidAttribute.Value;
}
// first try to find pagelayout by id
if (!string.IsNullOrEmpty(id))
{
SPFile file = rootWeb.GetFile(new Guid(id));
if (file != null && file.Exists)
{
itemUrl = file.ServerRelativeUrl;
}
}
// if no pagelayout is found by id, try the url
if (string.IsNullOrEmpty(itemUrl))
{
string url = null;
XAttribute urlAttribute = pageLayout.Attribute("url");
if (urlAttribute != null)
{
url = urlAttribute.Value;
}
SPFile file = rootWeb.GetFile(url);
if (file != null && file.Exists)
{
itemUrl = file.ServerRelativeUrl;
}
}
if (!string.IsNullOrEmpty(itemUrl))
{
list.Add(itemUrl);
}
}
}
return list.ToArray();
}
private static object GetValueFromPropertyBag(SPWeb web, string key)
{
if (web == null)
{
throw new ArgumentNullException("web");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("The argument can't be null or a empty string.", "key");
}
object value = null;
// First check the AllProperties collection
if (web.AllProperties.ContainsKey(key))
{
value = web.AllProperties[key];
}
// Still empty, check also the Properties collection
if (value == null)
{
if (web.Properties.ContainsKey(key))
{
value = web.Properties[key];
}
}
return value;
}
private static bool GetBooleanValueFromPropertyBag(SPWeb web, string key, bool defaultValue)
{
string value = GetValueFromPropertyBag(web, key) as string;
bool ret;
if (!bool.TryParse(value, out ret))
{
ret = defaultValue;
}
return ret;
}
}
No comments:
Post a Comment