2

I am really new in SharePoint dev, and sorry in advance if I am asking something weird or a completely wrong with this question.

The app is a SPA in angular (my world :)) which uses a sharepoint helper service to do some operation. In this example I am adding an item in a list (folder), everything works fine, the only thing is that in the method .set_folderUrl() I use an hardcoded path to the list. I would like to get the path url through another method (instead of the hardcoded path), if exists, from /sites of the list "/sites/nameSite/Lists/ListName/""

Many thanks.

        // Get current web (comparable to SPWeb)
        var oWeb = hostContext.get_web();
        // Get list by title (comparable to SPList)
        var oList = oWeb.get_lists().getByTitle(listName);
        // Create new object for list item creation
        clientContext.load(oList);
        //Create List Item Infor and set the Folder Path for the item to be created.
        var listItemCreationInfo = new SP.ListItemCreationInformation();


        listItemCreationInfo.set_folderUrl("/sites/nameSite/Lists/ListName/" + rootFolder);
        var itemInfo = oList.addItem(listItemCreationInfo);

        angular.forEach(data, function (value, prop) {
                if (value != null) {
                    itemInfo.set_item(prop, value);
                }
        });


        // Update the new item
        itemInfo.update();
3

You could get the list root folder relative url.

Include 'RootFolder' when loading the list and get firstly the rootFolder and secondly the rootFolder relative url.

// Get current web (comparable to SPWeb)
var oWeb = hostContext.get_web();
// Get list by title (comparable to SPList)
var oList = oWeb.get_lists().getByTitle(listName);
// Create new object for list item creation
clientContext.load(oList, 'RootFolder');
//Create List Item Infor and set the Folder Path for the item to be created.
var listItemCreationInfo = new SP.ListItemCreationInformation();

var rootFolder = oList.get_rootFolder();
var url = rootFolder.get_serverRelativeUrl();

listItemCreationInfo.set_folderUrl(url);
var itemInfo = oList.addItem(listItemCreationInfo);

angular.forEach(data, function (value, prop) {
    if (value != null) {
        itemInfo.set_item(prop, value);
    }
});

// Update the new item
itemInfo.update();

(I have not tested the code)

  • Include is for collections, you'll only get List back. Use: clientContext.load(oList, 'RootFolder'); – Anders Aune Dec 4 '16 at 16:30
0

You can get the server relative url of the list dynamically from the list object as given below:

var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists,'Include(RootFolder)');
context.executeQueryAsync(function () {
var enumerator = lists.getEnumerator();
while (enumerator.moveNext()) {
   var list = enumerator.get_current();
   var rootFolder = list.get_rootFolder();

   var url = rootFolder.get_serverRelativeUrl();
   console.log(url);

}
},
function (sender, args) {
 console.log('error');
});   
0

You have to load list rootfolder to get url of list:

var context = SP.ClientContext.get_current();

var list = web.get_lists().getByTitle("ListName"); // Name of the list
context.load(list);
context.load(list.get_rootFolder());

context.executeQueryAsync(onSuccess, onFail);

function onSuccess() {
  var rootFolder = list.get_rootFolder();
  var listURL = rootFolder.get_serverRelativeUrl(); // URL of the list
}

function onFail(sender, args) {
  console.log('Request Failed: ' + args.get_message() + '\n' + args.get_stackTrace());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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