{"id":78,"date":"2018-05-16T22:06:00","date_gmt":"2018-05-16T22:06:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/05\/16\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps\/"},"modified":"2020-07-25T22:39:23","modified_gmt":"2020-07-25T20:39:23","slug":"how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps\/","title":{"rendered":"How to Post Data to a SharePoint List with AngularJS &#8211; Easy Steps"},"content":{"rendered":"<p>Hello, good to see you here.<br \/>\nBe sure that it really is not so difficult to post data from a form to a SharePoint List. I would show you the steps and also give you the codes.<\/p>\n<div style=\"text-align: center;\">Just 5 Steps. Lets go!<\/div>\n<p>&nbsp;<\/p>\n<p><b>Content:<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps#s1\">Step 1: Create a SharePoint List in SharePoint<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps#s2\">Step 2: Add BootStrap, JQuery and AngularJS to Header<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps#s3\">Step 3: Add the html Form<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps#s4\">Step 4: Code the Script File<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-post-data-to-a-sharepoint-list-with-angularjs-easy-steps#s5\">Step 5: Test your page!<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>If you have any issue though, just let me know. Use the comment box below or the form to left of this page.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"s1\">Step 1: Create a SharePoint List<\/h3>\n<p>Create a list in SharePoint and called CustomerDetails.<br \/>\nThis list should contain the following fields three fields.<\/p>\n<ul>\n<li>Firstname<\/li>\n<li>Lastname<\/li>\n<li>Address<\/li>\n<\/ul>\n<p>We are keeping it very simple so you will understand exactly how it works.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"s2\">Step 2: Add Bootstrap, AngularJS and JQuery<\/h3>\n<p>Create a html page and add AngularJS and BootStrap to the header like shown below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;title&gt;<\/span>AngularJS Tutorial<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"bootstrap.js\"<\/span><span style=\"color: #007700;\">&gt;<\/span> <span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"application\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"jquery.min.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"application\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"angular.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"application\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"index.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n...\r\n\/\/We would place the form here\r\n...\r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p><b>Listing 1<\/b>: Add files in header section<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"s3\">Step 3: Add the html Form<\/h3>\n<p>Add the following html to the body of your page, in between the &lt;body&gt; and &lt;\/body&gt; tags. This is the form that would be filled to be submitted to the SharePoint list<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;form<\/span> <span style=\"color: #0000cc;\">name=<\/span><span style=\"background-color: #fff0f0;\">\"userForm\"<\/span> <span style=\"color: #0000cc;\">ng-app=<\/span><span style=\"background-color: #fff0f0;\">\"myapp\"<\/span> <span style=\"color: #0000cc;\">ng-controller=<\/span><span style=\"background-color: #fff0f0;\">\"myController\"<\/span> <span style=\"color: #007700;\">&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-group\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n <span style=\"color: #007700;\">&lt;label<\/span> <span style=\"color: #0000cc;\">for=<\/span><span style=\"background-color: #fff0f0;\">\"firstname\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Firstname <span style=\"color: #007700;\">&lt;\/label&gt;<\/span> \r\n <span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">ng-model=<\/span><span style=\"background-color: #fff0f0;\">\"Firstname\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-control\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"firstname\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>    \r\n  <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-group\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n <span style=\"color: #007700;\">&lt;label<\/span> <span style=\"color: #0000cc;\">for=<\/span><span style=\"background-color: #fff0f0;\">\"lastname\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Lastname:<span style=\"color: #007700;\">&lt;\/label&gt;<\/span>\r\n <span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">ng-model=<\/span><span style=\"background-color: #fff0f0;\">\"Lastname\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-control\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"lastname\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-group\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n <span style=\"color: #007700;\">&lt;label<\/span> <span style=\"color: #0000cc;\">for=<\/span><span style=\"background-color: #fff0f0;\">\"address\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Address:<span style=\"color: #007700;\">&lt;\/label&gt;<\/span>\r\n <span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">ng-model=<\/span><span style=\"background-color: #fff0f0;\">\"Address\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-control\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"address\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;button<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"submit\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"btn btn-primary\"<\/span>  <span style=\"color: #0000cc;\">ng-click=<\/span><span style=\"background-color: #fff0f0;\">\"submitCustomer()\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"btn btn-default\"<\/span> <span style=\"color: #0000cc;\">data-dismiss=<\/span><span style=\"background-color: #fff0f0;\">\"modal\"<\/span><span style=\"color: #007700;\">&gt;<\/span>Submit<span style=\"color: #007700;\">&lt;\/button&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/form&gt;<\/span><\/pre>\n<p><b>Listing 2:<\/b> Code the HTML Form<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"s4\">Step 4: Add the Script<\/h3>\n<p>Add the following code to your AngularJS script. Name this script index.js. It&#8217;s a bit complicated but I want you to first try it, then I would explain how it works to you or help you debug any error you may be getting. Remember to link this script to your html page and place the two in the same folder<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> myapp <span style=\"color: #333333;\">=<\/span> angular.module(<span style=\"background-color: #fff0f0;\">'myapp'<\/span>, []);\r\nmyapp.controller(<span style=\"background-color: #fff0f0;\">'myController'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>($q, $scope, $http){\r\n\r\n<span style=\"color: #888888;\">\/\/add nomination to start workflow <\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">function<\/span> getRequestDigest(){\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> deferred <span style=\"color: #333333;\">=<\/span> $q.defer();\r\n$http ({\r\nurl<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"RequestDigest url \"<\/span>, <span style=\"color: #888888;\">\/\/Get this url from your IT department<\/span>\r\nmethod<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"POST\"<\/span>,\r\nheaders<span style=\"color: #333333;\">:<\/span> {\r\n<span style=\"background-color: #fff0f0;\">\"Content-Type\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"application\/json; odata=verbose\"<\/span>,\r\n<span style=\"background-color: #fff0f0;\">\"Accept\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"application\/json; odata=verbose\"<\/span>\r\n}\r\n}).success(<span style=\"color: #008800; font-weight: bold;\">function<\/span> (response) {\r\nconsole.log(<span style=\"background-color: #fff0f0;\">'X-RequestDigest value:'<\/span>, response.d.GetContextWebInformation.FormDigestValue);\r\n$scope.requestDigest <span style=\"color: #333333;\">=<\/span> response.d.GetContextWebInformation.FormDigestValue;\r\ndeferred.resolve(response.d);\r\n}).error(<span style=\"color: #008800; font-weight: bold;\">function<\/span> (err) {\r\nconsole.log(<span style=\"background-color: #fff0f0;\">'Error in POST request digest'<\/span>, err);\r\ndeferred.reject(err);\r\n});\r\n<span style=\"color: #008800; font-weight: bold;\">return<\/span> deferred.promise;\r\n}\r\n<span style=\"color: #888888;\">\/\/digest end<\/span>\r\n\r\n<span style=\"color: #888888;\">\/\/FUNCTION TO POST ITEM TO THE DATABASE<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">function<\/span> AddCustomer(listName){\r\n$scope.submitSuccess<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">false<\/span>;\r\n$scope.submitError <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>;\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> deferred <span style=\"color: #333333;\">=<\/span> $q.defer();\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> requestData;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> GetItemTypeForListName <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">function<\/span> (name) {\r\n<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"SP.Data.\"<\/span> <span style=\"color: #333333;\">+<\/span> name.charAt(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>).toUpperCase() <span style=\"color: #333333;\">+<\/span> name.split(<span style=\"background-color: #fff0f0;\">\" \"<\/span>).join(<span style=\"background-color: #fff0f0;\">\"\"<\/span>).slice(<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>) <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"ListItem\"<\/span>;\r\n};\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> itemType <span style=\"color: #333333;\">=<\/span> GetItemTypeForListName(listName);\r\n\r\n<span style=\"color: #888888;\">\/\/JSON BODY OF THE REQUEST<\/span>\r\nrequestData <span style=\"color: #333333;\">=<\/span> {\r\n<span style=\"background-color: #fff0f0;\">\"Title\"<\/span><span style=\"color: #333333;\">:<\/span> $scope.Firstname,\r\n<span style=\"background-color: #fff0f0;\">\"LastNamePhonetic\"<\/span><span style=\"color: #333333;\">:<\/span> $scope.Lastname,\r\n<span style=\"background-color: #fff0f0;\">\"WorkAddress\"<\/span><span style=\"color: #333333;\">:<\/span> $scope.Address,\r\n__metadata <span style=\"color: #333333;\">:<\/span> { type<span style=\"color: #333333;\">:<\/span> itemType }\r\n};\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> postUrl <span style=\"color: #333333;\">=<\/span> baseUrl <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"_api\/web\/lists\/getbytitle(listName)\/items\"<\/span>; <span style=\"color: #888888;\">\/\/Replace baseUrl<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> config <span style=\"color: #333333;\">=<\/span> {\r\nheaders<span style=\"color: #333333;\">:<\/span> {\r\n<span style=\"background-color: #fff0f0;\">\"Content-Type\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"application\/json; odata=verbose\"<\/span>,\r\n<span style=\"background-color: #fff0f0;\">'Accept'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'application\/json; odata=verbose'<\/span>,\r\n<span style=\"background-color: #fff0f0;\">'X-RequestDigest'<\/span><span style=\"color: #333333;\">:<\/span> $scope.requestDigest\r\n}\r\n};\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"requestDigest\"<\/span>, $scope.requestDigest);\r\n\r\n$http.post (postUrl, JSON.stringify(requestData), config).then(<span style=\"color: #008800; font-weight: bold;\">function<\/span> (data) {\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"success\"<\/span>, data);\r\n$scope.submitSuccess<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">true<\/span>;\r\n\r\n},<span style=\"color: #008800; font-weight: bold;\">function<\/span>(error){\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"failed\"<\/span>, error);\r\n$scope.submitError<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">true<\/span>;\r\n});\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">return<\/span> deferred.promise;\r\n\r\n};\r\n<span style=\"color: #888888;\">\/\/END OF POST<\/span>\r\n\r\n<span style=\"color: #888888;\">\/\/button- generate new item in the list and run wf<\/span>\r\n$scope.submitCustomer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">function<\/span> (){\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"submitCustomer\"<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> promise1 <span style=\"color: #333333;\">=<\/span> getRequestDigest();\r\npromise1.then(<span style=\"color: #008800; font-weight: bold;\">function<\/span>(){ <span style=\"color: #888888;\">\/\/1st run the RequestDigest, then the AddCustomer<\/span>\r\nAddCustomer(<span style=\"background-color: #fff0f0;\">\"CustomerDetails\"<\/span>);\r\n});\r\n}\r\n<span style=\"color: #888888;\">\/\/button end<\/span>\r\n\r\n});\r\n\r\n<\/pre>\n<p><b>Listing 3:<\/b> Content of your index.js file<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"s5\">Step 5: Test your page!<\/h3>\n<p>Although it looks complicated, but it is really very simple. Trust me!<\/p>\n<p>Go ahead to view the page.<br \/>\nEnter some items in the form and submit and check that the items gets inserted. Then go the list in SharePoint to make sure the item actually got inserted.<\/p>\n<p>Let me know if you have challenges, I would be able to provide some guidance for you<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, good to see you here. Be sure that it really is not so difficult to post data from a form to a SharePoint List. &hellip; <\/p>\n","protected":false},"author":2,"featured_media":622,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[113],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":9,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":1205,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions\/1205"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/622"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}