Use Case
Many businesses need to automate folder creation in SharePoint based on dynamic inputs. A common requirement is to create folders under a given SharePoint site and a specified parent path. The parent path can be a direct drive or a nested structure like A/B
or A/B/C
, where A
is the drive name.
Using Microsoft Graph API 1.0, we can achieve this by sending requests to create folders dynamically based on an input JSON file containing:
- Site Name: The SharePoint site where folders should be created.
- Parent Path: The target location within the document library.
- Folder Name: The name of the new folder to be created.
Solution Steps
Step 1: Prerequisites
Before proceeding, ensure the following:
- Access to Microsoft Graph API 1.0
- Appropriate SharePoint permissions (
Files.ReadWrite.All
or Sites.Manage.All
)
- Azure AD App Registration with API permissions granted
Step 2: Retrieve the Site ID
First, fetch the Site ID using the site name:
API Request:
GET https://graph.microsoft.com/v1.0/sites/{tenant-name}.sharepoint.com:/sites/{site-name}
Authorization: Bearer {access-token}
Response:
{
"id": "site-id",
"name": "Your Site Name"
}
Step 3: Retrieve the Drive ID
Every SharePoint document library is associated with a drive. Retrieve the drive ID using:
API Request:
GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives
Authorization: Bearer {access-token}
Response:
{
"value": [
{
"id": "drive-id",
"name": "Documents"
}
]
}
Step 4: Create Folders Dynamically
To create a folder, use the parent path from the input JSON.
Scenario 1: Creating Folder in Root (A
)
If the parentPath
is A
, the request should be:
API Request:
PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children
Content-Type: application/json
Authorization: Bearer {access-token}
{
"name": "FolderName",
"folder": {},
"@microsoft.graph.conflictBehavior": "fail"
}
Scenario 2: Creating Folder in Subdirectories (A/B
or A/B/C
)
For nested folders, modify the request to include the path:
API Request:
PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{parentPath}/FolderName
Content-Type: application/json
Authorization: Bearer {access-token}
{
"folder": {},
"@microsoft.graph.conflictBehavior": "fail"
}
Step 5: Handling Errors and Edge Cases
- Folder Already Exists: Use
"@microsoft.graph.conflictBehavior": "replace"
if you want to replace an existing folder.
- Invalid Path: Ensure that the
parentPath
exists before attempting to create a folder.
- Permissions Issue: Validate that the API token has the correct scope for SharePoint folder creation.
Detailed steps with screenshots:
Create Rest connection using microsoft graph:
Integration flow:
Configure trigger:
Request:
{ "parentpath" : "DriveName/FolderName", "foldername" : "New Folder Name",
"site" : "Site Name" }
Response:
{ "status" : "Success", "url" : "<folder url returned from API response>", "Error_Message" : "<Error message information>" }
Get site id:
/sites/{tenant-name}.sharepoint.com:/sites/{site_name}
Get Drive Id:
/sites/{siteid}/drives
Add $filter





