Skip to main content
deleted 5 characters in body
Source Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41

AbridgmentShort - Access item attribute from a picture library by picture url.

Abridgment - Access item attribute from a picture library by picture url.

Short - Access item attribute from a picture library by picture url.

edited title
Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41

CSOM JSOM - get listitem by file URL

added 34 characters in body
Source Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41

Get corresponding ListItem CSOM - get listitem by imagefile URL

 //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
        }
    );
}

Get corresponding ListItem by image URL

 //get absoulute image url
var imageUrl = $("#Image img")[0].src;

//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
        }
    );
}

CSOM - get listitem by file URL

//get absoulute image url
var imageUrl = $("#Image img")[0].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
        }
    );
}
added 2393 characters in body
Source Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41
Loading
added 336 characters in body
Source Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41
Loading
Source Link
Mx.
  • 3.3k
  • 1
  • 21
  • 41
Loading