How To Create Folders And Subfolders In SharePoint C#

Are you looking to create folders and subfolders in SharePoint using C#? This section will walk you through the process, whether you prefer the SharePoint interface or programmatically using the SharePoint API.

To create folders and subfolders in SharePoint using C#, you can follow the steps outlined in the code snippets provided. First, you need to establish a connection to the SharePoint site using the appropriate credentials. Then, you can use the SharePoint API to create folders and subfolders in the desired SharePoint list or library.

One approach is to navigate to the SharePoint list or library and use the “New Folder” button on the Ribbon to create a folder. If the button is disabled, you can enable it by going to List settings and setting the “Make New Folder” command available option to “Yes”.

To create folders programmatically, you can use the SharePoint API in C#. In the provided code snippet, the SPSite and SPWeb objects are used to access the SharePoint site and web. The SPList object is used to retrieve the desired list, and the AddItem method is used to create a new folder. The Update method is then called to save the changes.

Another code snippet demonstrates how to create a subfolder within a folder. This involves retrieving the parent folder using the SPFolderCollection and accessing its SubFolders property. The Add method is then used to create a new subfolder within the parent folder.

Finally, there is a mention of using PowerShell to create a folder in SharePoint’s document library. However, the actual PowerShell code is not provided in the given text.

In summary, to create folders and subfolders in SharePoint using C#, you can either use the SharePoint interface or programmatically using the SharePoint API.

Manual Approach: Creating Folders in SharePoint

To create folders in SharePoint using the user interface, follow these steps to enable and utilize the “New Folder” button on the SharePoint Ribbon.

  1. Navigate to the SharePoint list or library where you want to create the folder.
  2. Click on the “List” or “Library” tab on the SharePoint Ribbon.
  3. Locate and click on the “List Settings” or “Library Settings” button.
  4. Scroll down to the “General Settings” section and click on the “Advanced settings” link.
  5. Under the “Folders” section, select “Yes” for the “Make New Folder command available” option.
  6. Scroll down and click on the “OK” button to save the changes.
  7. Now, you should see the “New Folder” button enabled on the SharePoint Ribbon.
  8. Click on the “New Folder” button, enter a name for the folder, and press enter to create it.

This manual approach allows you to create folders in SharePoint without writing any code. It is a straightforward method that can be used by any user with the necessary permissions.

Example: Creating a New Folder in SharePoint

“To create a new folder in SharePoint, navigate to the desired list or library, enable the ‘New Folder’ button on the SharePoint Ribbon, and click on it. Enter a name for the folder and press enter. The folder will be created in the SharePoint list or library.”

Steps Description
1 Navigate to the desired list or library.
2 Click on the “List” or “Library” tab on the SharePoint Ribbon.
3 Click on the “List Settings” or “Library Settings” button.
4 Scroll down to the “General Settings” section and click on the “Advanced settings” link.
5 Under the “Folders” section, select “Yes” for the “Make New Folder command available” option.
6 Scroll down and click on the “OK” button to save the changes.
7 Click on the “New Folder” button on the SharePoint Ribbon.
8 Enter a name for the folder and press enter.

Programmatic Approach: Creating Folders and Subfolders with C#

If you prefer a programmatic approach to creating folders and subfolders in SharePoint, you can utilize the SharePoint API in C# to achieve this. The code snippets provided in this section will guide you through the process.

To begin, you need to establish a connection to the SharePoint site using the appropriate credentials. Once connected, you can access the SharePoint API to create folders and subfolders within the desired SharePoint list or library.

In the code snippet below, we demonstrate how to create a new folder using the SharePoint API:

SPList list = web.Lists["YourListName"];
string folderName = "New Folder Name";
SPListItem newFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
newFolder.Update();

If you wish to create a subfolder within an existing folder, you can use the following code:

SPFolder parentFolder = list.RootFolder.SubFolders["Parent Folder Name"];
string subfolderName = "New Subfolder Name";
SPListItem newSubfolder = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, subfolderName);
newSubfolder.Update();

By utilizing the SharePoint API in C#, you can programmatically create folders and subfolders in SharePoint, providing a flexible and efficient approach to organizing your SharePoint content.

Summary

In summary, the programmatic approach to creating folders and subfolders in SharePoint using C# allows for greater control and automation. By leveraging the SharePoint API, you can establish a connection to the SharePoint site and use code snippets to create folders and subfolders in the desired list or library. Whether you need to create a single folder or multiple subfolders, the SharePoint API in C# provides the necessary tools to streamline the process.

Creating Folders in SharePoint Using PowerShell

In addition to the manual and programmatic approaches discussed earlier, it is worth noting that PowerShell can also be used to create folders in SharePoint’s document library. However, the specifics of the PowerShell code are beyond the scope of this article.

To create folders in SharePoint using PowerShell, you would typically start by establishing a connection to the SharePoint site and web using the SharePointOnlineCredentials class. Once connected, you can use the Add-PnPFolder cmdlet to create a new folder in the desired document library.

For example, the following PowerShell code can be used to create a folder named “New Folder” in the document library “Documents”:

$credentials = Get-Credential
Connect-PnPOnline -Url "https://your-sharepoint-site-url" -Credentials $credentials

Add-PnPFolder -Name "New Folder" -Folder "Documents"

Running this PowerShell script will create a new folder named “New Folder” within the “Documents” document library in SharePoint.

Keep in mind that PowerShell provides a convenient and efficient way to automate folder creation in SharePoint. However, it is recommended to test the code in a development or test environment before running it in a production environment to ensure it meets your specific requirements.

Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.