3

Regarding my question, at this moment i'm writing some client object model code and I can't find a way to retrieve create make an url ...

My question now is: How can I create an url to an item? I get an ListItem object with the client object code yes but how to retrieve now an url from it?

If the full url is not possible how can i create a full url to this item?

Thanks for a help.

Update, i have the following code but it is to get the link to document:

var objectType = this.listItem.get_fileSystemObjectType();
if(objectType!='invalid'){
var url = this.listItem.get_item('_dlc_DocIdUrl').get_url();
var title = this.listItem.get_item('FileLeafRef');
window.open('mailto:?subject=Emailing%3A%20'+ title + '&body=' + url);
}
// here should come the creation of URL to a list item
else{
....
}

Now I have to have link to an item .. and not document, if outside of the library

1 Answer 1

1

It depends whether the specified list item belongs to List or Documents library:

  • for Documents library: var absItemUrl = item.get_item('EncodedAbsUrl');
  • for List you could return url of display form page

Example

function getListItemUrl(listTitle,itemId,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var item = list.getItemById(itemId);
   ctx.load(item,['EncodedAbsUrl','FileRef']);
   ctx.load(list,['BaseType','DefaultDisplayFormUrl']);
   ctx.executeQueryAsync(
      function() {
            var url;
            var baseType = list.get_baseType();
            if(baseType === SP.BaseType.documentLibrary) {
                 url = item.get_item('EncodedAbsUrl'); 
            }
            else {
                 var absSiteUrl = item.get_item('EncodedAbsUrl').replace(item.get_item('FileRef'),''); 
                 url = absSiteUrl + list.get_defaultDisplayFormUrl() + "?ID=" + itemId;
            }   
            success(url);
      },
      error);
}

Usage

var listTitle = 'Documents';
var itemId = 1;

getListItemUrl(listTitle,itemId, 
   function(url){
       console.log(url);
   },
   function(sendera,args){
       console.log(args.get_message());
   });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.