//get absoulute image url
var imageUrl = $("#Image img")[0].src;src.split("?")[0]; //remove querystring
//get image file properties
getFileProperties(imageUrl, imageUrl);
function getFileProperties(url, fileUrl) {
//to get the file specific client context
//you can create the context based on a url
//the url sadly must match the pure site's url
//exmpl.
//http://contoso/library/image.jpg does not work
//http://contoso/library does not work
//http://contoso does work
//to get the pure site's url I simply add a try
//error query and repeat recursively until no error
//is thrown or the url does not contain "/"
var ctx = new SP.ClientContext(url); //get context
var siteCollection = ctx.get_site(); //try get site to check if context is valid
ctx.load(siteCollection);
ctx.executeQueryAsync(
function () {
// context is valid - proceed
var relUrl = fileUrl.replace(_spPageContextInfo.siteAbsoluteUrl, ''); //convert to relative url
var file = ctx.get_web().getFileByServerRelativeUrl(relUrl); //get file
ctx.load(file, 'ListItemAllFields');
ctx.executeQueryAsync(
function () {
var listItem = file.get_listItemAllFields();
var comment = listItem.get_fieldValues()._Comments;
var copyright = listItem.get_fieldValues().wic_System_Copyright;
$("#Description input").val(comment); //apply
$("#Copyright input").val(copyright);
},
function (sender, args) {
console.log(args.get_message()); //errorhandling
}
);
},
function (sender, args) {
//context is invalid - shrink url and try again
if (url.indexOf("/") !== -1) {
url = url.substr(0, url.lastIndexOf("/"));
getFileProperties(url, fileUrl); //recursive call
}
//else - the url is not compatible
}
);
}