Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

Difference between revisions of "SPO Programmazione BO"

From Aino Wiki

Jump to: navigation, search
(Mappa e Link)
 
(No difference)

Latest revision as of 14:19, 27 May 2024

SharePoint Framework

SharePoint Online Client Components SDK (usato per abilitare lo sviluppo con SharePoint OnLine)
You can use the SharePoint client object model (CSOM =ClientSideObjectModel) to retrieve, update, and manage data in SharePoint.
SharePoint makes the CSOM available in several forms:

  • .NET Framework redistributable assemblies
  • .NET Standard redistributable assemblies
  • JavaScript library (JSOM)
  • REST/OData endpoints

Microsoft scrive: "It's recommended to use the CSOM for .NET Standard version for all your SharePoint Online CSOM developments". Ma occorre avere l'accesso al portale Azure per poter registrare l'applicazione questo dipende dal tenant in cui si opera. Questa strada l'ho scartata perché il mio tenant non mi da l'accesso pertanto uso: CSOM .NET Framework.

docs.microsoft.com

Alternative a CSOM .NET Framework

Usando PnP libraries ... da approfondire...

Esempio Copia files SharePoint vs FileSystem

Usando l'approccio con CSOM .NET Framework che è il più "comodo" non richiedendo autorizzazioni dal portale Azure, segue esempio di Applicazione in Windows Form (framework 4.5.2).
Attenzione, installare le librerie usando NuGet Microsoft.SherePoint.Client e Microsoft.SharePointOnline.CSOM.

NuGet MicrosoftSharePointClient.png

Supponendo di organizzare la solution in un progetto Desktop app in Windows Form e una libreria Helper di SharePoint Online.
Segue l'App.config in cui parametrizzare le informazioni opzionali:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="SPO_RootURL" value="https://pizza.sharepoint.com" />
    <add key="SPO_SiteName" value="SitoDiTest" />
    <add key="SPO_SiteLibrary" value="Documents" />
    <add key="SPO_Access_EmailUserName" value="giuseppe.aino@pizza.it" />
    <add key="SPO_Access_Password" value="???????" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

Progetto Windows Form

using SP = Microsoft.SharePoint.Client;
using System;
using System.Security;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using VF_SPO_Library;
using VF_Common;
 
//DOC: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code
//SPO_Helper
//VF_SPO_Library
 
namespace SPSynchro
{
    public partial class FrmMain : System.Windows.Forms.Form
    {
        #region Attributi privati
        private static string m_SPO_RootURL
        {
            get
            {
                return ConfigurationManager.AppSettings["SPO_RootURL"].ToString(); ;
            }
        }
        private static string m_SPO_SiteName
        {
            get
            {
                return ConfigurationManager.AppSettings["SPO_SiteName"].ToString(); ;
            }
        }
        private static string m_SPO_SiteLibrary
        {
            get
            {
                return ConfigurationManager.AppSettings["SPO_SiteLibrary"].ToString(); ;
            }
        }
        private static string m_SPO_Access_EmailUserName
        {
            get
            {
                return ConfigurationManager.AppSettings["SPO_Access_EmailUserName"].ToString(); ;
            }
        }
        private static string m_SPO_Access_Password
        {
            get
            {
                return ConfigurationManager.AppSettings["SPO_Access_Password"].ToString(); ;
            }
        }
        private const string c_AppVersion = "1.00";
        #endregion
 
        private SPO_Helper m_SPO;
 
        public FrmMain()
        {
            InitializeComponent();
        }
 
        #region UI Event Handler
        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                Txt_SPSiteTenant.Text = m_SPO_RootURL;          // https://pizza.sharepoint.com
                Txt_SPSiteName.Text = m_SPO_SiteName;           // CC-CVReportingGateway
                Txt_SPFFolderLibrary.Text = m_SPO_SiteLibrary;  // ResourceExchange
 
