Dynamic Scrolling image creation using JavaScript

Step 1: Create a list named "Header-Scrollable-Images"
Step 2: Create a choice column with the value of "Yes / No"
Step 3: Create a column of hyperlink / Picture.

Site Actions --> Edit Page --> Add a Web part --> Select Content Editor Web part

Add the Script in a txt file and refer the file in the CEWP
or
Paste the below code directly in CEWP

<div id="sliderFrame">
</div>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(loadSharePointPictures, 'sp.js');
function loadSharePointPictures() {
    //fetch the list of items using the client object model
    var context = new SP.ClientContext.get_current();
    //get the current website
    var web = context.get_web();
    //get the pictures list
    var list = web.get_lists().getByTitle('Header-Scrollable-Images');
    //create the query to get all items
    //var query = SP.CamlQuery.createAllItemsQuery();
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Contains><FieldRef Name=\'ShowinScroll\'/>' +
        '<Value Type=\'Choice\'>Yes</Value></Contains></Where></Query></View>');
    //get all items from the query
    pictures = list.getItems(camlQuery);
    //load the context
    context.load(pictures, 'Include(FileLeafRef,FileDirRef)');
    //execute the query in async mode
    context.executeQueryAsync(
        Function.createDelegate(this, this.success),
        Function.createDelegate(this, this.failed));
}
function success(sender, args) {
    pictureArray = new Array();
    var pictureCount = 0;
    var enumerator = this.pictures.getEnumerator();
    while(enumerator.moveNext()) {
        var currentItem = enumerator.get_current();
        var filename = currentItem.get_item('FileLeafRef');
        /*filename = filename.replace('.', '_');
        filename += '.jpg';*/
        var dir = currentItem.get_item('FileDirRef');
        filename = dir + '/' + filename;
        pictureArray[pictureCount++] = filename;
    }
    var newHtml = '';
  newHtml += '<div id="slider">';
    for(i=0; i<this.pictureArray.length; i++) {
        newHtml += '<img  src="';
        newHtml += this.pictureArray[i];
        newHtml += '" />';
    }
 newHtml += '</div>';
    $('#sliderFrame').html(newHtml);
}
function failed(sender, args) {
    $('#sliderFrame').html(args.get_message());
}
</script>

Comments

Post a Comment

Popular posts from this blog

Activating a SharePoint Feature on Multiple Sites or Site Collections using PowerShell

Managed Path with WildCard and Explicit Inclusion

List All SharePoint 2010 PowerShell Commands