1

I have a custom standard view for a document library which contains thumbnail images of each item uploaded.

For ease of use, I'd like to be able to add 'live update' checkboxes to my standard view so users can toggle two options on each picture and have the properties immediately updated in the database.

The datasheet view does everything I want except display the thumbnail of each image, so in a library containing thousands of images with arbitrary and random names that's no good for my purpose.

Is there a way to add checkboxes to a standard view that have the same 'live update' function of the datasheet view checkboxes?

  • Can you add here the relevant part of your xslt code? – MdMazzotti Feb 3 '14 at 8:55
3
+50

Here's a solution. It involves using jQuery and the SharePoint JSOM library.

Open your view in SharePoint Designer advanced edit mode. I think you already customized the XSLT. Add this code where you want the checkboxes to appear:

<input type="checkbox" id="{generate-id()}{$ID}cbxProp1">
    <xsl:if test="$thisNode/@Prop1.value = 1">
        <xsl:attribute name="checked">true</xsl:attribute>    
    </xsl:if>
    <xsl:attribute name="data-id">
        <xsl:value-of select="$thisNode/@ID"></xsl:value-of>
    </xsl:attribute>
</input>  
<input type="checkbox" id="{generate-id()}{$ID}cbxProp2">
    <xsl:if test="$thisNode/@Prop2.value = 1">
        <xsl:attribute name="checked">true</xsl:attribute>    
    </xsl:if>
    <xsl:attribute name="data-id">
        <xsl:value-of select="$thisNode/@ID"></xsl:value-of>
    </xsl:attribute>
</input>  

I assumed that the checkboxes will be bound to Yes/No properties in your library. (Replace the nodes name (@Prop1 / @Prop2) with yours).

Add the following javascript code to the content placeholder with ID PlaceHolderBodyAreaClass:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(docReady, "sp.js");

function docReady(){
    $('input[id$="cbxProp1"]').click(function(e){ 
        e.stopImmediatePropagation();  
        $this = $(this);    
        var value;
        $this.attr('checked', $this.attr('checked') == 'checked' ? true : false);       
        var itemId = $this.data('id');
        var value = $this.attr('checked') == 'checked';
        updateItem(itemId, 'Prop1', value); //replace Prop1 with the internal name of your first yes/no field
    });

    $('input[id$="cbxProp2"]').click(function(e){ 
        e.stopImmediatePropagation();  
        $this = $(this);    
        var value;
        $this.attr('checked', $this.attr('checked') == 'checked' ? true : false);       
        var itemId = $this.data('id');
        var value = $this.attr('checked') == 'checked';
        updateItem(itemId, 'Prop2', value); //replace Prop2 with the internal name of your second yes/no field
    });
}

function updateItem(id, property, value){
    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle('Images'); //REPLACE WITH YOUR LIBRARY TITLE

    this.oListItem = oList.getItemById(id);
    this.id = id;

    oListItem.set_item(property, value);

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    // log or whatever
    console.log('Item id ' +  this.id +' updated!');
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

The previous javascript code has a dependency on jQuery, so if you don't have it, add it to either your masterpage or the current page.

  • This is an excellent and complete answer. I'll just add that if you were willing to also include the SPServices javascript library (spservices.codeplex.com), you could further simplify the code. – tyshock Feb 3 '14 at 21:09
  • Yeah, I know this excellent library, however I built my own library which is a wrapper around the native JSOM library, with jQuery promises. It makes working with libraries and items a breeze, reducing the boilerplate code and the messiness of the async callbacks nesting. The update on the code in my answer would have been 2 lines long. Sooner or later I will put it on codeplex/github – MdMazzotti Feb 3 '14 at 22:09
  • Yes, get that on github. It sounds great! – tyshock Feb 3 '14 at 22:52
  • Looks good, I'll implement and test tomorrow. One thing I've spotted in that jQuery that might cause an issue though is using id's as the selectors for the click function, as I'll have an unknown number of checkboxes on any page; I'll bind that to a class that I'll add on and get the id from the data-id field in the inner function. – bdx Feb 4 '14 at 3:01
0

While MDMazzotti's answer seems perfect, it uses JQuery and JSOM.. If you don't want to rely on that, you can create a Visual Web Part to work with your selected Items.. How will you create a Checkbox in a Standard List View is covered here:

SharePoint 2010: Get Selected Items from List View in Visual Web Part

This will allow you to add the checkbox column in any page without actually editing the XSLT for the List View.. You just need to reference the Christophes library to render the Checkbox properly (add the script in a Content Editor Web Part on the same page).. You can also access the values of Checkbox in JQuery to perform bulk action..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.