Creating Buttons from any Link Library in SharePoint 2013
Here's a fun little trick to create buttons from the contents of a SharePoint 2013 Link List.
First here's the finished code.
<script type="text/javascript" src="/SiteAssets/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery.SPServices-0.5.4.min.js"></script>
<link rel="stylesheet" type="text/css" href="/SiteAssets/std_Buttons.css"/>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "Links",
CAMLViewFields: "<ViewFields><FieldRef Name='URL' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
var urlstr = $(this).attr("ows_URL").split(',');
var liHtml = "<a class='ms-ContentAccent1-bgColor btn_1line_xSma' href='" + urlstr[0] + "'>" + urlstr[1] + "</a><br/>" ;
$("#links").append(liHtml);
});
}
});
});
</script>
<div id="links" >
</div>
Note that you'll need to find, download, then upload to your SharePoint 2013 site those 2 jquery files.
the std_Buttons.css is my own. I provided it in another post, but I'm happy to share. The JQuery looks for a list named "Links" and then you see the CAML Query to pull the URL.
The link list is a little tricky to parse. The web address and the description are both stored in the URL field separated by a comma. In this line:
var urlstr = $(this).attr("ows_URL").split(',');
I split the display field into an array on the comma.
If there are commas in Link description, your button will only grab up to the first comma. To fix that would require looping up to the upper bound of the array and string build to get the description, but I prefer to let it just truncate there and say that it's functioning as designed.
A more likely scenario is a link that ends in a slash / which will provide an escape character to the href command and cause the description to appear beside the button. We can't have that so let's close that escape route.
var liHtml = "<a class='ms-ContentAccent1-bgColor btn_1line_xSma' href='" + urlstr[0] + "'''>" + urlstr[1] + "</a><br/>" ;
The list I wrote this against had no Title field, but I was able to redo it to use the Title as the button text, the link description to be the hover text, and the URL to provide the link.
First here's the finished code.
<script type="text/javascript" src="/SiteAssets/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery.SPServices-0.5.4.min.js"></script>
<link rel="stylesheet" type="text/css" href="/SiteAssets/std_Buttons.css"/>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "Links",
CAMLViewFields: "<ViewFields><FieldRef Name='URL' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
var urlstr = $(this).attr("ows_URL").split(',');
var liHtml = "<a class='ms-ContentAccent1-bgColor btn_1line_xSma' href='" + urlstr[0] + "'>" + urlstr[1] + "</a><br/>" ;
$("#links").append(liHtml);
});
}
});
});
</script>
<div id="links" >
</div>
Note that you'll need to find, download, then upload to your SharePoint 2013 site those 2 jquery files.
the std_Buttons.css is my own. I provided it in another post, but I'm happy to share. The JQuery looks for a list named "Links" and then you see the CAML Query to pull the URL.
The link list is a little tricky to parse. The web address and the description are both stored in the URL field separated by a comma. In this line:
var urlstr = $(this).attr("ows_URL").split(',');
I split the display field into an array on the comma.
If there are commas in Link description, your button will only grab up to the first comma. To fix that would require looping up to the upper bound of the array and string build to get the description, but I prefer to let it just truncate there and say that it's functioning as designed.
A more likely scenario is a link that ends in a slash / which will provide an escape character to the href command and cause the description to appear beside the button. We can't have that so let's close that escape route.
var liHtml = "<a class='ms-ContentAccent1-bgColor btn_1line_xSma' href='" + urlstr[0] + "'''>" + urlstr[1] + "</a><br/>" ;
The list I wrote this against had no Title field, but I was able to redo it to use the Title as the button text, the link description to be the hover text, and the URL to provide the link.
Comments
Post a Comment