1) Create a new Web Control
The web control shall retrieve the description from your top level site.
In your Visual Studio project, right click and choose Add > New Item... In the New Item dialogue select Module. Give the new module a name (SiteDescription) and click OK.
Remove the txt file.
Add a class (Add > Class...), give it a name (SiteDescription) and click Add.
The source code of your class will look something like this:
namespace NameSpace.SiteDescription
{
/// <summary>
/// SiteDescription class is used to displayes the description of the top level web.
/// </summary>
[ToolboxData("<{0}:SiteDescription runat=server></{0}:SiteDescription>")]
public class SiteDescription : WebControl
{
private string _alt = "- default description of site";
/// <summary>
/// override so that the surrounding tag is a div insteadof a span
/// </summary>
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Div; }
}
/// <summary>
/// Add the description as an em tag
/// </summary>
/// <param name="writer"></param>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust"
)]
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Em);
writer.WriteEncodedText(_alt);
writer.RenderEndTag();
}
private void InitiateDescription()
{
try
{
var webApp = SPContext.Current.Site.WebApplication;
SPWeb siteToUse = SPContext.Current.Web;
foreach (SPSite site in webApp.Sites)
{
if (site.RootWeb.Title.Equals("Title")) /* your title...*/
{
siteToUse = site.RootWeb;
break;
}
}
if (!String.IsNullOrEmpty(siteToUse.Description))
{
_alt = siteToUse.Description;
}
}
catch (Exception e)
{
/*error handling*/
}
}
/// <summary>
/// Create new web control
/// </summary>
public BannerDescription()
{
InitiateDescription();
}
}
}
Edit the Elements.xml file of your new Web Control:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control
Id="SiteDescription"
Sequence="24"
ControlClass="Namespace.SiteDescription.SiteDescription"
ControlAssembly="Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxx">
</Control>
</Elements>
2) Use your description web control in your master page
<NameSpace:SiteDescription ID="SiteDescriptionWebControl" runat="server"/>
Visual Studio will prompt you with the right import statements.
No comments:
Post a Comment