Branding

Provisionate MasterPage and CSS with Feature

Posted on Updated on

Sometimes, when you don’t want to use Powershell, you can set your branding from a Feature, it’s the old procedure but very “elastic”.

In the bellow sample, I’ll give you the option to change you master page to Front Office and Back Office, you can also define your CSS File.

I’m processing all Sub Webs also, just change what you want. This is a Feature SCOPE Site

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (var site = properties.Feature.Parent as SPSite)
    {
        if (site == null) return;
        using (var web = site.OpenWeb())
        { 
            var masterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/YOURCustomFO.master" : web.Site.ServerRelativeUrl + 
                                                            "/_catalogs/masterpage/YOURCustomFO.master";
            var customMasterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/seattle.master" : web.Site.ServerRelativeUrl + 
                                                         "/_catalogs/masterpage/seattle.master";
            //string cssUrl = web.Site.ServerRelativeUrl == "/" ? "/Style Library/xx.css" : web.Site.ServerRelativeUrl + "/Style Library/xx.css";

            #region MasterPage e CSS

            // Define Master page for FO and BO
            web.CustomMasterUrl = masterUrl;
            web.MasterUrl = customMasterUrl;

            //Alternate CSS URL 
            //web.AlternateCssUrl = cssUrl;

            // inheritance settings
            web.AllProperties["__InheritsMasterUrl"] = "True";
            web.AllProperties["__InheritsCustomMasterUrl"] = "True";
            //web.AllProperties["__InheritsAlternateCssUrl"] = "True";

            web.Update();

            foreach (SPWeb subWeb in web.Webs)
            {
                try
                {
                    ProcessSubWebs(subWeb);
                }
                finally 
                {
                    if (subWeb != null)
                        subWeb.Dispose();
                }
            }
            #endregion
        }
    }
} 

Some things you want to hide in your Master Page

Posted on Updated on

<style>            
	/* Turns off the border on the bottom of the tabs */
	.ms-cui-ribbonTopBars > div {
		border-bottom: 1px solid transparent !important;
	} 
	/* Hide Ribbon */
	#s4-ribbonrow {
		display: none;
	}

	/* Hide Suite Bar */
	#suiteBar {
		display: none;
	}

	/* Remove Left Navigation Bar */
	#sideNavBox {
		DISPLAY: none;
	}

	/* Remove Logo */
	#s4-titlerow {
		display: none !important;
	}
</style> 

Hide Ribbon and Suite Bar from Anonymous Users

Posted on Updated on

SharePoint 2013, like 2010 have Trimmed Controls in order to define display rules for Authentications Roles. In SharePoint 2013 we have the Suite Bar, you must hide it also as Trimmed like this

Find the <body in Master Page and add the <style>

<body>
    <style>
        #s4-ribbonrow {
            display: none;
        }
    </style>
    <style>
        #suiteBar {
            display: none;
        }
    </style>
 
Add the Trimmed Control for before <div id=”s4-titlerow” for permissions PermissionsString=”AddAndCustomizePages”
 

<SharePoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControlRibbon" runat="server" PermissionsString="AddAndCustomizePages">
    <script type="text/javascript">
        document.getElementById("s4-ribbonrow").style.display = "block";
        document.getElementById("suiteBar").style.display = "block";
    </script>
</SharePoint:SPSecurityTrimmedControl> 

Change SharePoint Master Page PlaceHolderPageTitle with jQuery

Posted on Updated on

In SharePoint, the object “PlaceHolderPageTitle” is responsible to render the Title. So if you have a property to bind the text, you should be able to render like this:

Property to include object titles

<SharePoint:ProjectProperty Property="Title" runat="server" />

In the Master Page

<SharePoint:PageTitle runat="server">
      <asp:ContentPlaceHolder id="PlaceHolderPageTitle" runat="server"><SharePoint:ProjectProperty Property="Title" runat="server" /></asp:ContentPlaceHolder>
 </SharePoint:PageTitle>  
 
The Span that Renders the Place Holder is:
 
<span id="DeltaPlaceHolderPageTitleInTitleArea">
     <asp:ContentPlaceHolder ID="PlaceHolderPageTitleInTitleArea" runat="server" />
</span>  
 

Using jQuery, you can change what ever you want, in this case the object Title.

I suggest to use ExecuteOrDelayUntilScriptLoaded because in this case you are sure that the DOM is loaded, preventing conflicts between jQuery functions

Change Title

<script type="text/javascript">
  ExecuteOrDelayUntilScriptLoaded(function () {    
    $('span#DeltaPlaceHolderPageTitleInTitleArea').each(function () {      
         $(this).empty();
         $(this).append(“OK i want this one”);
    });
}, "sp.js"); 
</script> 
 
Change if specific number of characters reached
 
<script type="text/javascript">
  ExecuteOrDelayUntilScriptLoaded(function () {    
    $('span#DeltaPlaceHolderPageTitleInTitleArea').each(function () {
        if ($(this).text().length >= 103) {
            var tituloPageTitle = $(this).text().substr(0, 100) + "...";
            $(this).empty();
            $(this).append(tituloPageTitle);
        }
    });
}, "sp.js");
</script> 
 

Good Luck 😉

Add Enterprise Keywords to List with Receiver

Posted on Updated on

Let’s see, a easy tag field is the “TaxKeyword”, the default for “Enterprise Keywords”. The advantage is that he is already at the Search Schema an Indexed, so you can use in all objects that you want, Page Layouts for Publishing Pages, Lists, whatever.

How to do it in a SharePoint Feature Receiver?

SCOPE: Web

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var web = properties.Feature.Parent as SPWeb;
    AddDocs(web);
}

private void AddDocs(SPWeb web)
{
    if (web == null) return;
    var list = web.Lists.TryGetList("Documents");
    if (list != null)
    {
        var field = list.ParentWeb.AvailableFields["Enterprise Keywords"];
        if (!list.Fields.ContainsField("Enterprise Keywords"))
        {
            list.Fields.Add(field);
            list.EnableModeration = true;
            list.EnableVersioning = true;

            var view = list.Views[0];
            view.ViewFields.Add(field);
            view.Update();

            list.Update();
            web.Dispose();
        }
        else
        {
            //Log
        }
    }
    else
    {
        //Log
    }
} 

 

How to add it to a Page Layout?

<%@ Register TagPrefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


 <Taxonomy:TaxonomyFieldControl FieldName="TaxKeyword" InputFieldLabel="My Custom Tags" runat="server"></Taxonomy:TaxonomyFieldControl> 

The security validation for this page is invalid

Posted on Updated on

When working with custom branding in SharePoint 2013, sometimes it’s easy to make a mistake. For instance, you area changing a BackOffice Master Page and when you add a page, after you fill the Title the error appears

the security validation for this page is invalid

The common problem is you deleted the next Place Holder

<asp:ContentPlaceHolder id="PlaceHolderFormDigest" runat="server">
        <SharePoint:FormDigest runat="server"/>
</asp:ContentPlaceHolder> 
 

Just add it back and it should be fine