EDMSRPath is the full path of the file (eg. http://url/ListName/blabla/be/bo/x.pdf)
WSPath is the url of the document library (eg. http://url)
char[] separator = ("/").ToCharArray();
string[] folder = EDMSRPath.Substring(EDMSRPath.IndexOf("ListName") + ("ListName").Length + 1).Split(separator);
//check first folder
// This method is separated to avoid getting the subfolders for all top folders, as it will have too much recurrence.
getFolders(string.Empty, ref dt);
string firstFolder = WSPath + "/ListName";
if (!checkFolderExist(dt, firstFolder + "/" + folder[0]))
{
CreateFolder(firstFolder, folder[0]);
}
firstFolder += "/" + folder[0];
//check the subfolders required
dt = new DataTable();
if (folder.Length > 1)
{
getFolders(folder[0], ref dt, true);
string subfolder = firstFolder;
for (int i = 1; i < folder.Length; i++)
{
if (!checkFolderExist(dt, subfolder + "/" + folder[i]))
{
CreateFolder(subfolder, folder[i]);
}
subfolder += "/" + folder[i];
}
}
Now the actual functionsCheckFolderExist - to check if the folder exists
private static bool checkFolderExist(DataTable dt, string folderpath)
{
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString() == folderpath || dt.Rows[i][0].ToString() == folderpath.Replace(" ","%20"))
{
return true;
}
}
}
return false;
}
And function to create the folder private static string CreateFolder(string path, string foldername)
{
string res = string.Empty;
try
{
using (ListWS.Lists listService = new ListWS.Lists())
{
listService.Url = ListURL;
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Credentials = new System.Net.NetworkCredential(username, password);
XmlDocument doc = new XmlDocument();
string xmlCommand = "<Batch OnError='Continue' RootFolder='" + path +
"'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" +
foldername + "</Field></Method></Batch>";
doc.LoadXml(xmlCommand);
XmlNode batchNode = doc.SelectSingleNode("//Batch");
XmlNode node1 = listService.UpdateListItems("ListName", batchNode);
if ((node1 != null) && (node1.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (node1.FirstChild.FirstChild.InnerText == SUCCESS))
{
// success
res = "";
}
else
{
res = "Create new folder failed for: " + foldername + ". Error Details: " + node1.InnerText;
}
}
}
catch (Exception ex)
{
if (ex.Message != "Column name 'ID' is defined for different mapping types.")
throw ex;
}
return res;
}
No comments:
Post a Comment