How to Apply/Inject Custom CSS into SharePoint Master Page in Office 365?

Hi All,

In office 365 environment, Microsoft had recommended not to customize the master pages. So to avoid customization, we can inject CSS at run time to Office 365 Master Pages. This can be achieved using the below CSOM Code in any provider hosted model app development. Basically we are creating Custom Action at run time and injecting CSS using custom actions.

/ /Build a custom action

UserCustomAction cssAction = web.UserCustomActions.Add();

//Adding SCript Link location

cssAction.Location = “ScriptLink”;
cssAction.Sequence = 100;
cssAction.ScriptBlock = @”document.write(‘<link rel=””stylesheet”” href=””” + assetLibrary.RootFolder.ServerRelativeUrl + @”/custom.css”” />’);”;
cssAction.Name = actionName;

// Apply the CSS injection

cssAction.Update();
clientContext.ExecuteQuery();

Do visit my other blogs on office365 below.

Apply Site Logo In Office 365 using CSOM Code

Set Alternate CSS URL in Office 365 using CSOM

Set Theme to a site in SharePoint Online Or Office 365 programmatically using CSOM

Hope this piece of code helps. Happy SharePointing 🙂

Leave a Reply

Your email address will not be published.