2

I have a farm wide feature that overrides the suite control, the substance of which is similar to the following:

public partial class SuiteLinksDelegateCtrl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {

            // Add CSS class to fix logo image height in subsites
            FixHeaderStylesHeight(writer);

            writer.RenderBeginTag(HtmlTextWriterTag.Style);
            writer.Write(".ms-core-suiteLinkList {display: inline-block;}");
            writer.RenderEndTag();

            // Hide elements pre-loading
            //writer.RenderBeginTag(HtmlTextWriterTag.Style);
            //writer.Write("#s4-bodyContainer {display: none;}");
            //writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-core-suiteLinkList");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            // todo: replace with SQL database call?
            // build out menu links
            AddSuiteLink(writer, "http://sharepoint", "Groups", "lnkGroups");
            AddSuiteLink(writer, "http://sharepoint", "Documents", "lnkDocuments");
            AddSuiteLink(writer, "http://sharepoint", "Projects", "lnkProjects");
            AddSuiteLink(writer, "http://sharepoint", "Services", "lnkServices");
            AddSuiteLink(writer, "http://sharepoint", "Teams", "lnkTeams");

            AddSuiteLink(writer, "http://sharepointpersonal/default.aspx", "Newsfeed", "lnkNewsfeed");
            AddSuiteLink(writer, "http://sharepointpersonal/_layouts/15/MySite.aspx?MySiteRedirect=AllSites", "Followed Sites", "lnkFollowedSites");

            writer.RenderEndTag();

            writer.Write("<script src='http://sharepoint/scripts/jquery-1.10.2.min.js'></script>");
            writer.Write("<script src='http://sharepoint/scripts/avdw-pkg.js'></script>");
            writer.Write("<script src='http://sharepoint/scripts/menu.js'></script>");
            writer.Write("<link rel='stylesheet' type='text/css' href='http://sharepoint/scripts/tiles.css' />");

            base.Render(writer);
        }

        protected static void FixHeaderStylesHeight(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Style);
            writer.Write(".ms-siteicon-img { max-height: 80px; }");
            writer.RenderEndTag();
        }

        /// <summary>
        /// Add a new link to the suite bar
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="url"></param>
        /// <param name="name"></param>
        /// <param name="linkId"></param>
        protected static void AddSuiteLink(HtmlTextWriter writer, string url, string name, string linkId)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-core-suiteLink");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-core-suiteLink-a");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, linkId);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-verticalAlignMiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(name);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }

I have a requirement to write some different CSS to this area when I'm on the main page, but not when I'm on the others.

Basically:

// Hide elements pre-loading
writer.RenderBeginTag(HtmlTextWriterTag.Style);
writer.Write("#s4-bodyContainer {display: none;}");
writer.RenderEndTag();

Should only execute if I'm on the home page, and not on any other page. Is something like this possible?

3

SOME UNREQUESTED ADVICE: You're doing a lot of crazy stuff in your control, adding jQuery in the Suites delegate and then not using it? if that is used somewhere else and/or globally take a look at the AdditionalPageHead delegate and use your own .ascx to add these things, there's no reason you should be doing this in the code behind. Even better if you have access to the masterpage.

That said, if you know the home and consider it to be static in its address you can simply check for:

if(Request.Url.AbsolutePath.EndsWith("/Home.aspx") || SPContext.Current.Site.ServerRelativeUrl == "/")
    //do stuff

You can also know the SharePoint Home based on the WelcomePage settings:

using(var web = SPContext.Current.Site.OpenWeb(SPContext.Current.Site.ServerRelativeUrl))
{
    var home = web.RootFolder.WelcomePage;
}

And then compare it to the Request.Url.AbsolutePath bits above.

  • JQuery is being used within the suite delegate, just not in this area (not everything included above) - more going on here then is explained in the post. – Michael A Nov 19 '13 at 1:11
  • Is there a way to have this not execute when I'm editing the page, only when the page is in display mode? – Michael A Nov 19 '13 at 1:15
  • Yes but doesn't have a definitive answer, take a look at stackoverflow.com/questions/9478913/… – Francisco Aquino Nov 19 '13 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.