1

Before explaining my problem, I give you the context.

I've made an first page that will display a "Marquee" message (message that will scroll horizontally).

I would like to make possible to edit the "Marquee message" from an another page. So, I've created an other page with a textarea and a button. When I write a text and click on the button, an item is created in a list called "MarqueeList".

That list contain only the last message that we create : my script delete all the items before add a new one.

My problem is that I would to get that message item, using a script from the page that contain the marquee, and store it into a variable.

I've tried many and any script, but it doesn't work.

If someone could help me, or advise me to the good way?

Thanks a lot

Here is my code with API REST :

function retrieveItem(){
$.ajax({
    url : "<siteURL>/_api/web/lists/getbytitle('MarqueeMessage')/items?$orderby=ID desc&$top=1",
    type : "GET",
    data : data,
    headers:  
    {  
        "Accept": "application/json;odata=verbose",  
        "Content-Type": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "IF-MATCH": "*",  
        "X-HTTP-Method": null  
    },  
    cache: false,  
    success : onSuccess,
    error : onError
});

function onSuccess(data){   
        var item = data.d.results;  
        var msg = item.Message;
        alert(msg);
}

function onError(error)  {  
    alert(JSON.stringify(error));
}    
}
3
  • which version of SharePoint you are using? – Ganesh Sanap Jul 2 '20 at 8:24
  • I'm using Sharepoint 2013 – TLAsker Jul 2 '20 at 8:26
  • Check below answer, try it and let me know if it works for you. – Ganesh Sanap Jul 2 '20 at 8:29
0

You can use SharePoint REST API to get the list item and use its column values to show on the page.

Endpoint:

<siteURL>/_api/web/lists/getbytitle('List Name')/items

If you want to get the latest created item (single list item) then you can use:

<siteURL>/_api/web/lists/getbytitle('List Name')/items?$orderby=ID desc&$top=1

References:

  1. Working with lists and list items with REST
  2. CRUD Operation On List Items Using REST API Services In SharePoint 2013

Update from Comments:

You can try using below code:

function retrieveItem() {
    $.ajax({
        url : "<siteURL>/_api/web/lists/getbytitle('MarqueeMessage')/items?$orderby=ID desc&$top=1",
        type : "GET",
        headers: {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose"
        },  
        cache: false,  
        success: function(data) {
            var listItems = data.d.results;
            if(listItems && listItems.length) {
                var msg = listItems[0].Message; // Here you need to use the internal name of your "Message" column
                console.log(msg);
            }
        },
        error: function(error) {  
            console.log(error);
        }
    });
}
14
  • 1
    Thanks for your answer. I will try :) – TLAsker Jul 2 '20 at 8:31
  • Sure, try it and comment here if you come across any error or issue. – Ganesh Sanap Jul 2 '20 at 8:33
  • I've tried something, I edit my post with the code. It doesn't work, I haven't got the alert. I'm not very sure about what I have done :/ – TLAsker Jul 2 '20 at 11:24
  • Are you getting any error in browser console? If Yes, please add the error message to your question. Also try the code given in my updated answer. – Ganesh Sanap Jul 2 '20 at 11:54
  • I'm getting an error at the line 12 > "data : data,". The error = "Uncaught ReferenceError : data is not defined" – TLAsker Jul 2 '20 at 12:09
0

Is there a chance the list is in a different domain than the page with marquee?

If that is the case, you need to change the "Context" object and point it to the site containing the list.

For Rest API :

var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
  {
    url: appweburl +
          "/_api/SP.AppContextSite(@target)/web/lists?@target='" +
          hostweburl + "'",
    method: "GET",
    success: successHandler,
    error: errorHandler
  }
)

Reference: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints

if you are using JSOM:

$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
});


// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(<app web url>);
// Illustrates how to use the executeAsync method.
// Function to prepare and issue the request to get
//  SharePoint data.
       function execCrossDomainRequest() {
   var executor;

       // Initialize the RequestExecutor with the app web URL.
       executor = new SP.RequestExecutor(appweburl);

       // Issue the call against the host web.
       // To get the title using REST we can hit the endpoint:
       //      app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl'
       // The response formats the data in the JSON format.
       // The functions successHandler and errorHandler attend the
       //      success and error events respectively.
       executor.executeAsync(
           {
               url:
                   appweburl +
                   "/_api/SP.AppContextSite(@target)/web/title?@target='" +
                   hostweburl + "'",
                   method: "GET",
                   headers: { "Accept": "application/json; odata=verbose" },
                   success: successHandler,
                   error: errorHandler
                }
            );
        }

       // Function to handle the success event.
       // Prints the host web's title to the page.
       function successHandler(data) {
           var jsonObject = JSON.parse(data.body);

           document.getElementById("HostwebTitle").innerHTML =
               "<b>" + jsonObject.d.Title + "</b>";
       }

       // Function to handle the error event.
       // Prints the error message to the page.
       function errorHandler(data, errorCode, errorMessage) {
           document.getElementById("HostwebTitle").innerText =
               "Could not complete cross-domain call: " + errorMessage;
       }

Reference: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/dn735924(v%3Doffice.15)

1
  • Thanks for your answer ! No, they are all on the same domain. I will try your solution. – TLAsker Jul 2 '20 at 8:55

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.