1

I have searched high and low and cannot find any existing article or guide that adequately shows me how to do something very simple. I am not very well versed in Javascript so might be making mistakes, but hoping the answer is a simple one.

I have a standard list in a SharePoint 2013 site, with a few custom fields. One field is a single line text field called "Link". All i want to do is have a script sitting in a script editor web part on the (OOTB) display form which will display the value of the 'link' field for the current item that is being displayed in the form.

My plan from there will be to build a URL from this data, and to do that i am fine with. It's just getting the value from SharePoint into a global variable that is making me go round in circles.

Can anyone help me with the code i need to simply pull the 'link' field value from the SharePoint list for the current item that is being displayed and store it in a global variable (so i can then go on to use it in code to build a link and other stuff)?

  • If you have tried any code, can you please share that code block. – Rohit Waghela Feb 16 '17 at 10:49
2

Please try with below code.

// A $( document ).ready() block.
$( document ).ready(function() {
var urlGetItem = "/_api/Web/Lists/GetByTitle('SpTutorial')/Items(" + Your Item Id + ")";
    getItems(urlGetItem);
});

function getItems(url) {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            console.log(data.d.Link);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}
| improve this answer | |
  • Have tried this. Have saved a copy of jquery-3.1.1.min.js in a library on the site and referenced it in the opening code. Have used the code you have provided about but am getting an error displayed in an alert window when the page is loaded: {“readyState”:4,”responseText”:”404 NOT FOUND”,”status”:404,”statusText”:”NOT FOUND”} – AndyFlems Feb 16 '17 at 12:55
  • Have you pass item id and your list display name in below url? /_api/Web/Lists/GetByTitle('SpTutorial')/Items(" + Your Item Id + ") Please check with edited code – Samir Khimani Feb 16 '17 at 12:56
  • Yes. The list is a random one called 'hgfdsa' i created in the interest of proving the concept. And the item is ID 1, so for the urlGetItem i have put: "/_api/Web/Lists/GetByTitle('hgfdsa')/Items(" + 1 + ")"; – AndyFlems Feb 16 '17 at 12:58
  • "/_api/Web/Lists/GetByTitle('hgfdsa')/Items(1) Use above url – Samir Khimani Feb 16 '17 at 12:59
  • Have tired: var urlGetItem = "_api/Web/Lists/GetByTitle('hgfdsa')/Items(1)"; but still get the same error. – AndyFlems Feb 16 '17 at 13:00
0

The dispform.aspx should have a markup somewhat like below (checked via inspect element):

<tr>
    <td nowrap="true" valign="top" width="165px" class="ms-formlabel">
        <h3 class="ms-standardheader">
            <a name="SPBookmark_Link"></a>
            Link
        </h3>
    </td>
    <td valign="top" class="ms-formbody" width="450px" id="SPFieldText">
    <!-- FieldName="Link"
             FieldInternalName="Link"
             FieldType=" Link"
      -->
            This is some text present in the link field
    </td>
</tr>

So you need to fetch This is some text present in the link field value present.

For that you can use the below jquery:

var linkValue = $('h3:contains("Link")').closest('td').next('td').text();
console.log(linkValue.trim());

Here Link is the internal name of the column

| improve this answer | |
  • Thanks for this suggestion, but i was looking for the code to get the data direct from the list source as opposed to picking it up from somewhere else on the same page. – AndyFlems Feb 16 '17 at 13:31
  • Ok but why make an additional rest call when the data is already available on the page ? Its simply a question of traversing DOM and getting what you want. – Gautam Sheth Feb 16 '17 at 13:47

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.