1

After doing a lot of research on REST, I'm stuck. Any help would be appreciated. I want to users to be able to update the status of a Task by clicking a button which takes them to a url.

  • The column is a choice column, and it's called formStatus, with (3) choices (Ongoing, Completed, and Cancelled)
  • The url is to a Site Page called '../../sitepages/taskslanding.aspx', and the script lives on that site page in the content editor
  • I've named the scrip 'jsomJs.js', which is below:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { var query = window.location.search.substring(1); var vars = query.split("="); var itemId = vars[1]; var ctx = new SP.ClientContext.get_current(); var customList = ctx.get_web().get_lists().getByTitle('Tasks'); var listItem = customList.getItemById(itemId); listItem.set_item('formStatus', 'Completed'); listItem.update(); ctx.executeQueryAsync( function(){ alert('Item updated'); }, function(sender, args){ alert('Error: ' + args.get_message()); } ); });

I created a url for each list item, when clicked, fired the following (I'm using Task ID=5 just as an example)

<a href="../../sitepages/taskslanding.aspx?ID=5>Approve This Task</a>

UPDATE 2/6/19

For whatever reason, this code ONLY works on Task (ID #103). When entering the url ../../tasklanding.aspx?ID=103, this code works as expected.

My Task IDs start at #33 and end on #945, wo #103 isn't even the earliest task! May I add that this code does NOT work on Task #100, #101, #102, #104, etc. Any one have ANY idea how to fix this? Or why it is happening?

  • 1
    Technically, this is not the REST API, this is JSOM (JavaScript Object Model), you might get better answers if you re-tag it as such. – Dylan Cristy Dec 18 '18 at 17:05
  • 1
    The "Sorry, something went wrong" error page is not being caused by your Javascript throwing an error because it can't find the task with ID 870. Whatever errors your Javascript throws will show up in the alert you put in your fail handler. I would say the "Sorry something went wrong" error has more to do with the taskslanding.aspx page than your script. Is that page checked out? Is there some other web part on that page that's trying to do something? – Dylan Cristy Dec 20 '18 at 17:32
  • Thanks for the response. The ../../SitePages/taskslanding.aspx has (1) content editor on the page, which points to the Site Assets library js file containing the EXACT code in the original question. ID# 870 exists, and I am confused as to what isn't working. Do I need to reference the list name (Tasks) in the query string? Are we certain that I can run this script from a Site Page? – TheJDScott Dec 20 '18 at 20:40
  • 1
    Hmm... I don't know if I would link to a js file from a CEWP. You're doing that using the Content Link property? If so, what I would do is this: create an HTML file that has script tags that link to your js file, and pull in the HTML file using the CEWP Content Link. See this other answer where I am kind of explaining the same thing. You definitely should be able to run that code on a site page, it seems like it's not getting loaded on to the page correctly. – Dylan Cristy Dec 20 '18 at 22:39
1

I tested your script in my online environment, it works fine.

For error "No item exists...", confirm the ID exists.

The error "Input string was not in a correct format" caused by you got wrong ID parameter as there are other parameters, you could use below script to get parameter instead.

function getQueryStringParameter(paramToRetrieve) {
            var params = document.URL.split("?")[1].split("&");
            for (var i = 0; i < params.length; i = i + 1) {
                var singleParam = params[i].split("=");
                if (singleParam[0] == paramToRetrieve) return singleParam[1];
            }
        }

Update:

Try combine as below:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function()
        {
    var itemId =getQueryStringParameter('ID');
    var ctx = new SP.ClientContext.get_current();
    var customList = ctx.get_web().get_lists().getByTitle('Tasks');
    var listItem = customList.getItemById(itemId);
    listItem.set_item('formStatus', 'Completed');
    listItem.update();
    ctx.executeQueryAsync(
        function(){ alert('Item updated'); }, 
        function(sender, args){ alert('Error: ' + args.get_message()); }
    );
    });
function getQueryStringParameter(paramToRetrieve) {
            var params = document.URL.split("?")[1].split("&");
            for (var i = 0; i < params.length; i = i + 1) {
                var singleParam = params[i].split("=");
                if (singleParam[0] == paramToRetrieve) return singleParam[1];
            }
        }
  • Thanks Lee -- I updated my post so you can see the error messages and ensure the ID exists. Still getting an error and have no idea what it's about. As far as your suggestion, should I be adding this to my existing code or combining them? – TheJDScott Dec 20 '18 at 17:04
  • 1
    Try the updated code. – Lee_MSFT Dec 21 '18 at 0:53
  • I appreciate your help. I've updated and added the code (in both Script Ed. and Content Ed. -- screenshot here: link), it's still saying the item doesn't exist. I'm at a loss -- what am I not seeing? – TheJDScott Dec 21 '18 at 16:33
1

SP.List.getItemById() takes a number as an argument, but your query string parameter is a string. (Documentation here.)

Try

var itemId = parseInt(getQueryStringParameter('ID'));

or

var itemId = Number(getQueryStringParameter('ID'));
  • Thanks, dude -- I tried them both, and still telling me the item doesn't exist. I've been at this for the past few days and I cannot figure out what I'm doing wrong. – TheJDScott Dec 21 '18 at 18:49
  • 1
    Yeah, sorry we have been unable to help figure it out. – Dylan Cristy Dec 21 '18 at 18:58
  • Appreciate all of the time you've spent. Will update this if I ever figure it out. – TheJDScott Dec 21 '18 at 19:21

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.