Javascript onclick

Olaf Weisse 21 Reputation points
2021-06-16T09:10:43.917+00:00

HI I need some help with a script.
Scenario: I have a Sharepoint Online Subsite with some informations, I want that users can click on a button (onclick) which then opens a list for entering date to the List for maybe changes comments to the subsite. But now the question, I wanted to have the URL of the Subsite also in teh list on title Column, so I searched found some informations about windows.href and I think I need this is a variable to hand it over to the list.

I searched the Net but didn´t found what I needed.

Any ideas here how to get that done, I have some code with which I can read out the URL, and save it into varable, and also how to onclick open the list bur I nend to combine that.

Regards Olaf

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-06-17T06:50:43.567+00:00

    Hi @Olaf Weisse ,
    We can retrieve URL first, then we need to create a new item of URL. Finally we will redirect to the list.
    Here is my code for sample:

            //retrieve url  
            var SubsiteUrl = window.location.href + "";  
            //create new item  
            $.ajax  
                ({  
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Js List')/items",  
                    type: "POST",  
                    headers:  
                {  
                    "Accept": "application/json;odata=verbose",  
                    "Content-Type": "application/json;odata=verbose",  
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()  
                },  
                    data: JSON.stringify  
                ({  
                    __metadata:  
                    {  
                        type: "SP.Data.Js_x0020_ListListItem"  
                    },  
                    Title: SubsiteUrl  
                }),  
                    success: function (data, status, xhr) {  
                        //redirect to list  
                        window.location.replace("list url");  
                    },  
                    error: function (xhr, status, error) {  
                    }  
                });  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Weisse 21 Reputation points
    2021-06-17T11:40:45.213+00:00

    RaytheonXie-MSFT,
    thanks for your answer and the code, I will check it next week after my vacation.
    Regards Olaf

    0 comments No comments

  2. Olaf Weisse 21 Reputation points
    2021-06-21T07:46:21.317+00:00

    HI RaytheonXie-MSFT,
    Thanks again for the Code. I think I undersatnd what you want to do here, buit I missed to say in my frist Post i`m not a prgrammer so I need some more help.
    How do I have to implement the Code, as xxx.js at Site Assets or in a Content Editor Webpart, really novize in Programming.

    Thanks
    Olaf


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.