SharePoint CSS and JavaScript Examples

Here we will discuss few SharePoint CSS examples and a few SharePoint javascript examples.

We will see below SharePoint css and javascript examples:

  • SharePoint hide approve/reject button
  • Redirect to a different page after adding new list items in SharePoint
  • SharePoint hide edit item ribbon
  • Color code SharePoint list rows
  • SharePoint list open attachments in a new tab
  • Assign value to RichTextbox field using JavaScript in SharePoint 2013
  • CSS best practices for SharePoint
  • How to display current date in SharePoint Page using JavaScript
  • Display web part page in a modal dialog box using JavaScript in SharePoint

We can use the same SharePoint CSS example and SharePoint JavaScript examples in SharePoint 2013/2016/2019 also.

SharePoint CSS Examples

Below are a few SharePoint CSS examples:

SharePoint hide approve/reject button

In this section, we will discuss how to hide the approve/reject button in SharePoint from the ribbon in SharePoint Online/2013/2016.

Below is the CSS code which you can add using a script editor web part, in page where you have to hide the approve/reject button. Maybe in AllItems, EditForm page in SharePoint.

By default, in the SharePoint list you can see the Approve/Reject button like below:

SharePoint hide approve/reject button
SharePoint hide approve/reject button
<style type="text/css">    
a[id="Ribbon.ListItem.Workflow.Moderate-Large"]  
{  
display:none;  
}              
</style>

Once you insert the code, you can see the Approve/Reject button will disappear from the ribbon in the SharePoint list.

SharePoint 2013 hide approve/reject button
SharePoint 2013 hide approve/reject button

SharePoint hide edit item ribbon

Now, we will see how can we hide edit item from ribbon in the SharePoint list. The code will work in SharePoint Online/2013/2016.

Below is the SharePoint CSS code which you can add inside a script editor page in SharePoint list.

<style type="text/css">    
a[id="Ribbon.ListForm.Display.Manage.EditItem-Large"]  
{  
display:none;  
}              
</style> 

Color code SharePoint list rows

Now this SharePoint CSS example, we will see color code SharePoint list rows. We will see how to give alternate row color in the SharePoint list.

<style type="text/css">  
  
