0

I have a dropdown menu that redirects me to a website when I click on an item.

<select onChange="window.location.href=this.value">
    <option value='none' selected>Please Select WebSite...</option>
    <option value="http://www.Link1.com">Title1</option>
    <option value="http://www.Link2.com">Title2</option>
    <option value="http://www.Link3.com">Title3</option>
</select>

I want to use this script with SharePoint and I'm wondering how I can retrieve the name and the link from a SharePoint list.

My Sharepoint list is named "WEB" and it has two columns:

  • "Title" (Text)
  • "Link" (Href)
1
  • What version of SharePoint are you working with?
    – Thriggle
    Sep 15, 2016 at 18:13

2 Answers 2

0

This code gets data from the list. Copy paste this to the console to see it work or to the script editor web part.

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('WEB');
var query = SP.CamlQuery.createAllItemsQuery();
var items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(function() {
    for (var i = 0; i < items.get_count(); i++) {
        var item = items.getItemAtIndex(0);
        var obj = item.get_fieldValues();
        console.log(obj); // this is sharepoint list item object
        console.log(obj.Title); // the Title
        console.log(obj.Link); // the Link
    }
});
0

Depending on which version of SharePoint you are using, the answer could be different. We are currently running SharePoint 2010 in our Production environment, and I have used the SPServices Library to do things like this in the past. You can find the source code and documentation here:

https://spservices.codeplex.com/

A basic example would look something like this:

var $j10 = $.noConflict(true);

$j10(document).ready(function() {

  var list = "Current Sites";

  /* Fields within the List you want to utilize or display */

  var fields = "<ViewFields>" +
    "<FieldRef Name='Title' />" +
    "<FieldRef Name='Link' />" +
    "</ViewFields>";

  /* Query parameters for the data set you want to return, in CAML syntax */

  var query = "<Query>" +
    "<OrderBy>" +
    "<FieldRef Name='Title' Ascending='TRUE' />" +
    "</OrderBy>" +
    "</Query>";

  /* Method Call */

  GetListItems(list, fields, query);

});

function GetListItems(listName, listFields, listQuery) {
  $j10().SPServices({

    operation: "GetListItems",
    listName: listName,
    CAMLViewFields: listFields,
    CAMLQuery: listQuery,

    completefunc: function(xData, Status) {

      $j10(xData.responseXML).SPFilterNode("z:row").each(function() {

        /* Any processing required for each row of data begins here... */

        /* Get Field Data */

        var name = ($j10(this).attr("ows_SiteName"));
        var url = ($j10(this).attr("ows_Url"));

        /* Process and Display Field Data */

        $j10("#yourContainerID").append("<option value='" + url.substr(url.indexOf("#") + 1) + "'>" + name.substr(name.indexOf("#") + 1) + "</option>");
      });
    }
  });
}

In order to get this to run, you would need to register the appropriate versions of the SPServices script and jQuery library on the page. This is well documented on the codeplex site referenced above.

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.