Xslt code:
<xsl:template name="tracking_var_3" xml:id="id_76"/>
<xsl:template match="/" xml:id="id_12">
<nstrgmpr:execute xml:id="id_31">
<nstrgmpr:TemplateParameters xml:id="id_32">
<ns32:siteId xml:id="id_32">
<xsl:value-of xml:id="id_33" select="$GetSiteID/nsmpr0:executeResponse/ns27:response-wrapper/ns27:id"/>
</ns32:siteId>
</nstrgmpr:TemplateParameters>
<nstrgmpr:QueryParameters xml:id="id_47">
<xsl:choose>
<xsl:when test="contains(/nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, "/")">
<ns32:_0x646c72_filter>
<xsl:value-of select="concat ("name eq '", substring-before (/nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, "/" ), "'" )"/>
</ns32:_0x646c72_filter>
</xsl:when>
<xsl:otherwise xml:id="id_61">
<ns32:_0x646c72_filter xml:id="id_62">
<xsl:value-of xml:id="id_63" select="concat ("name eq '", /nssrcmpr:execute/ns18:request-wrapper/ns18:parentpath, "'" )"/>
</ns32:_0x646c72_filter>
</xsl:otherwise>
</xsl:choose>
</nstrgmpr:QueryParameters>
</nstrgmpr:execute>
</xsl:template>
</xsl:stylesheet>
Create folder
/drives/{drive-id}/root{parentPath}/children
Request:
{
"name": "Test3",
"folder": {"name":"Test3"},
"@microsoft.graph.conflictBehavior": "rename"
}
Xslt code:<xsl:template match="/" xml:id="id 11">
trgmpr:execute xml:id="id 12">
<nstrgmpr: TemplateParameters xml:id="id_42">
<xsl:choose xml:id="id 119">
<xsl:when xml:id="id_120" test="contains (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, "/")"> <ns40:driveid xml:id="id_ 121">
<xsl:value-of xml:id="id_122" select="$GetDriveID/nsmpro: executeResponse/ns33: response-wrapper/ns33: value [ns33: name=substring-before (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, "/")]/ns33:id"/> </ns40: driveid>
</xsl:when>
<xsl:otherwise xml:id="id_123">
<ns40: drive id xml:id="id 124">
<xsl:value-of xml:id="id_125" select="$GetDriveID/nsmpre: executeResponse/ns33: response-wrapper/ns33: value [ns33: name=/nss rcmpr: execute/ns17: request-wrapper/ns17: parentpath]/ns33:id"/> </ns40: driveid> </xsl:otherwise>
</xsl:choose>
<xsl:choose xml:id="id_83">
<xsl:when xml:id="id_84" test="contains (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, "/")">
<ns40:directory xml:id="id_45">
<xsl:value-of xml:id="id_46" select="concat (":/", substring-after (/nssrcmpr: execute/ns17: request-wrapper/ns17: parentpath, "/" ), ":/")"/> </ns40:directory>
</xsl:when>
<xsl:otherwise xml:id="id_87">
<ns40:directory xml:id="id_45">
<xsl:value-of xml:id="id_89" select=""/""/>
</ns40:directory>
</xsl:otherwise>
</xsl:choose>
</nstrgmpr:TemplateParameters>
<ns39:request-wrapper xml:id="id_47">
<ns39: name xml:id="id_48">
<xsl:value-of xml:id="id_49" select="/nssrcmpr: execute/ns17: request-wrapper/ns17: foldername"/>
</ns39:name>
<ns39:folder xml:id="id_50">
<ns 39: name xml:id="id_51">
<xsl:value-of xml:id="id_52" select="/nssrcmpr: execute/ns17: request-wrapper/ns17: foldername"/>
</ns39:name>
</ns39: folder>
<ns39: 8x617472_microsoft.graph.conflictBehavior xml:id="id_53">
<xsl:value-of xml:id="id_54" select="" rename""/>
</ns 39: 0x617472_microsoft.graph.conflictBehavior>
</ns39:request-wrapper>
</nstrgmpr: execute>
</xsl:template>
Map response:
Xslt code:
<nstrgmpr:executeResponse xml:id="id_12">
<ns17: response-wrapper xml:id="id_52">
<xsl:choose>
<xsl:when test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status = 201">
<ns17:status>
<xsl:value-of select="" Success""/>
</ns17: status>
</xsl:when>
<xsl:otherwise xml:id="id_115">
<ns17:status xml:id="id 116">
<xsl:value-of xml:id="id_117" select="" Error""/>
</ns17:status>
</xsl:otherwise>
</xsl:choose>
<ns17:url xml:id="id 126">
<xsl:if xml:id="id_125" test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status = 201"> <xsl:value-of xml:id="id_127" select="$CreateSharePointFolder/nsmpr4: executeResponse/ns29: response-wrapper/ns29:webUrl"/>
</ns17:url>
</xsl:if>
<xsl:if xml:id="id_129" test="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.status != 201"> <ns17: Error Message xml:id="id_130"> <xsl:value-of xml:id="id_131" select="$CreateSharePointFolder/nsmpr4: executeResponse/ns28: ConnectivityProperties/ns28: RestAPI/ns28:http.response.reason"/> </ns17: Error_Message>
</xsl:if>
</ns17: response-wrapper>
</nstrgmpr: executeResponse>
</xsl:template>
Test:
Reference:
No comments:
Post a Comment