Frage

I have read this question and answer, but the REST call in that question is attempting to grab a folder by GetFolderByServerRelativeUrl and then add a file directly to it, so clearly if the folder is not already there, you won't be able to get it.

But what if I constructed a URL like this:

/_api/web/lists/getbytitle('My Library')/rootfolder/files/add(url='folder1/folder2/filename.docx')

Would SharePoint create folder1 and folder2 on the way to adding the file?

Or, what about if, as in the answer linked to, I had to make a separate call to create the folder, but when creating the folder I actually want to create several nested folders. So again, imagine that there's only the root folder of the document library, and I did

{
    '__metadata': {
        'type': 'SP.Folder'
    },
    'ServerRelativeUrl': '/sites/MySite/MyDocLib/folder1/folder2/folder3'
}

Would SharePoint automatically create all three subfolders?

War es hilfreich?

Lösung

Would SharePoint automatically create all three subfolders?

As per my analysis SharePoint REST API will not create all three subfolders automatically.

If folder1 is not exist in the document library then it will throw Not Found error.

Below is a code I am using for creating folders in document library:

<script type="text/javascript">
    function createFolder(folderPath, newFolderName) {
        var folderEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + folderPath + "')/folders/add(url=\'" + newFolderName + "\')";

        $.ajax({
            url: folderEndpoint,
            type: "POST",
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("Folder created successfully");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
</script>

OR:

function createFolderWithPayload() {
        var folderEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders";
        var folderPayload = {
            "__metadata": {
                "type": "SP.Folder"
            },
            "ServerRelativeUrl": "/sites/SiteName/Shared Documents/folder1"
        };

        $.ajax({
            url: folderEndpoint,
            type: "POST",
            data: JSON.stringify(folderPayload),
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("Folder created successfully");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }

Considering I have only a document library and there is no folder in it.

Scenario 1:

When I tried:

createFolder('/sites/SiteName/Shared Documents', 'folder1')

It will create a folder1 as it does not exist in document library.

Scenario 2:

When I tried:

createFolder('/sites/SiteName/Shared Documents/folder2', 'folder3')

Throws an error that folder2 not found.

Would SharePoint create folder1 and folder2 on the way to adding the file?

Above scenario's are same with adding file.

Below is a code I am using for creating files in document library:

function createFile(folderPath, newFileName) {
        var fileEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + folderPath + "')/Files/add(url=\'" + newFileName + "\',overwrite=true)";

        $.ajax({
            url: fileEndpoint,
            type: "POST",
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("Folder created successfully");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }

Considering I have only a document library and there is no folder in it.

Scenario 1:

When I tried:

createFile('/sites/SiteName/Shared Documents', 'rest.txt')

It will create a rest.txt as it does not exist in document library.

Scenario 2:

When I tried:

createFile('/sites/SiteName/Shared Documents/folder1', 'rest.txt')

Throws an error that File not found.

Microsoft Documentation: Working with folders and files with REST

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top