Share via

I want to update EventStatus column field when AutopublishDate column date is equal to todays date and AutopublishTimes column time is equal to or smaller than todays time

Anonymous
2022-12-20T07:06:18.543+00:00

I have this sharepoint list, I want to update EventStatus column field when AutopublishDate column date is equal to todays date and AutopublishTimes column time is equal to or smaller than todays time please guid me to solve this.

I am using below code

function UpdateAutopublishToPublish() {

$.ajax({

    async: true,  // Async by default is set to “true” load the script asynchronously  
    url:"https://(list-name)/_api/web/lists/GetByTitle('Events')/items?$select=ID,AutopublishDate,AutopublishTimes,EventStatus,AutopublishTime&$filter=EventStatus eq 'Autopublish'",   // URL to fetch data from sharepoint list  
    method: "GET",  //Specifies the operation to fetch the list item  

    headers: {    
        "accept": "application/json;odata=verbose",   //It defines the Data format  
        "content-type": "application/json;odata=verbose"   //It defines the content type as JSON  

    },    
    success: function(data) {   
    data = data.d.results;  
    $.each(data, function(index, value) {  
      var AutopublishDate1 = value.AutopublishDate;  
      var AutopublishTimes1 = value.AutopublishTimes;  
        
      var d = new Date();  
      var etimeOnly = d.toLocaleString([], { hour: '2-digit', minute: '2-digit' });  
      var edateOnly = d.toLocaleDateString();  
        
      var convertAutopublishDate = moment(AutopublishDate1).utc().format('MM/DD/YYYY');   
        

           var echangetoPublish = "Published"               
        
      if (convertAutopublishDate == edateOnly && AutopublishTimes1 <= etimeOnly){  
      alert(AutopublishTimes1+ "-----" +etimeOnly);  
      alert("Done");  

      $.ajax({  
                   url: "https://(list-name)/_api/web/lists/GetByTitle('Events')/items?$select=ID,AutopublishDate,AutopublishTimes,EventStatus&$filter=EventStatus eq'Autopublish'",  

type: "POST",
async:true,
data: JSON.stringify({
'__metadata': {
'type': 'SP.Data.EventsListItem'

            },  
             'EventStatus': echangetoPublish  
          }),  
          headers: {  
            "accept": "application/json;odata=verbose",  
            "content-type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",             //Overrite the changes in the sharepoint list item  
            "X-HTTP-Method": "MERGE"      // Specifies the update operation in sharepoint list  
          },  
          success: function (data) {  
            alert("Item Updated successfully", "success");  
            //Reninitialize the datatable  



          },  
          error: function (error) {  
            console.log(error);  

          }  

        });//Post ajax end  
        
        
      }  
      });  
        
        
    },    
    error: function(error) {    
        console.log(JSON.stringify(error));    

    }    

})    
   

}

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | ASP.NET | Other
0 comments No comments

Answer accepted by question author
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2022-12-21T02:15:22.3+00:00

    Hi @Anonymous
    Per my test, you can get all items id which AutopublishDate column date is equal to todays date and AutopublishTimes column time is equal to or smaller than todays time by rest api. And update column EventStatus in these items. Please refer to following code

    function UpdateListItem()  
    {  
           GetItemId();  
    }  
       
    function GetItemId()  
    {  
            $.ajax  
            ({  
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Events')/items?$filter=(AutopublishDate eq datetime" + today.toISOString() + ") and (AutopublishTimes le datetime" + today.toISOString() + ')',  
                type: "GET",  
                headers:  
                   {  
                       "Accept": "application/json;odata=verbose"  
                   },  
                success: function (data, status, xhr) {  
                    var dataresults = data.d.results;  
    		for (var i = 0; i < data.d.results.length; i++) {  
                            var Id = data.d.results[i]["Id"];  
                            UpdateListItemUsingItemId(Id);    
                        }                          
                                   
                },  
                error: function (xhr, status, error) {  
                    console.log("Failed");  
                }  
            });   
    }  
       
    function UpdateListItemUsingItemId(Id) {  
           $.ajax   
            ({  
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Events')/items(" + Id +")",  
            type: "POST",          
            headers:   
            {   
                // Accept header: Specifies the format for response data from the server.  
                "Accept": "application/json;odata=verbose",  
                //Content-Type header: Specifies the format of the data that the client is sending to the server  
                "Content-Type": "application/json;odata=verbose",  
                // IF-MATCH header: Provides a way to verify that the object being changed has not been changed since it was last retrieved.  
                // "IF-MATCH":"*", will overwrite any modification in the object, since it was last retrieved.  
                 "IF-MATCH": "*",  
                //X-HTTP-Method:  The MERGE method updates only the properties of the entity , while the PUT method replaces the existing entity with a new one that you supply in the body of the POST  
                "X-HTTP-Method": "MERGE",  
                // X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header  
                "X-RequestDigest": $("#__REQUESTDIGEST").val()  
            },  
             data: JSON.stringify({  
             __metadata:  
                {  
                  // Format of the "type" is: SP.Data.<<ListName>>ListItem. First character of the <<ListName>> should be in Capital  
                  type: "SP.Data.EventsListItem"  
                },  
                EventStatus: "echangetoPublish"  
            }),  
            success: function(data, status, xhr)   
            {   
                console.log("Success");       
            },   
            error: function(xhr, status, error)   
            {   
                console.log("Failed");  
            }   
        });  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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 additional answers

Sort by: Most helpful

Your answer

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