                string mainURL = string.Format("{0}/sites/{1}", m_SPO_RootURL, m_SPO_SiteName);
                TxtUserName.Text = m_SPO_Access_EmailUserName;
                TxtPassword.Text = m_SPO_Access_Password;
 
                m_SPO = new SPO_Helper(mainURL, m_SPO_Access_EmailUserName
                                              , Security.ToSecureStr(m_SPO_Access_Password));
                RcTxtOutput.Text += string.Format("{0}\r\n", m_SPO.SharePointMessageOut);
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Form Load Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnDownload_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                string docLibrary = TxtSP_URL_FirstFolder.Text;   //es:   /sites/CC-CVReportingGateway/ResourceExchange
                string subFoldersCSV = TxtSPSubFolder.Text;
                string destFolderFS = TxtDestFileSystem.Text;   //es:   D:\Tmp\
 
                m_SPO.DownloadFilesFromSharePoint(docLibrary, subFoldersCSV, destFolderFS
                                                , out strMsgOutcome);
                RcTxtOutput.Text += strMsgOutcome;
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnUpload_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                string siteUrl = TxtMainURL.Text;                       //Es.: https://pizza.sharepoint.com/sites/CC-CVReportingGateway
                string docLibrary = Txt_SPFFolderLibrary.Text;          //FirstFolder
                string clientSubFolder = TxtSPSubFolder.Text;
                string fileNameLocalFullPath = TxtSrcFileSystem.Text; // D:\Tmp\Img\file.ext
 
                m_SPO.UploadFileToSharePoint(siteUrl, docLibrary, clientSubFolder
                                    , fileNameLocalFullPath, out strMsgOutcome);
 
                RcTxtOutput.Text += string.Format("File {0} caricato in: {1}\r\n{2}\r\n\r\n"
                                    , fileNameLocalFullPath, clientSubFolder, strMsgOutcome);
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
 
        private void BtnDeleteSPOFile_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                string docLibrary = TxtSP_URL_FirstFolder.Text;
                string subFoldersCSV = TxtSPSubFolder.Text;
                string fileName = TxtFileName.Text;
 
                if (m_SPO.DeleteFileFromSharePoint(docLibrary, subFoldersCSV, fileName
                                                , out strMsgOutcome))
                {
                    RcTxtOutput.Text += string.Format("CANCELLATO File {0} caricato in: {1}\r\n\r\n"
                                        , fileName, subFoldersCSV);
                }
                else
                {
                    RcTxtOutput.Text += string.Format("Cancellazione FALLITa o File {0} non trovato.\r\n\r\n"
                                        , fileName);
                }                
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnCreateDirectory_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                string docLibrary = Txt_SPFFolderLibrary.Text;
                string directoryName = TxtDirectoryToCreateOrRemove.Text;
                string execOutcome = m_SPO.CreateFolder(docLibrary, directoryName);
 
                RcTxtOutput.Text += string.Format("{0}\r\n\r\n", execOutcome);
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnDeleteDirectory_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                string targetFolderFullPath = string.Format("{0}/{1}", Txt_SPFFolderLibrary.Text, TxtDirectoryToCreateOrRemove.Text);
 
