Category Archives: jQuery

ListAttachments for SharePoint updated: v2.5.0

It has been a while since I last worked on this script but now I could finally fix the bug that caused the bubble to disappear right away in Internet Explorer.

Go grab the latest version from the Product Page. and please let me now if it works for you.

Changelog v2.5.0

  1. Fix: The bubble will no longer disappear right away in Internet Explorer

ListAttachments for SharePoint BETA release: v2.5.0BETA2

I released a new BETA version of ListAttachments for SharePoint 2013 and SharePoint Online.

Changelog BETA1:

  • Almost completely rewritten to make it more robust and efficient
  • Fixed the issue in IE where the bubble pops up and disappears immediately
  • Changed from SOAP to REST to minimize the payload and speed up the whole process of retrieving the required data from the server
  • Lots of other little fixes and improvements

Changelog BETA2:

  • Fixes a bug where the script fails for Read-Only users because SP.Ribbon.js is initialized too late

You can get the latest version on the Product Page. If you find any bugs, I would appreciate if you’d report them in either the comment section or the forum. Thanks!

Get all attachments of a particular list item via REST and with minimal overhead

In this post I want to show you how I retrieve the attachments of a particular list item in my ListAttachments-script.

In order for this code to work you will need jQuery since we are are using jQuery’s Ajax-function.

Check out the code below:

<script>
function getAttachments(listName, itemId){
  var attachments = {
    count: 0,
    items: []
  };
  jQuery.ajax({
    url: L_Menu_BaseUrl + "/_api/web/lists/getbytitle('" + listName + "')/items('" + itemId + "')/AttachmentFiles",
    method: "GET",
    async: false,
    headers: { "Accept": "application/json; odata=nometadata" },
    success: function (data) {
      jQuery(data.value).each(function (i) {
        attachments.items.push({
          extension: this.FileName.substring(this.FileName.lastIndexOf('.') + 1),
          name: this.FileName,
          path: this.ServerRelativeUrl
        });
        attachments.count++;
      });
    }
  });
  return allAttachments;
}

console.log(getAttachments("Custom List", 1));
</script>

What’s happening here is that we have a function called getAttachments which takes two arguments, listName and itemId.

This function will simply retrieve a JSON object by sending a GET request to
L_Menu_BaseUrl + “/_api/web/lists/getbytitle(‘” + listName + “‘)/items(‘” + itemId + “‘)/AttachmentFiles”.
L_Menu_BaseUrl is a global variable on your SharePoint-site that contains the relative path of the current site.

This script also sends odata=nometadata in the header which will reduce the amount of data that is sent back by roughly 50% by omitting a lot of meta data (which isn’t required in my case anyway).

I’m also using a synchronous API call (“async: false”) since the rest of my script depends on the returned value of that function.

Here is what is returned by the server when this code is executed on an item with 3 attachments:

{
    "value": [
        {
            "FileName": "attachment1.doc",
            "ServerRelativeUrl": "/Lists/Custom List/Attachments/1/attachment1.doc"
        },
        {
            "FileName": "attachment2.pdf",
            "ServerRelativeUrl": "/Lists/Custom List/Attachments/1/attachment2.pdf"
        },
        {
            "FileName": "attachment3.jpg",
            "ServerRelativeUrl": "/Lists/Custom List/Attachments/1/attachment1.jpg"
        }
    ]
}

In order to make the returned object a bit easier to work with, I then put all the information into my own object that has an attachment-counter and an array of objects that contain the file-extension, name and path of each attachment.

And that’s the content of the final object which is returned by the getAttachments-function:

Object {
	count: 3,
	items:[	Object { extension="doc",  name="attachment1.doc",  fullPath="/Lists/Custom List/Attachments/1/attachment1.doc"}, 
		    Object { extension="pdf",  name="attachment2.pdf",  fullPath="/Lists/Custom List/Attachments/1/attachment2.pdf"}, 
		    Object { extension="jpg",  name="attachment3.jpg",  fullPath="/Lists/Custom List/Attachments/1/attachment3.jpg"}]
}

StickyHeaders 3.0 Released

StickyHeaders 3.0 for SharePoint 2013 on-premises and SharePoint Online has been released.

I spent a lot of time to get rid of the bugs in previous versions and I came up with greatly simplified code that makes a lot of things better than before. One of the biggest changes is that the sticky headers now keep their full functionality in quick edit mode.

