문제

작업

짧은 - 그림 라이브러리에서 그림 라이브러리에서 Item 속성에 액세스합니다.

long - PageLayout을 편집 할 때 이미지가 페이지에 추가되면 이미지의 설명을로드해야합니다.이것은 사용자가 여러 번 작성하지 못하도록 버튼 "설명"버튼으로 트리거됩니다.

접근

function GetImageDescription (imageUrl) {
//possible image URLs so far -- additions are welcome !
imageUrl = "http://www.exampleURL/lorem-ipsum.jpg" //does not need to work
imageUrl = "http://root/Sub/PublishingImages/MyFolder/MyFolderN/lorem-ipsum.jpg" //does not work
imageUrl = "http://root/OtherSub/PublishingImages/lorem-ipsum.jpg"; // would work when using correct context.
imageUrl = "http://root/Sub/PublishingImages/lorem-ipsum.jpg"; // works

var libraryUrl = imageUrl.substr(0, imageUrl.lastIndexOf("/")); //trim the picture URL (what about folders ?)
var clientContext = SP.ClientContext.get_current(); 
var lists = clientContext.get_web().get_lists(); //get all lists
clientContext.load(lists, 'Include(DefaultViewUrl, Title)'); //load lists with needed attributes

clientContext.executeQueryAsync(function (sender, args) {
    var enumerator = lists.getEnumerator();
    while (enumerator.moveNext()) { //walk over all lists
        var list = enumerator.get_current(); //get current list
        var listUrl = list.get_defaultViewUrl(); //get somewhat containing the url - View for example
        listUrl = listUrl.substr(0, listUrl.lastIndexOf("/Forms")) // trim to get pure list URL
        if (endsWith(listUrl, libraryUrl)) { // check if its a match
            //list found ! 
            //do awesome stuff  
        }
    }
}, function (sender, args) {
    throw "Sadly there occured an error - " + args.get_message() + ' ' + args.get_stackTrace();
});

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
.

폴더가 생성되지 않은 한이 작업을 수행합니다.

update

Vadim Gremyachev가 전달되어 교차 사이트 호환성을 추가했습니다.

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

도움이 되었습니까?

해결책

절대 URL을 서버 상대 URL로 변환하고 Sp.web.getFileByServerReleativeURL 메소드 아래에서 설명 된 지정된 서버 상대 URL에있는 파일 객체를 반환하려면 다음과 같이 해당합니다.

function getFileProperties(url,success,error) {

   var ctx = SP.ClientContext.get_current(); 
   var relUrl = url.replace(_spPageContextInfo.siteAbsoluteUrl,'');  //convert to relative url
   var file = ctx.get_web().getFileByServerRelativeUrl(relUrl);   //get file
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         success(file);
      }, 
      error
   );
}
.

사용량

//get image file properties, e.g. Comments  
getFileProperties('https://contoso.sharepoint.com/media/PublishingImages/Lake-Baikal.jpg',
  function(file){
     var listItem = file.get_listItemAllFields();
     var comments = listItem.get_fieldValues()._Comments;
     console.log(comments);
  },
  function(sender,args){
      console.log(args.get_message());
  });
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top