                string execOutcome = m_SPO.DeleteFolder(targetFolderFullPath);
                RcTxtOutput.Text += string.Format("{0}\r\n\r\n", execOutcome);
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            string strMsgOutcome = string.Empty;
            try
            {
                strMsgOutcome = m_SPO.RefreshLogin();
                RcTxtOutput.Text += strMsgOutcome;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnSelectDestDownFolder_Click(object sender, EventArgs e)
        {
            try
            {
                FolderBrwDlg_Dest.SelectedPath = TxtDestFileSystem.Text;
                if (string.IsNullOrWhiteSpace(Path.GetDirectoryName(TxtDestFileSystem.Text.Trim())))
                {
                    FolderBrwDlg_Dest.SelectedPath = Path.GetDirectoryName(TxtDestFileSystem.Text.Trim());
                }
                DialogResult result = FolderBrwDlg_Dest.ShowDialog();
                TxtDestFileSystem.Text = FolderBrwDlg_Dest.SelectedPath + "\\";
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void BtnSelectSrcFile_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDlg.InitialDirectory = @"D:\";
                OpenFileDlg.Title = "Select the file to upload to SharePoint online directory";
                //OpenFileDlg.DefaultExt = "txt";
                //OpenFileDlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
                //OpenFileDlg.FilterIndex = 2;  
                //this.OpenFileDlg.Multiselect = true;
                OpenFileDlg.CheckFileExists = true;
                OpenFileDlg.CheckPathExists = true;
                if (OpenFileDlg.ShowDialog() == DialogResult.OK)
                {
                    TxtSrcFileSystem.Text = OpenFileDlg.FileName;
                }
            }
            catch (Exception ex)
            {
                RcTxtOutput.Text += string.Format("Error: {0}\r\n", ex.Message);
                MessageBox.Show(ex.Message, "Error"
                                , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string strMsgOutput = string.Format("Ver: {0}\r\n\r\nSviluppatore: {1}\r\nE-mail: {2}\r\n\r\n{3}"
                                                , c_AppVersion
                                                , "Giuseppe AINO"
                                                , "giuseppe.aino@pizza.it"
                                                , "SPINDOX.it");
            MessageBox.Show(strMsgOutput, "About"
                        , MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion
 
        #region Private methods
 
        private void ComposeUIAccessInfo()
        {
            //Base URL:
            TxtMainURL.Text = string.Format("{0}/sites/{1}"
                , Txt_SPSiteTenant.Text 
                , Txt_SPSiteName.Text);
            //SP First folder:      /sites/CC-CVReportingGateway/ResourceExchange
            TxtSP_URL_FirstFolder.Text = string.Format("{0}/{1}"
                , TxtMainURL.Text.Replace(Txt_SPSiteTenant.Text, string.Empty)
                , Txt_SPFFolderLibrary.Text);
        }
        #endregion
 
    }
}

Libreria Helper di SharePoint online:

using SP = Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using System.IO;
 
namespace VF_SPO_Library
{
    public class SPO_Helper
    {
        public SP.ClientContext Context;
        /// <summary>
        /// Messaggio di output della libreria disponibile solo per determinate operazioni.
        /// Utile per il Log e troubleshooting
        /// </summary>
        public string SharePointMessageOut = string.Empty;
 
        /// <summary>
        /// Es.: https:\\pizza.sharepoint.com/sites/NomeDelSito
        /// </summary>
        private string m_mainURL = string.Empty;
        /// <summary>
        /// e-mail a cui corrisponde la licenza Office 365 per l'accesso a SharePoint Online
        /// </summary>
        private string m_userName = string.Empty;
        /// <summary>
        /// Password di dominio criptata
        /// </summary>
        private SecureString m_password = new SecureString();
 
        public SPO_Helper()
        {
        }
 
        /// <summary>
        /// Oltre ad istanziare la classe istanzia il Contex che è l'oggetto chiave per ogni operazione su SharePoint Online.
        /// Restituisce un messaggio di welcome di inizializzazione.
        /// </summary>
        /// <param name="mainURL">Es.: https: pizza.sharepoint.com/sites/NomeDelSito</param>
        /// <param name="userName">e-mail a cui corrisponde la licenza Office 365 per l'accesso a SharePoint Online</param>
        /// <param name="password">Password di dominio criptata</param>
        public SPO_Helper(string mainURL, string userName, SecureString password)
        {
            SharePointMessageOut = RefreshLogin(mainURL, userName, password);
        }
 
        #region Public methods
        /// <summary>
        /// Riesegue il login "verboso" su SharePoint. Può essere usato per loggarsi su differente sito,
        /// memorizza la MainURL e le credenziali di riferimento in questa istanza di classe.
        /// </summary>
        /// <param name="mainURL">Es.: https: pizza.sharepoint.com/sites/NomeDelSito</param>
        /// <param name="userName">e-mail a cui corrisponde la licenza Office 365 per l'accesso a SharePoint Online</param>
        /// <param name="password">Password di dominio criptata</param>
        /// <returns>Messaggio di avvenuto accesso e descrizione del sito SharePoint</returns>
        public string RefreshLogin(string mainURL, string userName, SecureString password)
        {
            m_mainURL = mainURL;
            m_userName = userName;
            m_password = password;
 
            return RefreshLogin();
        }
 
        /// <summary>
        /// Riesegue il login con i dati di riferimento già memorizzate nelle variabili locali a questa istanza di classe.
        /// </summary>
        /// <returns>Messaggio di avvenuto accesso e descrizione del sito SharePoint</returns>
        public string RefreshLogin()
        {
            string welcomeMsgOut = string.Empty;
 
            Context = new SP.ClientContext(m_mainURL); // Es.: "https://pizza.sharepoint.com/sites/NomeSito"
            Context.Credentials = new SP.SharePointOnlineCredentials(m_userName, m_password);
 
            // The SharePoint web at the URL.
            SP.Web web = Context.Web;
            // We want to retrieve the web's properties.
            Context.Load(web);
            // Execute the query to the server.
            Context.ExecuteQuery();
 
            // Now, the web's properties are available and we could display
            // web properties, such as title.
            welcomeMsgOut = string.Format("{0}\r\n{1}\r\n\r\n", web.Title, web.Description);
 
            return welcomeMsgOut;
        }
 
        /// <summary>
        /// Scarica da SharePoint, l'intero contenuto (solo files) nella cartella "subFoldersCSV" (sottostante la "docLibraryPath") e li colloca nella cartella locale "localFolderDest".
        /// </summary>
        /// <param name="docLibraryPath">URL relativo della cartella\libreria del sito, es.: /sites/CC-CVReportingGateway/ResourceExchange</param>
        /// <param name="subFoldersCSV">Percorso cartella sottostrante la principale ovvero la "docLibraryPath". Il divisore delle cartelle in questa stringa può essere \ oppure /</param>
        /// <param name="localFolderDest">Percorso sul FileSystem locale, es.: D:\Tmp\Immagini</param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        /// <returns>Indica se almeno un file è stato scaricato in locale</returns>
        public bool DownloadFilesFromSharePoint(string docLibraryPath, string subFoldersCSV
                                                , string localFolderDest
                                                , out string strMsgOutcome)
        {
            SP.FileCollection files = null;
            bool leafFolderFound = false;
            bool fileDownloaded = false;
            int i = 0;
            int n = 0;
            string strSubMsgOutcome = string.Empty;
 
            strMsgOutcome = string.Empty;
            if (!string.IsNullOrWhiteSpace(subFoldersCSV))
            {
                subFoldersCSV = subFoldersCSV.Replace('/', '\\');
 
                string[] arrFoldersToSelect = subFoldersCSV.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                n = arrFoldersToSelect.Length;
                SP.FolderCollection folders = Context.Web.GetFolderByServerRelativeUrl(docLibraryPath).Folders;
                Context.Load(folders);
                Context.ExecuteQuery();
 
                //es    subFoldersCSV = "Files\DaLoredana\Old"
                foreach (string folderToSelect in arrFoldersToSelect)
                {
                    i++;
                    foreach (SP.Folder folder in folders)
                    {
                        if (folder.Name == folderToSelect)
                        {
                            if (i < n)
                            {
                                folders = folder.Folders;
                                Context.Load(folders);
                                Context.ExecuteQuery();
 
                                foreach (SP.Folder folder_deep in folders)
                                {
                                    strMsgOutcome += string.Format("SubFolder: {0}\r\n", folder_deep.Name);
                                }
                            }
                            else
                            {
                                files = folder.Files;
                                Context.Load(files);
                                Context.ExecuteQuery();
                                leafFolderFound = true;
                            }
                            break;
                        }
                    }
                }
            }
            else
            {
                files = Context.Web.GetFolderByServerRelativeUrl(docLibraryPath).Files;
 
                Context.Load(files);
                Context.ExecuteQuery();
                leafFolderFound = true;
            }
            if (leafFolderFound
                && files != null
                && files.Count > 0)
            {
                fileDownloaded = DownloadAllLeafFileList(files, localFolderDest, out strSubMsgOutcome);
            }
 
            strMsgOutcome += strSubMsgOutcome;
            Context.Dispose(); //???
            return fileDownloaded;
        }
 
        /// <summary>
        /// Fa upload di un file dal locale FileSystem collocandolo in una cartella su SharePointOnline. 
        /// La cartella può esser annidata ma sottostante la cartellaprincipale\libreria indicata.
        /// </summary>
        /// <param name="siteUrl">Es.: https://pizza.sharepoint.com/sites/NomeDelSito</param>
        /// <param name="docLibraryName">Nome libreria (Cartella principale) su SharePoint, es.: Documents</param>
        /// <param name="clientSubFolder">Percorso cartella sottostante la principale es.: SottoCartellaSP\SottoSottoCartellaSP</param>
        /// <param name="fileNameFullPath">Percorso completo FileSystem locale es.: C:\Sorgente\testupload.pdf</param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        public void UploadFileToSharePoint(string siteUrl, string docLibraryName
                                            , string clientSubFolder
                                            , string fileNameLocalFullPath // D:\Tmp\Img\Barcellona_1920x364.jpg
                                            , out string strMsgOutcome)
        {
            strMsgOutcome = string.Empty;
            clientSubFolder = clientSubFolder.Replace('/', '\\');
            string[] arrSPOFolderName = clientSubFolder.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);  // Files\daUmberto\xLoredana
            int i = 0;
            int n = arrSPOFolderName.Length;
 
            #region Insert the data                
            SP.Web web = Context.Web;
 
            SP.FileCreationInformation newFile = new SP.FileCreationInformation();
            newFile.Overwrite = true;
            byte[] fileContent = System.IO.File.ReadAllBytes(fileNameLocalFullPath);
            newFile.ContentStream = new MemoryStream(fileContent);
            newFile.Url = Path.GetFileName(fileNameLocalFullPath); //Barcellona_1920x364.jpg
 
            SP.List DocLibrary = web.Lists.GetByTitle(docLibraryName); //Non sicuro che funzioni
 
            SP.Folder SPOFolder = null;
 
            foreach (string spoFolderName in arrSPOFolderName)
            {
                if (i == 0)
                {
                    SPOFolder = DocLibrary.RootFolder.Folders.GetByUrl(spoFolderName);
                    Context.Load(DocLibrary);
                }
                else
                {
                    SPOFolder = SPOFolder.Folders.GetByUrl(spoFolderName);
                }
                SPOFolder.Update();
                Context.ExecuteQuery();
                i++;
            }
            SP.File UploadFile = SPOFolder.Files.Add(newFile);
 
            Context.Load(UploadFile);
            Context.ExecuteQuery();
 
            strMsgOutcome += string.Format("The File has been uploaded"
                                + Environment.NewLine + "FileUrl -->" + siteUrl
                                + "/" + docLibraryName + "/"
                                + clientSubFolder + "/" + Path.GetFileName(fileNameLocalFullPath));
            #endregion
        }
 
        /// <summary>
        /// Cancella un file su SharePoint, il file può esser annidato in base al persorso indicato in "subFoldersCSV", percorso che parte sotto "docLibraryPath".
        /// </summary>
        /// <param name="docLibraryPath">URL relativo della cartella\libreria del sito, es.: /sites/CC-CVReportingGateway/ResourceExchange</param>
        /// <param name="subFoldersCSV">Percorso cartella sottostrante la principale ovvero la "docLibrary". Il divisore delle cartelle in questa stringa può essere \ oppure /</param>
        /// <param name="fileName">Nome del file da cancellare</param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        /// <returns>Indica se il file esiste nel percorso indicato per la cancellazione</returns>
        public bool DeleteFileFromSharePoint(string docLibraryPath     //es:   /sites/CC-CVReportingGateway/ResourceExchange
                                            , string subFoldersCSV
                                            , string fileName
                                            , out string strMsgOutcome)
        {
            bool deleted = false;
            strMsgOutcome = string.Empty;
 
            subFoldersCSV = subFoldersCSV.Replace('\\', '/');
            // Es.: /sites/CC-CVReportingGateway/ResourceExchange/SubFolder1/SubFoldern/FileName.ext
            string relativeFilePath = string.Format("{0}/{1}/{2}", docLibraryPath, subFoldersCSV, fileName);
 
            SP.File file = null;
            if (FileExists(relativeFilePath, out file, out strMsgOutcome))
            {
                file.DeleteObject();
                Context.ExecuteQuery();
 
                deleted = !FileExists(relativeFilePath, out file, out strMsgOutcome);
                strMsgOutcome = string.Format("Conferma file '{0}' cancellato", fileName);
            }
            else
            {
                deleted = true;
                strMsgOutcome = string.Format("Il file '{0}' non era presente", fileName);
            }
            return deleted;
        }
 
        /// <summary>
        /// Crea una nuova cartella su SharePoint, in realtà crea tutte le cartelle del percorso se assenti.
        /// </summary>
        /// <param name="docLibraryName">Semplice nome della cartella\libreria del sito, es.: Documents</param>
        /// <param name="newFolderFullPath">Percorso cartella sottostrante la principale ovvero la "docLibraryName". Il divisore delle cartelle in questa stringa può essere \ oppure /</param>
        /// <returns>Messaggio di log con l'esito dell'operazione</returns>
        public string CreateFolder(string docLibraryName  
                                , string newFolderFullPath)
        {
            string logOut = string.Empty;
            /* https://sharepoint.stackexchange.com/questions/94820/c-csom-create-folders-in-lists-programmatically */
            string[] arrSPOFolderName = newFolderFullPath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            SP.List list = Context.Web.Lists.GetByTitle(docLibraryName);
            //list.EnableFolderCreation = true;
 
            SP.Folder myRemoteFolder = list.RootFolder;
 
            //Scansiona ogni directory del path in "newFolderFullPath", se la cartella non esiste la crea automaticamente
            foreach (string myFolder in arrSPOFolderName)
            {
                Context.Load(myRemoteFolder);
                myRemoteFolder.Folders.Add(myFolder);
                Context.ExecuteQuery();
 
                myRemoteFolder = Context.Web.GetFolderByServerRelativeUrl(myRemoteFolder.ServerRelativeUrl + "/" + myFolder);
            }
            logOut = string.Format("Folder path '{0}' correctly performed.", newFolderFullPath);
            return logOut;
        }
 
        /// <summary>
        /// Elimina la cartella foglia specificata nel percorso indicato. Elimina anche le sottocartelle anche se non vuote.
        /// </summary>
        /// <param name="targetFolderFullPath">Percorso completo compresa la prima cartella ovvero la libreria. Es.: Documents\Files\NuovaSub1\TargetFolder</param>
        /// <returns></returns>
        public string DeleteFolder(string targetFolderFullPath)
        {
            string logOut = string.Empty;
 
            targetFolderFullPath = targetFolderFullPath.Replace('\\', '/');
 
            // Enter server relative URL of the folder
            SP.Folder spTargetFolder = Context.Web.GetFolderByServerRelativeUrl(targetFolderFullPath);
 
            //La cartella foglia sarà cancellata anche nel caso contenga files o altre cartelle
            spTargetFolder.DeleteObject();
 
            Context.ExecuteQuery();
            logOut = string.Format("Folder path '{0}' correctly removed.", targetFolderFullPath);
            return logOut;
        }
 
        /// <summary>
        /// Risponde alla domanda se il file esiste nel percorso specificato.
        /// </summary>
        /// <param name="relativeFilePathURL">Es.: /sites/CC-CVReportingGateway/ResourceExchange/SubFolder1/SubFoldern/FileName.ext</param>
        /// <param name="file">Se il file esiste restituisce le info del file in formato SharePoint</param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        /// <returns>Se il file esiste o meno</returns>
        public bool FileExists(string relativeFilePathURL
                                , out SP.File file
                                , out string strMsgOutcome)
        {
            strMsgOutcome = string.Empty;
            try
            {
                SP.Web web = Context.Web;
 
                file = web.GetFileByServerRelativeUrl(relativeFilePathURL);
                Context.Load(file);
                Context.ExecuteQuery();
                strMsgOutcome = string.Format("File '{0}' exists and supplied in output.", file.Name);
                return true;
            }
            catch (Microsoft.SharePoint.Client.ServerException ex)
            {
                if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
                {
                    file = null;
                    strMsgOutcome = string.Format("File '{0}' not exists.", file.Name);
                    return false;
                }
                else
                    throw;
            }
        }
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootFolder"></param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        public void GetTitleFolderLists(string rootFolder
                                        , out string strMsgOutcome)
        {
            strMsgOutcome = string.Empty;
            SP.Web web = Context.Web;
 
            // Assume the web has a list named "Announcements".
            SP.List foldersList = Context.Web.Lists.GetByTitle(rootFolder);
 
            // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
            // so that it grabs all list items, regardless of the folder they are in.
            SP.CamlQuery query = SP.CamlQuery.CreateAllItemsQuery(); //CreateAllItemsQuery(100)
            SP.ListItemCollection items = foldersList.GetItems(query);
 
            // Retrieve all items in the ListItemCollection from List.GetItems(Query).
            Context.Load(items);
            Context.ExecuteQuery();
            foreach (SP.ListItem listItem in items)
            {
                // We have all the list item data. For example, Title.
                strMsgOutcome += string.Format("{0}\r\n", listItem["Title"]);
            }
            strMsgOutcome += string.Format("finish\r\n");
        }
        #endregion
 
        #region Private methods
        /// <summary>
        /// Data una lista di files già determinata provvede ciclicamente a scaricarli localmente nella cartella del FileSystem specificata.
        /// </summary>
        /// <param name="files">Elenco SharePoint di files candidati</param>
        /// <param name="localFolderDest">Cartella del FileSystem locale</param>
        /// <param name="strMsgOutcome">Messaggio di log con l'esito dell'operazione</param>
        /// <returns>Indica se almeno un file della lista è stato scaricato localmente</returns>
        private bool DownloadAllLeafFileList(SP.FileCollection files, string localFolderDest
                                                , out string strMsgOutcome)
        {
            bool fileDownloaded = false;
            strMsgOutcome = string.Empty;
 
            //localFolderDest deve finire con   \       !
            foreach (SP.File file in files)
            {
                SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(Context, file.ServerRelativeUrl);
                Context.ExecuteQuery();
 
                var filePath = localFolderDest + file.Name;
                using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                {
                    fileInfo.Stream.CopyTo(fileStream);
                }
                fileDownloaded = true;
                strMsgOutcome += string.Format("Copyed: {0}\r\n", file.Name);
            }
            strMsgOutcome += string.Format("{0} files scaricati\r\n", files.Count);
            return fileDownloaded;
        }
        #endregion
    }
}

Mappa e Link


SharePoint OnLine - indice | SharePoint WYSIWYG


MS Power Platform - Office 365 | Dizionario | [[ | ]]


Parole chiave: