Create Folder in SharePoint using Web Services

Introduction:

In this article I am going to explain you that How to create folder using sharepoint web service.
To do this we will be using DWS.asmx web service and “CreateFolder” web method.
“CreateFolder” Creates a subfolder in the document library of the current Document Workspace site.

Syntex :-

1
public string CreateFolder (string url)

URL. The proposed site-based URL of the folder to create; for example, “Shared Documents/folder_name“.

If you want to create folder “IT” inside “Shared Documents” library then path will be “Shared Documents/IT”
If you wish to create “HR” under “IT” folder Then you need to create 2 time folder
1st it will be “Shared Documents/IT” and
2nd it will be for “Shared Documents/IT/HR”.

The Sample Code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/// <summary>
/// create folder
/// If path is "Shared Documents/folder1/folder2"
/// then this function should be called with string array 
/// which will contains value as "folder1" and "folder2"
/// </summary>
/// <param name="file">string of folder.</param> 
public bool CreateFolder(string[] file)
{
	//note: settings.DocumentLibrary is a string which contains "Shared Documents"
 
	DWS.Dws dwsReference = new AvinashNamespcae.DWS.Dws();
	bool isFolderCreated = false;
	try
	{
		dwsReference.Credentials = credentials;
		dwsReference.Url = settings.SiteUri.ToString().ToString().TrimEnd('/') + "/_vti_bin/DWS.asmx";
		//create folder for 1st string which is "folder1". so path would be "Shared Documents/folder1"
		dwsReference.CreateFolder(settings.DocumentLibrary + "/" + file[0]);
		//create folder for 2nd string which is "folder2". so path would be "Shared Documents/folder1/folder2"
		dwsReference.CreateFolder(settings.DocumentLibrary + "/" + file[0] + "/" + file[1]);
		isFolderCreated = true;
	}
	catch (Exception ex)
	{
		// handle exception
	}
	finally
	{
		if (dwsReference != null)
			dwsReference.Dispose();
	}
	return isFolderCreated;
}


References:-

msdn link: http://msdn.microsoft.com/en-us/library/ms774480(v=office.12).aspx

Thanks!
Avinash

calendarMarch 20, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , ,  · Posted in: MOSS, SharePoint, Web Service

Leave a Reply

Spam Protection: , required

myworldmaps infoyen