Add Button to web part to Open any address in Dialog box

This is a script that you can use for SharePoint 2010 (untested in SharePoint 2013)

I currently use this script to open a list item’s NewForm.aspx page.

But you can use it to display and web address in a pop-up dialog.

And no JQuery is required!

Added notifications, if Ok or Cancel buttons clicked in the dialog.

Open Link button

dialog windows
Any URL can be displayed in Dialog

To test it out, just add this code to a txt or html file and link it to a Content Editor Web Part

<script type="text/javascript">

function portal_openModalDialog() {
var options = SP.UI.$create_DialogOptions();
options.url = "/_layouts/Upload.aspx?List={327D2CF7-F385-4969-8973-E6FD3908A996}&RootFolder="; //Change the URL to the page you require.
options.title = "Title of Dialog Window";
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}
function CloseCallback(result, value) {
if (result === SP.UI.DialogResult.OK) {
SP.UI.Notify.addNotification("Your request has been submitted");
}
if (result === SP.UI.DialogResult.cancel) {
SP.UI.Notify.addNotification("Request cancelled");
}
}
</script>

<button name="button" type="button" onclick="javascript:portal_openModalDialog();javascript:return false;" href="#" target="_self">Open Link</button>

Pastebin Version: http://pastebin.com/mfmQLUCX (opens in new tab)
Advertisements

Save list item as PDF file from Display Form using SWF buttons

This article explains how to add swf Flash buttons using TableTools to your List View and Default Display Form.

I’ve tested this in SharePoint 2010, using IE8. I have not tested this in SharePoint 2013 but it would work there too.

This is a pure front-end solution, using only SP Designer or notepad for those on a locked down intranet.

The only requirement is that your browser needs to be able to display Flash and SWF files.

The original post and code came from Danish Islam, which explains the how the code and buttons are linked to a List View.

Use jQuery Datatables for SharePoint List Webpart with export to CSV, EXCEL, PDF and Print features

The buttons contained in jQuery Datatable TableTools are:

Copy, CSV, Excel, PDF, Print.

buttons

This is the result of adding the script to the List View:

listTableTools

Note: The search box is live “Search as you type” filtering the list. The paging is Ajax and loads instantly.

All that you require are:

  • The files to be uploaded
  • Change the file paths in the the code
  • Change the file paths of the images in the CSS files.
  • Add a Content Editor Web Part to your List View or Display Form view.

If you place this script in a content editor web part above a list view, the full affect of Datatable Tabletools will be displayed.

Also, important note: Use the jQuery.js file that comes with the Datatables Zip Package.  Using jQuery v1.12 the buttons did NOT display and it took me a couple hours to determine it was the jQuery.js file.

<style type="text/css">
.even, .odd, .sorting_1 {
 background-color: transparent !important;
}
.dataTable THEAD TH {
 border: 0 !important;
}
</style>
<script type="text/javascript" src="/scripts/datatables/jquery.js"></script>
<script type="text/javascript" src="/scripts/datatables/jquery.dataTables.min.js"></script>
<link type="text/css" rel="stylesheet" href="/scripts/datatables/css/jquery.dataTables.css">
<script type="text/javascript" src="/scripts/datatables/dataTables.tableTools.js"></script>
<link type="text/css" rel="stylesheet" href="/scripts/datatables/css/dataTables.tableTools.css">

<script type="text/javascript" >
 $(document).ready(function(){
 $(".ms-listviewtable").prepend($("<thead></thead>").append($(".ms-listviewtable").find("tr:first"))).DataTable({"sDom": 'T<"clear">lfrtip', 

 "tableTools": {
 "sSwfPath": "/scripts/datatables/copy_csv_xls_pdf.swf"
 // Change this path according to file location 

 } 

 });
 });
</script>

For a List Display Item, I removed the CSV and Excel buttons and also added an “Email Link” button.

displayform


<style type="text/css">
.even, .odd, .sorting_1 {
background-color: transparent !important;
}
.dataTable THEAD TH {
border: 0 !important;
}
</style>
<link type="text/css" rel="stylesheet" href="/scripts/datatables/css/jquery.dataTables.css">
<link type="text/css" rel="stylesheet" href="/scripts/datatables/css/dataTables.tableTools.css">
<script type="text/javascript" src="/scripts/datatables/jquery.js"></script>
<script type="text/javascript" src="/scripts/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/scripts/datatables/dataTables.tableTools.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".ms-formtable").prepend($("<thead></thead>").append($(".ms-formtable").find("tr:first"))).DataTable({"sDom": 'T<"clear">rt',
"tableTools": {
"sSwfPath": "/scripts/datatables/copy_csv_xls_pdf.swf",

"aButtons": [

{

"sExtends": "copy",
"sButtonText": "Copy to clipboard"
},
{
"sExtends": "pdf",
"sButtonText": "Save as PDF",

"fnComplete": function ( nButton, oConfig, oFlash, sFlash ) {
SP.UI.Notify.addNotification('<img src="/_layouts/images/loadingcirclests16.gif" style="vertical-align: top;"/> PDF Exported to file ...',false);
}
},
{

"sExtends": "print",
"sButtonText": "Print this item"
},

{

"sExtends":    "text",
"sButtonText": "Email link",
"sButtonClass":  "DTTT_button_csv",
"fnClick": function ( nButton, oConfig, oFlash ) {
window.location='mailto:?subject=' + document.title + '&body=' + window.location;
}
}
]
} // end of tabletools

});

});

