Tuesday 23 January 2018

How to set any SP.Field Value with REST API (Javascript) in Sharepoint 2013 to New SP.Listitem

Here is an example on how to create a new SP.ListItem using java script in SharePoint 2013 and setting most of the available fields in code using REST API.









 $(document).ready(function() {  
   var digestValue = $("#__REQUESTDIGEST").val();  
   var url = "/_api/Web/Lists/GetByTitle('TestList')/Items";  
   
   $("#clickBtn").on('click', function(e) {  
     e.preventDefault();  
     addNewItem(url);  
   });  
   
   function addNewItem(url, data) {  
   
     var data = {  
       __metadata: {  
         type: 'SP.Data.TestListListItem'  
       },  
       Title: 'Some title',  
       PetkaMultiText: 'Some multiline text. <bold>Can Be HTML</bold>',  
       PetkaChoiceDrop: 'Choice #2',  
       PetkaChoiceMulti: {  
         __metadata: {  
           type: "Collection(Edm.String)"  
         },  
         results: [  
           "Choice #1",  
           "Choice #2"  
         ]  
       },  
       PetkaPersonSingleId: 19,  
       PetkaPersonMultiId: {  
         __metadata: {  
           type: "Collection(Edm.Int32)"  
         },  
         results: [  
           1,  
           18  
         ]  
       },  
       PetkaManagedSingle: {  
         "__metadata": {  
           "type": "SP.Taxonomy.TaxonomyFieldValue"  
         },  
         "Label": "3",  
         "TermGuid": "08a8397f-2165-47cf-b185-37f0c40777e5",  
         "WssId": 3  
       },  
       // Not supported at this time. If you know how to make it work, let me know.  
       //PetkaManagedMulti: {  
       //  __metadata: {  
       //    type: "Collection(SP.Taxonomy.TaxonomyFieldValue)"  
       //  },  
       //  results: [{  
       //    "Label": "In the Press",  
       //    "TermGuid": "02832069-36a5-4d2b-af89-0e5378fc7cef",  
       //    "WssId": 1  
       //  }, {  
       //    "Label": "News & Announcements",  
       //    "TermGuid": "6a12f6bc-1174-4cea-a48a-cc6f4f7040d3",  
       //    "WssId": 2  
       //  }]  
       //},  
       PetkaLookupId: 2,  
          PetkaLookupMultiId: {  
            __metadata: {  
              type: "Collection(Edm.Int32)"  
            },  
            results: [  
              1,  
              2  
            ]  
          },  
       PetkaNumber: 1,  
       PetkaCurrency: 34,  
       PetkaDateTime: new Date().toISOString(),  
       PetkaYesNo: true,  
       PetkaHyperLink: {  
         __metadata: {  
           type: "SP.FieldUrlValue"  
         },  
         Url: "http://google.com",  
         Description: "Google Url Description"  
       }  
     };  
   
     $.ajax({  
       url: _spPageContextInfo.webAbsoluteUrl + url,  
       type: "POST",  
       headers: {  
         "accept": "application/json;odata=verbose",  
         "X-RequestDigest": digestValue,  
         "content-Type": "application/json;odata=verbose"  
       },  
       data: JSON.stringify(data),  
       success: function(data) {  
         console.log(data);  
       },  
       error: function(error) {  
         alert(JSON.stringify(error));  
       }  
     });  
   }  
 });  

No comments:

Post a Comment

Featured post

Data connections in Infopath forms

https://www.qdoscc.com/blog/how-automatically-retrieve-current-username-infopath-sharepoint-list-form

Popular Posts