Monday, November 16, 2015

SP2016 - SP2013 - O365 - SP.Publishing.SiteImageRenditions - Set ImageRenditions using Javascript

ImageRenditions allows the author to use the same image to display it in different sizes. More details about the ImageRenditions within the SharePoint can be found on the below articles.

In this post, we will see how to set the ImageRenditions in the SharePoint using Javascript.

Javascript API:

  • SP.Publishing.SiteImageRenditions.getRenditions - Gets the current list of ImageRenditions.
  • SP.Publishing.SiteImageRenditions.setRenditions - Sets the given list of ImageRenditions to the site.

Prerequisites of the site:

  • Publishing Site Collection
  • Blob cache enabled

Get the Current Site:

RegisterSod("sp.js", "/_layouts/15/sp.js");
SP.SOD.executeFunc("sp.js", "sp", function () {});
RegisterSod("sp.publishing.js", "/_layouts/15/sp.publishing.js");
SP.SOD.executeFunc("sp.publishing.js", "sp.publishing", function () {});
var context = SP.ClientContext.get_current();
var sourceWeb = context.get_web();
context.load(sourceWeb);
var targetSite = context.get_site();
context.load(targetSite);
context.executeQueryAsync(function(){},function(){});

Get the current list of ImageRenditions:

var imageRenditions = new SP.Publishing.SiteImageRenditions.getRenditions(context);
context.executeQueryAsync(function(){},function(){});
  • The above scripts get the full list of image renditions from the current site.

  • "imageRenditions" contains the List of SP.Publishing.ImageRendition object.
[{"SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.4316.1217","ErrorInfo":null,"TraceCorrelationId":"66cc419d-afa7-400e-443e-6f840d116d47"
},10,[
{
"_ObjectType_":"SP.Publishing.ImageRendition","Group":null,"Height":100,"Id":1,"IdCsom":1,"Name":"Display Template Picture 3 Lines","Version":1,"Width":100
},{
"_ObjectType_":"SP.Publishing.ImageRendition","Group":null,"Height":100,"Id":2,"IdCsom":2,"Name":"Display Template Picture On Top","Version":1,"Width":304
},{
"_ObjectType_":"SP.Publishing.ImageRendition","Group":null,"Height":220,"Id":3,"IdCsom":3,"Name":"Display Template Large Picture","Version":1,"Width":468
},{
"_ObjectType_":"SP.Publishing.ImageRendition","Group":null,"Height":68,"Id":4,"IdCsom":4,"Name":"Display Template Video","Version":1,"Width":120
}]
]

Create the new ImageRendition:

var customImageRendition = new SP.Publishing.ImageRendition();
customImageRendition.set_group("Intranet");
customImageRendition.set_height(100);
customImageRendition.set_width(100);
customImageRendition.set_name("thumbnail");
imageRenditions.push(customImageRendition);
  • The above script creates the new SP.Publishing.ImageRendition object.
  • Updates the "imageRenditions" list with the new ImageRendition.

Set the ImageRenditions:

var siteimageRenditions = new SP.Publishing.SiteImageRenditions.setRenditions(context,imageRenditions);context.executeQueryAsync(function(){},function(){});
  • Updates the Site ImageRenditions with the updated list.

The equivalent CSOM api is also available - Microsoft.SharePoint.Client.Publishing.SiteImageRenditions.


by Balamurugan Kailasam via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment