Recently, I’ve seen quite a few people asking about how to create folders in SharePoint Document Libraries using Power Automate.

Create folders in a SharePoint library

In my case I have a Document Library called Documents1 and I want to create a folder called New Folder

Using the Send an HTTP request to SharePoint action it is really a 5 minute task.

Create folders using Send an HTTP request to SharePoint action

Using the following REST API call I can get hold of the option to create a new folder.

_api/Web/GetFolderByServerRelativeUrl('Documents1')/Folders

Just supply the body that specifies the name of your folder.

{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/Documents1/New Folder'}

You could of course also do the same without the verbose option and nometadata instead. The body would then look something like this:

{'ServerRelativeUrl': '/Documents1/New Folder'}

Run the flow

Then when you run the flow you will see that the HTTP call is successful

output from the Send an HTTP request to SharePoint action

And my folder is created.

My folder has been created in SharePoint

Document Libraries in various Site Collections

For site collections (that aren’t the root site collection) or subsites you can use the following:

Create a folder in a site collection that isn't the root site collection

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

50 thoughts on “Create folders in a SharePoint document library using Power Automate”
  1. thanks for this Pieter. With your help i’ve got it working wonderfully in our environment. We have a complex file structure of folders that i was already creating (by creating a file) in order to have one folder per sales order (with these folders grouped as there’s lots). Now i’ve added in your flow code, tweaking it to use vairables so it’s easier for me to manage, so i now have numerous sub folders for each parent folder ready for the team to store all job related docs in a tidy way. This is going to be a great help. thanks !

  2. Thanks Pieter

    I’m a newbie… Could you introduce me to the rights allocation for the created folders ? Let’s say a main folder in the Document Library called Documents1 , with many subfolders, all accessible to Group A, one of them being accessible only to sSecurity group B ?

    Thanks,

    Gaston

  3. “message”: “Value cannot be null.\r\nclientRequestId: b0a3299f-fb4e-44d3-826f-6bef4c739715\r\nserviceRequestId: 44d1cd9e-10cc-8000-6e87-a13417104134”,

    I am getting this error. I would appreciate your help.

  4. Thank you for you prompt response. Here it is:

    Site Address
    Customer Bids – https://abc.sharepoint.com/sites/100010
    *Method
    POST
    *Uri
    _api/Web/GetFolderByServerRelativeUrl(‘BId%20Documents’)/Folders
    Headers

    Content-Type
    application/json; odata=verbose
    Accept
    application/json; odata=verbose
    Enter key

    Enter value

    Body
    { ‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘SpWeb.ServerRelativeUrl’: ‘/BId%20Documents/New Folder’}

      1. Now getting the following error. Thank you again for your help;

        {
        “error”: {
        “code”: 502,
        “source”: “flow-apim-europe-001-westeurope-01.azure-apim.net”,
        “clientRequestId”: “72737c0a-69f0-42d8-a945-60d7d3c56c1f”,
        “message”: “BadGateway”,
        “innerError”: {
        “message”: “(null) \”sites/100010/sites/100010/BId Documents/New Folder\” not found.\r\nclientRequestId: 72737c0a-69f0-42d8-a945-60d7d3c56c1f\r\nserviceRequestId: 13d6cd9e-8072-8000-7de0-5daf09e8af45″,
        “status”: 502,
        “source”: “https://abc.sharepoint.com/sites/100010/_api/Web/GetFolderByServerRelativeUrl(‘BId%20Documents’)/Folders”,
        “errors”: [
        “-2130247139”,
        “Microsoft.SharePoint.SPException”
        ]
        }
        }
        }

  5. Hello, I am trying to create a folder within a Document library also but is getting the following error:

    “message”: “Value cannot be null.”,
    “status”: 400,
    “source”: “https://XXX.sharepoint.com/sites/MainSite/Subsite/_api/Web/GetFolderByServerRelativeUrl(‘DocumentLibrary’)/Folders”,
    “errors”: [
    “-2147467261”,
    “System.ArgumentNullException”
    ]

    Thank you in advance!

      1. Thank you for the prompt reply. Do i add it in the URI or in the Body?

      2. Tried all three combinations off adding it to the GetFolderByServerRelativeUrl and ByServerRelativeUrl, Now getting a BadGateway error.

  6. Hello Pieter,
    I getting this error,

    “status”: 400,
    “message”: “Server relative urls must start with SPWeb.ServerRelativeUrl\r\nclientRequestId: ae4e610a-a527-4136-8b88-524b59f41b3c\r\nserviceRequestId: 1fa0d69e-80cd-8000-56fe-73dfc5582595”,

    appriciate your help.
    Keyvan

  7. This is interesting however I find it ridiculous that this level of effort is required to do something so common. Is makes me wonder why Microsoft does not directly support creating folders from PowerApps & Flow.

    1. Don’t forget that they can’t cover everything. There will always be some simple things that aren’t there. But imagine if you had to do all that flow does in code… it would take 10 times longer.

  8. I keep on getting this error

    {
    “status”: 400,
    “message”: “The expression \”Web/GetFolderByServerRelativeUrl(‘sites/corporate-files/Lists/WIP Testing)/Clients’\” is not valid.\r\nclientRequestId: cc97207c-54cb-4496-8a2f-87b371d1096a\r\nserviceRequestId: d2b7d79e-9004-0000-99d4-8f792c168144″,
    “source”: “https://mylifedigital.sharepoint.com/sites/corporate-files/_api/Web/GetFolderByServerRelativeUrl(‘sites/corporate-files/Lists/WIP%20Testing)/Clients’”,
    “errors”: [
    “-1”,
    “Microsoft.SharePoint.Client.InvalidClientQueryException”
    ]
    }

  9. Pieter, I have tried lots of combinations and am starting to go a little mad…. Any advice would be greatly received….

  10. {
    “message”: “Access denied. You do not have permission to perform this action or access this resource.\r\nclientRequestId: ce926637-4e56-4da4-abbe-53603caf7680\r\nserviceRequestId: 276ede9e-30b3-8000-86d8-bb9bfaca2eeb”,
    “status”: 403,
    “source”: “https://techmah.sharepoint.com/sites/SurgeonCompliance/_api/Web/GetFolderByServerRelativeUrl(‘sites/SurgeonCompliance/Shared%20Documents’)/Folders”,
    “errors”: [
    “-2147024891”,
    “System.UnauthorizedAccessException”
    ]
    }

      1. You would need to specify the parent folder name in GetFolderByServerRelativeUrl function and in the ServerRelativeUrl (in the Body) you will need to include the parent folder and the new folder name.

  11. Hi Pieter,

    Thanks for this post! Do you know how can I fix this error?
    {
    “error”: {
    “code”: 500,
    “source”: “flow-apim-msmanaged-na-northcentralus-01.azure-apim.net”,
    “clientRequestId”: “0086afe1-069c-4b92-a715-8bce776b1d26”,
    “message”: “BadGateway”,
    “innerError”: {
    “status”: 500,
    “message”: “The format of value ‘application/json;\nodata=verbose’ is invalid.\r\nclientRequestId: 0086afe1-069c-4b92-a715-8bce776b1d26”,
    “source”: “sharepointonline-wus.azconn-wus.p.azurewebsites.net”
    }
    }
    }

    Thanks!

  12. Hi Pieter,

    Am trying to create a folder in SharePoint under documents library but receiving below error

    {
    “status”: 404,
    “message”: “Cannot find resource for the request GetFolderByServerUrl.\r\nclientRequestId: f72fb757-fd57-41cc-af34-f89f6154bc71\r\nserviceRequestId: 4957399f-c059-a000-a014-18f25949fa32”,
    “source”: “https://xxxxxxx.sharepoint.com/sites/IDMACCESS/_api/web/GetFolderByServerUrl(‘/sites/IDMACCESS/shared%20Documents’)/Folders”,
    “errors”: [
    “-1”,
    “Microsoft.SharePoint.Client.ResourceNotFoundException”
    ]
    }

    could you please provide your suggestion

  13. I am trying to create a folder inside a folder but it is giving me this error
    {
    “error”: {
    “code”: “-1, Microsoft.SharePoint.Client.InvalidClientQueryException”,
    “message”: {
    “lang”: “en-US”,
    “value”: “Invalid JSON. Unexpected end of input reached while processing a JSON string value.”
    }
    }
    }

    I am giving the code I tried below
    trial_1 = document library
    folderC =parent folder
    idea1= subfolder

    POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl(‘trial_1/folderC’)/Folders

    headers:
    Authorization: “Bearer ” + accessToken
    Accept: “application/json;odata=verbose”
    Content-Type: “application/json”
    Content-Length: {length of request body as integer}
    X-RequestDigest: “{form_digest_value}”

    and body:
    {
    “__metadata”: {
    “type”: “SP.Folder”
    },
    “ServerRelativeUrl”: “trial_1/folderC/idea1”
    }
    Please help me with this

    1. What is your site url? You will need to make sure that the/sites/… part is included there.

      Also please check the quotes. Quite often single/double quotes corrupt when copied from blog posts.

      1. my site url ishttps://teamcodered.sharepoint.com/sites/SEProductions/_api/web/GetFolderByServerRelativeUrl(‘trial_1/folderC‘)/Folders
        I tried chnaging the quotes from single to double and vice vera but the error is still same

  14. I did the changes you suggested

    POST https://teamcodered.sharepoint.com/sites/SEProductions/_api/web/GetFolderByServerRelativeUrl('/sites/SEProductions/trial_1/folderC‘)/Folders

    Header:
    Accept:application/json;odata=verbose
    Content-Type:application/json;odata=verbose
    Content-Length:95
    I have added authorization and X-RequestDigest as well

    Body:
    {
    “__metadata”: {
    “type”: “SP.Folder”
    },
    “ServerRelativeUrl”: “/sites/SEProductions/trial_1/folderC/idea1”
    }

    still getting the same error.
    Am I missing something?

  15. Thank you for the reply

    As told by you this is
    POST https://teamcodered.sharepoint.com/sites/SEProductions/_api/web/GetFolderByServerRelativeUrl('/sites/SEProductions/trial_1/folderC‘)/Folders

    headers:
    Accept:application/json;odata=verbose
    Content-Type:application/json;odata=verbose
    Content-Length:95
    I have added authorization and X-RequestDigest as well

    And body
    {
    “__metadata”: {
    “type”: “SP.Folder”
    },
    “ServerRelativeUrl”: “/sites/SEProductions/trial_1/folderC/idea1”
    }

    But still it gives the same error
    Please tell if I am missing/doing it wrong

  16. Getting this error can you assist?
    “error”: {
    “code”: 502,
    “source”: “flow-apim-msmanaged-na-centralus-01.azure-apim.net”,
    “clientRequestId”: “979885fd-24a5-43bc-868a-c9d8d1aa46d3”,
    “message”: “BadGateway”,
    “innerError”: {
    “status”: 500,
    “message”: “The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)\r\nclientRequestId: 979885fd-24a5-43bc-868a-c9d8d1aa46d3\r\nserviceRequestId: 979885fd-24a5-43bc-868a-c9d8d1aa46d3”
    }
    }
    }

  17. Hi Pieter,
    I am having this error message: “(null) \”sites/|SITENAME|/|DocumentLibrary|/00300269975\” not found.”
    My request is as follows:
    POST https://|TENANT|.sharepoint.com/sites/|SITENAME|/_api/Web/GetFolderByServerRelativeUrl(‘/sites/|SITENAME|/|DocumentLibrary|’)/Folders HTTP/1.1
    Content-Type: application/json;odata=verbose
    Accept: application/json;odata=verbose
    {“__metadata”:{“type”:”SP.Folder”},”ServerRelativeUrl”:”/sites/|SITENAME|/|DocumentLibrary/00300269975″}
    I am quite lost. The weird request is because I am trying to use batch updates, assume sitename and document library are correct. Which could be the error?

    1. Hi Fabian, did you replace the actual values with those tokens like |TENANT|?

      I looks like the site or document library is not found. You could use the url in a browser and see if it gets any data. This to verify where things go wrong.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.