table.ms-formtable tr:nth-child(even) {background: #d3d3d3;}  
table.ms-formtable tr:nth-child(odd) {background: #e1eaf4;}  
table td.ms-formlabel,td.ms-formbody {font-weight: bold}  
table td .ms-standardheader {font-weight: bold}  
  
</style>

The above code, you can add inside a script editor web part. And you can see how the alternate row color will appear like below:

color code sharepoint list rows
color code sharepoint list rows

CSS best practices for SharePoint

Below are some CSS best practices to follow while using SharePoint.

Validation:
Validating your CSS is always important. This is to make sure that your CSS is error-free and is interpreted the right way in all browsers.
You can also validate through http://jigsaw.w3.org/css-validator/ URL.

Comments:
Commenting code is always a best practice while working with any language. So it is always advertisable to give a comment in CSS. You can comment like below:
/this is css style comment/

Naming:
Naming is very much important. Naming classes according to their function rather than what they will look like is a best practice and the name should be meaningful.

Compression:
It is a best practice to remove the white space from your .css fi les before you put the fi les in production. Smaller .css fi les will make your site load faster in the browser. You can use a CSS compressor like the one at the following URL to automatically remove the white space for you: http://cleancss.com.

SharePoint designer helps us to create custom CSS files.

SharePoint JavaScript Examples

Let us see a few SharePoint javascript examples.

Redirect to a different page after adding new list items in SharePoint

Now, we will see how to Redirect to a different page after adding new list items in SharePoint. The code will work in SharePoint Online/2013/2016, but the code will not work in Modern SharePoint Sites.

Below is the JavaScript code which you can add inside a script editor web part page.

<script  type="text/javascript" src="/sites/Test/Reports/Shared%20Documents/jquery-1.11.1.min.js"></script>  
  
<input type="button" value="Edit Item" id="editItem" onclick="MoveToViewPage()" />  
  
<script type="text/javascript">  
// get the item id parameter from the URL  
function urlParam (name){  
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);  
  
    return results[1] || 0;  
}  
  
function MoveToViewPage()  
{    
    window.location.href = "/sites/Test/Reports/Lists/Testing%20Environment/EditForm.aspx?ID=" + urlParam('ID') + "&Source=" + "/sites/Test/Reports/Lists/Testing%20Environment/DispForm.aspx?ID=" + urlParam('ID');  
}    
</script>

SharePoint list open attachments in a new tab

Now, in this section, we will decide how to open SharePoint list attachments in a new tab in SharePoint Online/2013/2016. We can Open list attachments in a new tab in the SharePoint list by using the below JavaScript code which you can add inside a script editor web part.

<script type="text/javascript" src="/sites/Test/Reports/Shared%20Documents/jquery-latest.js"></script>  
  
<script type="text/javascript">  
  
$(document).ready(function(){  
                $("table#idAttachmentsTable a").mousedown(function () {  
                var url=$(this).attr('href');  
                window.open(url);  
});  
});  
</script>  

Assign value to RichTextbox field using JavaScript in SharePoint 2013

Now we will see, how to assign a default value to a rich text box field using javascript in SharePoint 2013. And the form where we have the Rich text box is an InfoPath customized form.

Our InfoPath form is there inside a site page. And we have put one button ion the page and click on that button we wanted to put some default value to a rich textbox using javascript.

Once the form is customized the richtextbox becomes a <div> when you will check that in the view source. Here the id we got is from the view source of the page.

Add one script editor webpart and write the below code. Once you click on the submit then the text will appear in the Rich textbox.

<input type='button' id='123′ value='Click Me' onclick="updateListItem();"/>

<script language="javascript" type="text/javascript">
function updateListItem() {
document.getElementById(“ctl00_ctl30_g_412fea2a_5a21_4cf7_acbd_c9231e395d67_FormControl0_V1_I1_RTC8_RTI4_RT1_newRichText").innerHTML = ‘<b>Very bad day</b>';
}
</script>

This is not a great post but I just wanted to put since I was trying to set the value by using the .Value property but .innerHTML worked for me. Just note the letter case here also.

How to display current date in SharePoint Page using JavaScript

Here we will discuss how to display current date and time using javascript and jQuery in SharePoint page. We can use JavaScript/jQuery code inside a content editor web part. If you want show current date and time in SharePoint 2013/2016 page, then you can embed the code inside a script editor web part.

Here we will append today’s date in the SharePoint page using jQuery like “weekday, YYYY/MM/dd”.

For this follow below steps to display current date in SharePoint Page using JavaScript.

Edit the SharePoint page and then Add a Content Editor Web Part like below:

display current date in SharePoint 2010 Page

Now edit that Web Part and click on Click here to add new content.

After this On Ribbon under editing tools click on HTML after this On Ribbon under editing tools click on HTML Source under Format Text shown in fig.

How to display current date in SharePoint Page using JavaScript

After this one popup window is open, under this window add the following code.

<script src="/JS/jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function strpad00(s) {
s = s + ";
if (s.length === 1) s = '0' + s;
return s;
}

$(document).ready(function () {
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var now = new Date();
var n = weekday[now.getDay()];
var currentDate = now.getFullYear() + "/" + strpad00(now.getMonth() + 1) + "/" + strpad00(now.getDate());
$('.s4-pagedescription').append("<div style='float:right'>" + n + "," + currentDate + "</div>");
});

</script>

Now Save the page and the date will appear like below:

How to display current date in SharePoint 2010 Page using JavaScript

This is how we can display today’s date in SharePoint page using JavaScript.

Display web part page in a modal dialog box using JavaScript in SharePoint

Let us see how to display web part page inside a dialog box using JavaScript in SharePoint Online or SharePoint 2013/2016.

Here we will use the JavaScript and SP.UI.ModalDialog to display a web part page in the modal dialog box in SharePoint.

Remember the page should be a web part page, it will not work for wiki pages in SharePoint.

We can use the code inside a script editor web part or inside a content editor web part in a web part page in SharePoint.

<script type="text/javascript">
function OpenDialog(dialogUrl, dialogTitle) {
var options = {
url: dialogUrl,
title: dialogTitle
};
SP.UI.ModalDialog.showModalDialog(options);
}
</script>

<a href="#" onclick="OpenDialog('https://tsinfo.sharepoint.com/sites/TSInfoClassic/SitePages/Test.aspx','My Dialog Box');">Show Modal Dialog</a>

Now when you click on the above link then Test web part page will open as a dialog box. It will appear like below:

Display web part page in a modal dialog box using JavaScript in SharePoint

This is how to display web part page in a modal dialog box using JavaScript in SharePoint Online or SharePoint 2013/2016.

Retrieve workflow id by using jsom in SharePoint online

Let us see how to retrieve workflow id from a list using JavaScript object model code (jsom) in SharePoint online. Here we have a list in our SharePoint Online site and into that list, we have attached a few list workflows. By using, the below code we are going to retrieve the id of the workflow whose name is “Archiving”.

In this particular example, I have added the below code inside a script editor web part which I have added inside a web part page.

<input type="button" id="btnSubmit" value="Get Workflow ID" /><br/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
getWorkflowId();
});
}

function getWorkflowId() {
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
SP.SOD.registerSod(‘sp.workflowservices.js’, SP.Utilities.Utility.getLayoutsPageUrl(‘sp.workflowservices.js’));
SP.SOD.executeFunc(‘sp.workflowservices.js’, "SP.WorkflowServices.WorkflowServicesManager", function () {
context = SP.ClientContext.get_current();
web = context.get_web();
workflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager(context, web);

var subs = workflowServicesManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(‘4C384857-8AEF-484E-8163-FED3592E15A4’);
context.load(subs);
context.executeQueryAsync(function () {
var subEnumerator = subs.getEnumerator();
while (subEnumerator.moveNext()) {
var sub = subEnumerator.get_current();
if (sub.get_name() == ‘Archiving’) {
var templateId = ‘Workflow id: ‘+sub.get_id();
alert(templateId)
return;
}
}
},
function (sender, args) {
alert(args.get_message());
});
});
});
}
</script>

Once you Save the code and click on the button, it will display the workflow id inside an alert box like below:

Retrieve workflow id by using jsom in SharePoint online
Retrieve workflow id by using jsom in SharePoint online

This is how to retrieve workflow id by using jsom in SharePoint online Office 365.

You may like following SharePoint list view tutorials:

Here in this SharePoint CSS examples and SharePoint JavaScript Examples, we saw, how to hide approve/reject button from ribbon in SharePoint list. How to Redirect to a different page after adding new list items in SharePoint. How can we hide edit item ribbon in SharePoint list.

We also discussed Color code SharePoint list rows, how we can give alternative row colors in SharePoint list views. Finally, we discussed how we can open SharePoint list attachments in a new tab using JavaScript.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

  • >