</script>

The Saving to PDF button is the only front-end solution I have found that works without for IE8 and SharePoint 2010.

Any comments, questions, hit me up. Enjoy.

 

Display Todays Date in a Calculated Column with Javascript

Here’s what I used to display Today date in a Calculated Column with nothing but JavaScript in SharePoint 2010.

After a lot of research and trial and error, I finally have it displaying:

To display a date like this: 17/5/2015

Use this code:

=CONCATENATE("<p id='mydate'><script>var currentDate = new Date();var day = currentDate.getDate();var month = currentDate.getMonth()+1;var year = currentDate.getFullYear();document.getElementById('mydate').innerHTML = ( day + '/' + month + '/' + year );</script></p>")

 

calcColumnTodaysDate

 

Don’t forget to use data type returned as Number(1, 1.0, 100) This will convert the code to HTML

For U.S. Date, just swap the day and month over like this:

( day + ‘/’ + month + ‘/’ + year )

( month + ‘/’ + day + ‘/’ + year )

 

Update: The above code will only work for the first entry as the id name, i.e. ‘mydate’, needs to be unique.

So all you need to do is use another column that has Unique values, like a counter, or you could even combine two columns as thus:

 =CONCATENATE("<p id='"&PosNum&""&[First Name]&"'><script>var currentDate = new Date();var day = currentDate.getDate();var month = ('0' + (currentDate.getMonth()+1)).slice(-2);var year = currentDate.getFullYear();document.getElementById('"&PosNum&""&[First Name]&"').innerHTML = ( day + '/' + month + '/' + year );</script></p>"

The above code works and displays todays date for every Item.

 Date will now show as 20/05/2015

Hope this saves you some time 😉

Record Search Results Queries to a List using a Hit Counter

This is how I am using a Search Results page to record individual Users Search Queries in a list.  Each Search Query is displayed as how the user typed them in.

What I’ll explain is how we can grab the URL from the Search Results and strip out the unnecessary code to display only the User’s Search Query.

For Example

This Search Result URL:

?k=pdf&cs=This%20Site&u=https%3A%2F%2Fwww%2Enothingbutsharepoint%2Ecom&r=fileextension%3D"ascx"%20fileextension%3D"asp"%20fileextension%3D"aspx"%20fileextension%3D"htm"%20fileextension%3D"html"%20fileextension%3D"jhtml"%20fileextension%3D"js"%20fileextension%3D"mht"%20fileextension%3D"mhtml"%20fileextension%3D"mspx"%20fileextension%3D"php"

Will be converted to display only what the user has typed in the Search Box, which in this example, is: “pdf“.

Each time a user types in a Search, their query is recorded with their name, date and keywords typed.

Firstly, you need to install and configure the “Hit-Counter and Star-rating for SharePoint” from Alexander’s SharePoint JavaScripts site.

Once you have this configured it and you can see the users data added to the  HitCounter List , we can now modify the HitCounterAndStarRatingForSharePoint.js file to include the Users keywords entered.

1: First create a new column within the HitCounter list call it UserKeywords.  Add this column to your default view.

2: Edit the HitCounterAndStarRatingForSharePoint.js  and scroll down to about line 218:  //Add item.  Within that block of code you will see the column variables, make a new line under ReferringURL:refURL, and type in the new column name which is attached to the variable as such:

UserKeywords:searchQuery,

3:  Then under the following code at about Line: 152

// Get url and referrer
refURL = document.referrer;

Add the following lines of code:

searchURL = window.location.search;                               //returns string ?k=Users%20Search%20String
  modURL = searchURL.substring(3);                                   //removes characters ?k= from string
  decodedURL = decodeURIComponent(modURL);       //decode URL to translate encoded characters from string
  searchQuery = decodedURL.split('&')[0];                         //removes characters all after &

Each line I’ve added a comment that describes what each line does.  I don’t know how to write code in Javascript, so I had to Google Search each line to work out how to accomplish each Javascript function.

4:  Now you can test the Search Results page by entering in a Search Query and then checking the HitCounter List.

The UserKeywords column will now display the Search Keywords exactly how they are typed by the user.

Any questions, please comment below, thanks for reading.