using System;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace modulerix_Navigation_Tools_Global_Menu.CONTROLTEMPLATES.modulerixNavigationToolsGlobalMenu
{
public partial class modulerixNavigationToolsGlobalMenu : UserControl
{
public string Scope { get; set; }
public int Count { get; set; }
public int DisableMenu { get; set; }
public List<string> IDs { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Control menucontrol = GetControl(Page, "TopNavigationMenuV4");
if (menucontrol != null)
{
InitScope();
menucontrol.Visible = DisableMenu > 0 ? true : false;
var addTo = menucontrol.Parent.Parent.Controls;
for (int i = 0; i < Count; i++)
{
var webpart = CreateNavWebPart(IDs[i]);
if (webpart != null)
addTo.AddAt(0, webpart);
}
}
}
private WebPart CreateNavWebPart(string ID)
{
try
{
WebPart wp = (WebPart)Activator.CreateInstance("modulerix Navigation Tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5", "modulerix_Navigation_Tools.modulerix_Navigation_WebPart").Unwrap();
wp.ID = ID;
wp.SuppressWebPartChrome = true;
return wp;
}
catch { }
return null;
//return new modulerix_Navigation_WebPart
// {
// ID = "46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FF",
// SuppressWebPartChrome = true
// };
}
private void InitScope()
{
Count = Properties.GetValueByKey("modulerixNavigationMenuCount" + Scope);
DisableMenu = Properties.GetValueByKey("modulerixNavigationMenuDisable" + Scope);
switch (Scope)
{
case "Farm":
IDs = new List<string>
{
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FF",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FE",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FD",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FC",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FB",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7FA",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7F9",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7F8",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7F7",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE7F6"
};
break;
case "Application":
IDs = new List<string>
{
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FF",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FE",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FD",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FC",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FB",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6FA",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6F9",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6F8",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6F7",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE6F6"
};
break;
case "Site":
IDs = new List<string>
{
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FF",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FE",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FD",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FC",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FB",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5FA",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5F9",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5F8",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5F7",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE5F6"
};
break;
case "Web":
IDs = new List<string>
{
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FF",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FE",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FD",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FC",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FB",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4FA",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4F9",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4F8",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4F7",
"46D3C7D7-3928-4E56-B4E0-BE3F5A5DE4F6"
};
break;
}
}
private Control GetControl(Control rootControl, string controlID)
{
if (rootControl.ID == controlID)
return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = GetControl(controlToSearch, controlID);
if (controlToReturn != null)
return controlToReturn;
}
return null;
}
}
public class Properties
{
private static Dictionary<string, int> _values = new Dictionary<string, int>();
public static int GetValueByKey(string key)
{
if (!_values.ContainsKey(key))
_values.Add(key, GetPropertyValue(key));
return _values[key];
}
private static int GetPropertyValue(string propname)
{
int? _prop = 1;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
var farm = SPFarm.Local;
if (farm.Properties.ContainsKey(propname))
_prop = (int?)farm.Properties[propname];
var app = SPContext.Current.Site.WebApplication;
if (app.Properties.ContainsKey(propname))
_prop = (int?)app.Properties[propname];
var site = SPContext.Current.Site.RootWeb;
if (site.Properties.ContainsKey(propname))
_prop = site.GetProperty(propname) as int?;
var web = SPContext.Current.Web;
if (web.Properties.ContainsKey(propname))
_prop = web.GetProperty(propname) as int?;
});
_prop = _prop != null ? _prop : 1;
return (int)(_prop > 10 ? 10 : _prop);
}
}
}