Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Run Javascript code to Create Folders when Item is added to a List -REST SharePoint

$
0
0

Hi i have a simple code to create folders in a sharePoint Library. But i am trying to adapt the code to only create a folder when an item has been added to a specific SharePoint list.

So as soon as a user clicks save button on the newform page on the list. the javascript code runs picking the item title as foldername to be created in the specified library.

Any help will be appreciated

thanks

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" type="text/javascript"></script><script type="text/javascript">


//list users will add items into
var getRecentItemCountries = function () {

          var requestURL =  _spPageContextInfo.webAbsoluteUrl +"/_api/Web/Lists/getByTitle('State')/items?$select=Title";

            $.ajax({
                url: requestURL,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    console.log(data);
                                        //success(data);

                },
                error: function (data) {
                    //failure(data);
                }
            });
        }

        var createfolder = function (ListItemName) {

        var documentLibraryName = 'AppLogos';
        var ListItemName = 'test';



         var fileCollectionEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName + "')/folders/add(url=\'" + ListItemName + "\')";

            $.ajax({
                url: fileCollectionEndpoint,
                method: "POST",
                //body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/AppLogos/test'},
                headers: { "X-RequestDigest":  window.top.$("#__REQUESTDIGEST").val(), "Accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"  },
                success: function (data) {
                    console.log(data);
                                        //success(data);
                                        console.log("Folder creted succesfully");
                                        //alert(‘Folder created successfully’);

                },
                error: function (data) {
                    //failure(data);
                }
            });
        }

        $(document).ready(function (){

            getCountries();

            createfolder();
        })




</script>



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>