1

I have used SPServices for a long time, both creating, deleting and getting list items from my sharepoint sites.

Now I got a new site, and in this site the previous developer has created lists with folders.

The structure for all 4 lists is like this: (This is just an example)

  • Folder: Regional
    • Folder: Spain
      • Items....
  • Folder: International
    • Folder: Europe
      • Items....
  • Folder: Local
    • Folder: Department
      • Items....

In this exmaple: http://sympmarc.com/2011/03/28/listing-folders-in-a-sharepoint-list-or-library-with-spservices/ the guy shows how to display the folder structure when getting the list items.

But when using "UpdateListItems" how do I create folders and then add items to that folder?

I can use both SPServices.js and SP.js so I have no other requirement on how to do it, besides I need to be able to do it from Javascript. I'm only assigned the role of Web admin, so I do not have access to PowerShell, ServerSide code or anything like that.

1 Answer 1

0

You can use OOTB file upload facility for SharePoint library using below url:

https://sharepoint.contoso.com/sites/YourSiteUrl/_layouts/15/Upload.aspx?List={Destination LIST GUID}&RootFolder="ServerRelativeURL of destination folder"

Note: Upper mentioned url is working in SharePoint 2013. For SharePOint 2010, just open the library in IE browser --> click on "upload document" button, --> popup will be opened -->right click on that and click on properties and you will get proper url. It might be just 14 hive change in the URL.


Create Folder using JSOM:

<div><button onclick=" createFolder ()">Click here to AddaFieldtoList</button></div>  

  <div id="displayDiv"></div>  
  <script type="text/javascript">   
  function createFolder() {  
    var clientContext;  
    var oWebsite;  
    var oList;  
    var itemCreateInfo;   

    clientContext = new SP.ClientContext.get_current();  
    oWebsite = clientContext.get_web();  
    oList = oWebsite.get_lists().getByTitle("Shared Documents");  

    itemCreateInfo = new SP.ListItemCreationInformation();  
    itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);  
    itemCreateInfo.set_leafName("My new folder!");  
    this.oListItem = oList.addItem(itemCreateInfo);  
    this.oListItem.update();  

    clientContext.load(this.oListItem);  
    clientContext.executeQueryAsync(  
        Function.createDelegate(this, successHandler),  
        Function.createDelegate(this, errorHandler)  
    );  

    function successHandler() {  
        alert("New folder is created successfully!");  
    }  

    function errorHandler() {  alert("Error: "+arguments[1].get_message());
    }  
}  
</script>  

Add file to SharePoint Library using REST api:

function sendFileToServer(filename, file) 
{
    var reader = new FileReader();
    reader.onloadend = function(evt) 
    {
        if (evt.target.readyState == FileReader.DONE) 
        {
            var folder = localStorage.getItem('FolderName');

            var serverRelativeUrlToFolder = 'LibraryName/' + folder;
            var buffer = evt.target.result;
            //Upload to Specific folder 
            var docLibraryEndpoint = String.format( "{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
                            "/add(overwrite=true,url='{2}')",
                            _spPageContextInfo.webAbsoluteUrl, serverRelativeUrlToFolder, filename
                            );
            $.ajax({
                url: docLibraryEndpoint,
                type: "POST",
                data: buffer,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success:function()
                {
                    alert("Document uploaded successfully");
                },
                error:function()
                {
                    alert("Failed to upload the document");
                }
            });
        }
    };
    reader.readAsArrayBuffer(file);
}

References:

5
  • Thanks for the "file" example as well, but its just simple items that I want to add to the specific folder :) Eg: How do I add an item to "Europe" from my example. Remember this needs to work in SP2010. Your references are for later versions of SharePoint :) Commented Feb 23, 2017 at 10:07
  • I have updated my answer. Commented Feb 23, 2017 at 11:31
  • Its super!! :) i'm sorry if I have been unclear. When I say "items" I mean in a custom list. Not a document library. :) Commented Feb 23, 2017 at 19:48
  • How can you create folders in the custom list and add items in the custom list? Commented Feb 24, 2017 at 5:05
  • yes - Exactly! I'm working with a custom list, which has folders, and in my project, I need to be able to extract all data from that list, then go to another SharePoint site and insert all the same data with the same structure in a new list. I will, of course, create the list on the new site and setup fields :) It works perfectly the way I do it today, with SPServices, but since this list contains folders, it does not work anymore. Commented Feb 24, 2017 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.