Gabriele Del Giovine Blog (Z80 powered brain...)


Un blog su quel che faccio (o cerco di fare...)
Calendario
novembre 2024
lmmgvsd
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
Archivio Posts
Anno 2011

Anno 2010

Anno 2009

Anno 2008

Anno 2007

Anno 2006

Anno 2005
Links
    Mappa
    Blogs Amici
      Statistiche
      • Views Home Page: 304.899
      • Views Posts: 372.249
      • Views Gallerie: 256
      • n° Posts: 163
      • n° Commenti: 198

      SharePoint Dev Therapy: pillola 2 - CreateFolder

      Seconda pillola della terapia. Oggi è la volta di CreateFolder.
      Questa funzione consente la creazione di una intera struttura di folders all'interno di una lista o di una raccolta documenti.

      CreateFolder richiede due parametri in entrata e restituisce un oggetto di classe SPFolder

      FolderURL = il percorso delle folder da creare nella forma "Folder1/Folder2/Folder3" ed è relativo alla root della lista in cui si vuole creare la struttura di folder 

      SPList = è la lista in cui creare la struttura di folders

      La versione VB.NET

      Public Function CreateFolder(ByVal FolderUrl As String, ByVal SPList As Microsoft.SharePoint.SPList) As SPFolder
      Try
      Return Me._CreateFolder(SPList, FolderUrl)
      Catch ex As Exception
      Return Nothing
      End Try
      End Function

      Private Function _CreateFolder(ByVal targetList As SPList, ByVal folderUrl As String) As SPFolder
      If String.IsNullOrEmpty(folderUrl) Then
      Return targetList.RootFolder
      End If
      Dim folder As SPFolder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url & "/" & folderUrl)
      If (Not folder.Exists) Then
      If (Not targetList.EnableFolderCreation) Then
      targetList.EnableFolderCreation = True
      targetList.Update()
      End If
      Dim folders() As String = folderUrl.Trim("/"c).Split("/"c)
      Dim folderPath As String = String.Empty
      For i As Integer = 0 To folders.Length - 1
      Me.GetValidFileOrFolderName(folders(i), "_")
      folderPath &=
      "/" & folders(i)
      folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url & folderPath)
      If (Not folder.Exists) Then
      Dim newFolder As SPListItem = targetList.Items.Add("", SPFileSystemObjectType.Folder, folderPath.Trim("/"c))
      newFolder.Update()
      folder = newFolder.Folder
      End If
      Next i
      End If
      If folder Is Nothing Then
      Throw New SPException(String.Format("The folder '{0}' could not be found.", folderUrl))
      End If
      Return folder
      End Function

      Private Function GetValidFileOrFolderName(ByRef path As String, Optional ByVal replacementChar As Char = "_") As Boolean
      Dim illegalPathChars As New RegularExpressions.Regex("^\.|[\x00-\x1F,\x7B-\x9F,"",#,%,&,*,/,:,<,>,?,\\]+|(\.\.)+|\.$", RegularExpressions.RegexOptions.Compiled)
      If path Is Nothing Then
      Return False
      End If
      path = illegalPathChars.Replace(System.Web.HttpUtility.UrlDecode(path.Trim()), replacementChar.ToString())
      If path.Length > 128 Then
      path = path.Substring(0, 128)
      Return GetValidFileOrFolderName(path, replacementChar)
      End If
      Return path.Length > 0
      End Function

      E la versione C#

      public SPFolder CreateFolder(string FolderUrl, Microsoft.SharePoint.SPList SPList)
      {
      try
      {
      return this._CreateFolder(SPList, FolderUrl);
      }
      catch (Exception ex)
      {
      return null;
      }
      }

      private SPFolder _CreateFolder(SPList targetList, string folderUrl)
      {
      if (string.IsNullOrEmpty(folderUrl))
      {
      return targetList.RootFolder;
      }
      SPFolder folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url +
      "/" + folderUrl);
      if (!folder.Exists)
      {
      if (!targetList.EnableFolderCreation)
      {
      targetList.EnableFolderCreation =
      true;
      targetList.Update();
      }
      string[] folders = folderUrl.Trim('/').Split('/');
      string folderPath = string.Empty;
      for (int i = 0; i < folders.Length; i++)
      {
      string temppath1 = folders[i];
      this.GetValidFileOrFolderName(ref temppath1, '_');
      folders[i] = temppath1;
      folderPath +=
      "/" + folders[i];
      folder = targetList.ParentWeb.GetFolder(targetList.RootFolder.Url + folderPath);
      if (!folder.Exists)
      {
      SPListItem newFolder = targetList.Items.Add(
      "", SPFileSystemObjectType.Folder, folderPath.Trim('/'));
      newFolder.Update();
      folder = newFolder.Folder;
      }
      }
      }
      if (folder == null)
      {
      throw new SPException(string.Format("The folder '{0}' could not be found.", folderUrl));
      }
      return folder;
      }

      private bool GetValidFileOrFolderName(ref string path)
      {
      return GetValidFileOrFolderName(ref path, '_');
      }

      private bool GetValidFileOrFolderName(ref string path, char replacementChar)
      {
      RegularExpressions.Regex illegalPathChars =
      new RegularExpressions.Regex("^\\.|[\\x00-\\x1F,\\x7B-\\x9F,\",#,%,&,*,/,:,<,>,?,\\\\]+|(\\.\\.)+|\\.$", RegularExpressions.RegexOptions.Compiled);
      if (path == null)
      {
      return false;
      }
      path = illegalPathChars.Replace(System.Web.HttpUtility.UrlDecode(path.Trim()), replacementChar.ToString());
      if (path.Length > 128)
      {
      path = path.Substring(0, 128);
      return GetValidFileOrFolderName(ref path, replacementChar);
      }
      return path.Length > 0;
      }
      }

      Categoria: Sharepoint
      martedì, 12 ott 2010 Ore. 21.16
      Copyright © 2002-2007 - Blogs 2.0
      dotNetHell.it | Home Page Blogs
      ASP.NET 2.0 Windows 2003