Javascript to open SharePoint links list items in a new window

This is harder than it sounds, mostly because of the way SharePoint renders it pages and links within. If you are displaying a links list on a page and it is GROUPED using metadata, then you are going to have a bit of a tough time accessing those links using Javascript. The key is to use SharePoint’s version of the

$( document ).ready “trigger” called¬†_spBodyOnLoadFunctionNames.push

Below is some code I used which worked nicely. There is a bit of output you can remove once you see you are getting the results you want. ūüôā Just add into a CEWP on the same page (or MP ref).Add you script tags to ref jQuery lib and surround JS below:

function sharePointReady()
{
updateLinks();

//Hook into SharePoint event to fix all links in group-by tables.
var oldExpand = ProcessImn;

ProcessImn = function(){
var results = oldExpand.apply(this, arguments);

updateLinks();

return results;
}
}

function updateLinks()
{
var url = ‘://’ + window.location.hostname;
$(“a”).each(function() {
var link = this; // assign the link object to another variable for easier managability
var linkHref = link.href.toLowerCase(); // lower case it
console.log(url);
console.log(linkHref);
if(linkHref.indexOf(url) < 0 && linkHref.indexOf(‘javascript:’) 0){ // check to see if this is a PDF
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}

if(linkHref.indexOf(‘/forms/’) > 0 && linkHref.indexOf(‘).aspx’) > 0){
//check for links in the forms library
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}
});

}

//this is the SharePoint preferred alternative of $(document).ready
_spBodyOnLoadFunctionNames.push(“sharePointReady”);

 

Advertisements

Display List in Sub Site

So, I’ve been doing a bit of work on a 2007 environment lately so this solution is specifically for 2007 BUT I will provide a link to the 2010 script as well, so don’t worry.

Here is a bit of script that will let you display a list view on a sub site page. Paste it into a Content Editor web part but be sure to update the URL of the list view you want to display. Also, if you find you get an error, you can add ?contents=1 to your page URL and delete the CE web part if you need to.

The script comes from the following post: http://blog.pathtosharepoint.com/2009/01/22/a-simple-method-to-display-a-list-in-another-site/

and here is a jQuery adaptation that works well too: http://www.balestra.be/2012/04/want-to-display-a-sharepoint-list-from-another-site-collection-use-these-2-lines-of-jquery-script.html

<DIV id="ListPlaceholder"><IMG src="/_layouts/images/GEARS_AN.GIF"></DIV>
<!-- Paste the URL of the source list below: -->
<script type="text/javascript">
function DisplayThisList()
{
var placeholder = document.getElementById("ListPlaceholder");
var displaylist = null;
var sourcelist = document.getElementById("SourceList");
try {
   if(sourcelist.contentDocument)
      // Firefox, Opera
      {displaylist = sourcelist.contentDocument.getElementById("WebPartWPQ2") ;}
   else if(sourcelist.contentWindow)
      // Internet Explorer
      {displaylist = sourcelist.contentWindow.document.getElementById("WebPartWPQ2") ;}
   else if(sourcelist.document)
      // Others?
      {displaylist = sourcelist.document.getElementById("WebPartWPQ2") ;}
}
catch(err) { alert ("Loading failed");}
displaylist.removeChild(displaylist.getElementsByTagName("table")[0]);
var allDescendants = displaylist.getElementsByTagName("*");
for (i=0;i<allDescendants.length;i++) {
allDescendants[i].removeAttribute("id");
allDescendants[i].removeAttribute("onclick");
allDescendants[i].removeAttribute("onfocus");
allDescendants[i].removeAttribute("onmouseover");
}
placeholder.innerHTML = displaylist.innerHTML;
}
</script>

 

String Functions in SPD 2010… NOT!

So I had a few frustrations with the lack of string manipulation functions/activities in SharePoint Designer 2010. To put it bluntly, trying simply, to get the workflow item (document) library URL was like going to Ikea for for a loaf of bread. It was time consuming, confusing and most importantly fuitless. You can not do it! Well, you can’t do it using the workflow activities. The available string functions are laughable and so far as I can see, pretty useless. Anyway, enough digging at SPD and more soltuions please-

I ended up creating a custom HTML and JS page that I stored in the Site Assets library of my site collection. In my task email I added a link to point to this page and also passed it some workflow variables in the form of  querystring parameters. Ultimately I passed in the item ID and the URL of the item/document. My custom page did the string manipulation and redirction to the resulting URL- which in fact was just a view on the document library. The view just displayed all documents under this workflow. After I got this working nicely I ended up adding another link in the email to actually open the document properties page. This was useful as actioning the task meant downloading and then deleting the document. The properties page is the best place for doing this really. If anyone is interested I can post my custom page source and the link setup for the worklfow email. Works well

JQuery Dialog People Picker Validation

So this is a bit of a rugged solution but its just nice to have¬†SOME validation on a people picker within a JQuery dialog before that sucker disappears (when a user clicks OK). Below is an example dialog declaration and the click event over-ride function to validate the people picker (Check Names link). Unfortunately it does require the ‘Are you sure’ pop up, just so we¬†can give the people picker elements enough time to change based on the¬†built in ‘check names validation’. If we don’t have this pause then the validation is done before the Click event has completed and updated the relevant elements.¬†I’m sure you could use some wait function instead if you like. As you can see, the people picker control gets broken down in to quite the html nightmare- you should view the source of a page with one just for kicks.

$(‘a[id*=fromUser_checkNames]’).click(
    function() {
¬†¬†¬† alert(“Are you sure you want to delegate this responsibility?”);
¬†¬†¬† if($(‘span[id*=toUser_errorLabel]’) != null){
¬†¬†¬†¬†¬†¬†¬† if($(‘span[id*=toUser_errorLabel]’)[0].innerHTML != “”){
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬† alert(“Invalid User”);}
                }
    }
 
    );
 
$(‘#delegation’).dialog({
            bgiframe: true,
                autoOpen: false,
                width: 600,
                modal: true,
                buttons: {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† “Cancel”: function() {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† $(this).dialog(“close”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† $(‘input[id*=enddate]’).val(“”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† $(‘input[id*=startdate]’).val(“”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† $(‘div[id*=toUser_upLevelDiv]’).text(“”);
                showDelegation = false;
 
                        } ,
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† “Ok”: function() {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬† $(‘a[id*=toUser_checkNames]’).click();
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬† $(‘a[id*=fromUser_checkNames]’).click();
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬† if($(‘span[id*=toUser_errorLabel]’) != null){
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬†¬†¬†¬† if($(‘span[id*=toUser_errorLabel]’)[0].innerHTML != “”){
                                    return false;
                                }
                              }
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† $(this).dialog(“destroy”).appendTo(“#aspnetForm”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† eval($(“#<%= hdnDelegatePostback.ClientID %>”).val());
 
                        }
            }
        });