Furthermore, the performance has been dramatically increased and all calculations which are necessary to figure out whether the header shall be sticky or not take now less than 1ms for each scroll step on a normally powered computer (especially important when smooth scrolling is activated). You can grab the script directly from the related Product Page

Should you have any questions, problems or bug reports please use the comment section on the Product Page or the Forum.

QuickTip: Find out whether the page is in edit mode or not

You just want to run a script if the page is in edit mode or just execute if it is not?
Use this little function (requires jQuery) to check whether the page is in edit mode or not:

function checkIfEditMode(){
	return ($("#MSOLayout_InDesignMode").val() || $("#_wikiPageMode").val()) ? true : false
}

This script will work Web Part pages, SharePoint Publishing pages and Wiki pages.

QuickTip: SharePoint Attachment Field in List Forms


To show the attachment field in the form as in the above screenshot without the need to click the ‘Add attachment’-button in the ribbon, simply edit the form and put the script below into a Script Editor Webpart. This Script requires jQuery.

jQuery(document).ready(function(){
	//Get all the ribbon buttons from the action-group
	var $ribbonAttachButton = $("#Ribbon\\.ListForm\\.Edit\\.Actions a")
	//If the 'Add Attachment'-button is the only button in the group then hide the group, otherwise just hide the button 
	$ribbonAttachButton.length == 1 && $ribbonAttachButton.closest("li").hide() || $ribbonAttachButton.hide()
	//Get the iportant elements of the hidden attachment field
	var $attachElements = $("#csrAttachmentUploadDiv > #partAttachment > table > tbody > tr:eq(1),#csrAttachmentUploadDiv > #partAttachment > table > tbody > tr:eq(4)")
	//Get the 'OK'- & 'Cancel'-button
	var $attachButtons  = $attachElements.find(".ms-attachUploadButtons")
	//Insert an empty cell in order to adjust to the form's layout
	$("<td></td>").insertBefore($attachButtons)
	//Align buttons to the left
	$attachButtons.css("text-align","left")
	//Remove some margin and rename the 'OK'-button
	$attachButtons.find("#attachOKbutton").css("margin-left",0).val("Attach")
	//Adjust the fields label to match the rest of the form, adjust the height
	$attachElements.find(".ms-formlabel").html("<h3 class='ms-standardheader'>Add Attachments</h3>").css({"height":"auto","width":"auto"})
	//Make the file-selector bigger
	$attachElements.find("#onetidIOFile").addClass("ms-longfileinput")
	//Move elements to the end of the form table
	$attachElements.appendTo($("#part1 > .ms-formtable"))
})

Starting Workflows (SP 2010 WF Engine) With 1 Click

It always annoyed me that if you want to start a SP2010 workflow manually from the items context menu or the ribbon you are redirected to the page where you have to click yet another button to finally start a workflow.

But there is an better way. With a small script and with the help of the SPServices JavaScript library you can start workflows with just one click.

Continue reading

(Updated) Sticky Headers for SharePoint 2013 – v2.0

Update 4th of March, 2016: This script has its own Product Page now and all future updates will be posted there. Should you have any questions, problems or bug reports please use the comment section on the Product Page or the Forum.

A limitation of the StickyHeaders script in version 1.0 is that it is not working in Quick Edit mode. Therefore I updated the script and released version 2.0 which is now also working in Quick Edit mode. You can download the script at the end of this post.

Version 2.0 also eliminates a few bugs that showed up from time to time. While the sticky headers are fully functional in list views, the sticky headers lose their functionalities in Quick Edit mode, but I hope that I can address this issue later (I already got an idea how to do it, so check back a bit later if you need these functions).

Continue reading

(Updated) Open list item attachments by clicking the paperclip – Version 2

Update 28th of July, 2014: Script updated to version 2.3 (see the changelog)

End of January I adapted the script to open list item attachments with a simple click on the paperclip symbol, which Alexander Bautz from spjsblog.com wrote, to SharePoint 2013.

In the last couple of days I have made some further improvements to this script which I want to share with you.

Continue reading

Open list item attachments with a click on the paperclip

UPDATE February 13th, 2014: An even newer version is now available which you can check out here.

Another requirement of one of my users is to open file attachments directly by clicking on the paperclip icon without the detour of opening the item first. Alexander Bautz from spjsblog.com wrote the perfect script for this purpose which you can find here on his blog post. Thanks for that!

However, the last update was from mid 2012 and therefore the latest version is not compatible with SharePoint 2013. I made some minor adjustments to the code to restore the compatibility and I added a fade-in and fade-out animation, as well.

Continue reading