Esempio n. 1
0
        private static Folder CreateFolderImplementation(FolderCollection folderCollection, string folderName)
        {
            var newFolder = folderCollection.Add(folderName);
            folderCollection.Context.Load(newFolder);
            folderCollection.Context.ExecuteQueryRetry();

            return newFolder;
        }
Esempio n. 2
0
        static public String UploadToTeamSite(String localPath)
        {
            var siteUrl = ConfigurationManager.AppSettings["teamSiteUrl"];

            using (ClientContext clientContext = new ClientContext(siteUrl))
            {
                string username = ConfigurationManager.AppSettings["username"];

                //decrypt password
                string base64Encoded = ConfigurationManager.AppSettings["password"];
                string password;
                byte[] data = System.Convert.FromBase64String(base64Encoded);
                password = System.Text.ASCIIEncoding.ASCII.GetString(data);

                NetworkCredential _myCredentials = new NetworkCredential(username, password);
                clientContext.Credentials = _myCredentials;
                clientContext.ExecuteQuery();

                var ServerVersion = clientContext.ServerLibraryVersion.Major;

                var site = clientContext.Site;
                var web  = clientContext.Site.RootWeb;

                clientContext.Load(web, w => w.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                var serverRelativeUrl = clientContext.Site.RootWeb.ServerRelativeUrl;

                //Check and create folder
                string name = DateTime.Now.ToString("yyyy.MM.dd");
                Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("CookieCheckerResults");
                FolderCollection folders = list.RootFolder.Folders;
                clientContext.Load(list);
                clientContext.Load(folders);
                clientContext.ExecuteQuery();

                var folderExists = folders.Any(X => X.Name == name);
                if (!folderExists)
                {
                    Folder newFolder = folders.Add(name);
                    clientContext.Load(newFolder);
                    clientContext.ExecuteQuery();
                }

                //Add the file
                string[] Splits = localPath.Split('\\');
                using (FileStream fs = new FileStream(localPath, FileMode.Open))
                {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/sites/sp_team_nbg/CookieCheckerResults/" + name + "/" + Splits[Splits.Length - 1], fs, true);
                }

                return(ConfigurationManager.AppSettings["teamSiteUrl"] + "/CookieCheckerResults/" + name);
            }
        }
Esempio n. 3
0
 public void AddOrUpdateFolderItem(FolderItem item)
 {
     if (FolderCollection.Contains(item))
     {
         int sourceIndex = FolderCollection.IndexOf(item);
         FolderCollection.Remove(item);
         FolderCollection.Insert(sourceIndex, item);
         if (CurrentSelectedFolder != null && CurrentSelectedFolder.Equals(item))
         {
             CurrentSelectedFolderChanged?.Invoke(this, item);
         }
         IsFolderListChanged = true;
     }
     else
     {
         FolderCollection.Add(item);
         IsFolderListChanged = true;
     }
 }
Esempio n. 4
0
        /// <summary>
        /// This function adds a subfolder to the desiginated folder in SharePoint.
        ///
        ///  Please refer to the checkForReqProps metod for required properties.
        ///
        /// </summary>
        /// <param name="NewFolder"></param>
        /// <param name="ParentPath"></param>
        /// <returns></returns>
        public String AddSPFolder(string NewFolder, string ParentPath)
        {
            String           tmpRetVal = String.Empty;
            ClientContext    ctx       = null;
            Web              tmpWeb    = null;
            List             tmpList   = null;
            ListCollection   tmpLists;
            FolderCollection tmpFolders = null;

            Microsoft.SharePoint.Client.Folder tmpFolder = null;
            try
            {
                ctx      = new ClientContext(SPSiteURL);
                tmpWeb   = ctx.Web;
                tmpLists = tmpWeb.Lists;
                tmpList  = tmpLists.GetByTitle(SPRootPath);

                if (tmpList != null)
                {
                    tmpFolders = tmpWeb.GetFolderByServerRelativeUrl(ParentPath).Folders;

                    ctx.Load(tmpFolders);
                    ctx.ExecuteQuery();

                    tmpFolder = tmpFolders.Add(NewFolder);
                    tmpFolder.Update();
                    ctx.ExecuteQuery();

                    tmpRetVal = ParentPath + "/" + NewFolder;
                }
            }

            catch (Exception ex)
            {
                RetErrMessage = ex.Message;
                tmpRetVal     = String.Empty;
            }
            return(tmpRetVal);
        }
Esempio n. 5
0
        /// <summary>
        ///  Adds folders inside the document library
        /// </summary>
        /// <param name="clientContext">Context of client</param>
        /// <param name="list">List object</param>
        /// <param name="folderNames">Names of folder</param>
        /// <returns>List of folder added</returns>
        internal static List AddFolders(ClientContext clientContext, List list, IList <string> folderNames)
        {
            // Addition of Folders
            FolderCollection listFolders = list.RootFolder.Folders;

            Microsoft.SharePoint.Client.Folder listRootFolder = list.RootFolder;
            clientContext.Load(listFolders);
            if (0 < folderNames.Count)
            {
                foreach (string folderName in folderNames)
                {
                    // Check for empty folder names
                    if (!string.IsNullOrWhiteSpace(folderName))
                    {
                        listFolders.Add(folderName);
                        listRootFolder.Update();
                    }
                }
                list.Update();
                clientContext.ExecuteQuery();
            }
            return(list);
        }
Esempio n. 6
0
 /// <summary>
 /// Adds all the folders from Content type in matter library.
 /// </summary>
 /// <param name="clientContext">Client context</param>
 /// <param name="list">List of folders</param>
 /// <param name="folderNames">The folder names.</param>
 /// <returns>Microsoft SharePoint Client List</returns>
 public static List AddFolders(ClientContext clientContext, List list, IList <string> folderNames)
 {
     if (null != clientContext && null != list && null != folderNames)
     {
         FolderCollection listFolders    = list.RootFolder.Folders;
         Folder           listRootFolder = list.RootFolder;
         clientContext.Load(listFolders);
         clientContext.ExecuteQuery();
         if (0 < folderNames.Count)
         {
             foreach (string folderName in folderNames)
             {
                 // Check for empty folder names
                 if (!string.IsNullOrEmpty(folderName))
                 {
                     listFolders.Add(folderName);
                     listRootFolder.Update();
                 }
             }
             list.Update();
         }
     }
     return(list);
 }
Esempio n. 7
0
        public virtual object GetRootFolders(HttpContext context)
        {
            YZRequest        request     = new YZRequest(context);
            string           libType     = request.GetString("libType");
            string           uid         = YZAuthHelper.LoginUserAccount;
            FolderCollection rootFolders = new FolderCollection();

            using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
            {
                using (IDbConnection cn = provider.OpenConnection())
                {
                    LibraryCollection libs = LibraryManager.GetLibraries(provider, cn, uid, libType, null, null);
                    foreach (Library.Library lib in libs)
                    {
                        Folder folder = DirectoryManager.GetFolderByID(provider, cn, lib.FolderID);
                        folder.Name = lib.Name;

                        rootFolders.Add(folder);
                    }
                }
            }

            return(rootFolders);
        }
        private static Folder CreateFolderImplementation(FolderCollection folderCollection, string folderName, Folder parentFolder = null)
        {
            ClientContext context = null;
            if (parentFolder != null)
            {
                context = parentFolder.Context as ClientContext;
            }

            List parentList = null;

            if (parentFolder != null)
            {
                parentFolder.EnsureProperty(p => p.Properties);
                if (parentFolder.Properties.FieldValues.ContainsKey("vti_listname"))
                {
                    if (context != null)
                    {
                        Guid parentListId = Guid.Parse((String)parentFolder.Properties.FieldValues["vti_listname"]);
                        parentList = context.Web.Lists.GetById(parentListId);
                        context.Load(parentList, l => l.BaseType, l => l.Title);
                        context.ExecuteQueryRetry();
                    }
                }
            }

            if (parentList == null)
            {
                // Create folder for library or common URL path
                var newFolder = folderCollection.Add(folderName);
                folderCollection.Context.Load(newFolder);
                folderCollection.Context.ExecuteQueryRetry();
                return newFolder;
            }
            else
            {
                // Create folder for generic list
                ListItemCreationInformation newFolderInfo = new ListItemCreationInformation();
                newFolderInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
                newFolderInfo.LeafName = folderName;
                parentFolder.EnsureProperty(f => f.ServerRelativeUrl);
                newFolderInfo.FolderUrl = parentFolder.ServerRelativeUrl;
                ListItem newFolderItem = parentList.AddItem(newFolderInfo);
                newFolderItem["Title"] = folderName;
                newFolderItem.Update();
                context.ExecuteQueryRetry();

                // Get the newly created folder
                var newFolder = parentFolder.Folders.GetByUrl(folderName);
                // Ensure all properties are loaded (to be compatible with the previous implementation)
                context.Load(newFolder);
                context.ExecuteQuery();
                return (newFolder);
            }
        }
Esempio n. 9
0
        private static Folder CreateFolderImplementation(FolderCollection folderCollection, string folderName)
        {
            var newFolder = folderCollection.Add(folderName);
            folderCollection.Context.Load(newFolder);
            folderCollection.Context.ExecuteQuery();

            return newFolder;
        }
Esempio n. 10
0
        /// <summary>
        /// Upload files to SharePoint assets library
        /// </summary>
        /// <param name="matterCenterAssetsFolder">Matter Center Assets Folder</param>
        /// <param name="clientContext">Client Context</param>
        /// <param name="siteAssets">Site Assets</param>
        /// <param name="listFolders">List Folders</param>
        /// <param name="listval">Configuration values from Configuration Excel</param>
        private static void UploadFilesToFolder(string matterCenterAssetsFolder, ClientContext clientContext, List siteAssets, FolderCollection listFolders, Dictionary<string, string> listval)
        {
            try
            {
                // Add matter landing folder
                listFolders.Add(matterCenterAssetsFolder);
                siteAssets.RootFolder.Update();
                siteAssets.Update();
                clientContext.Load(listFolders, folders => folders.Include(folderProperty => folderProperty.Name, folderProperty => folderProperty.ServerRelativeUrl).Where(folder => folder.Name == matterCenterAssetsFolder));
                clientContext.ExecuteQuery();

                Console.WriteLine("\n " + matterCenterAssetsFolder + " folder created...");
                string matterCenterAssetsUrl = listFolders.FirstOrDefault().ServerRelativeUrl;
                string staticContentFolder = ConfigurationManager.AppSettings["StaticContentFolder"];
                if (!string.IsNullOrWhiteSpace(matterCenterAssetsUrl))
                {
                    string parentDirectory = Convert.ToString(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, CultureInfo.InvariantCulture) + "\\" + staticContentFolder + "\\" + matterCenterAssetsFolder;
                    string matterLandingAssetsFolder = ConfigurationManager.AppSettings["MatterLandingAssets"];
                    string webDashboardAssetsFolder = ConfigurationManager.AppSettings["WebDashboardAssets"];
                    string commonAssetsFolder = ConfigurationManager.AppSettings["CommonAssets"];
                    string documentLandingAssetsFolder = ConfigurationManager.AppSettings["DocumentLandingAssets"];

                    string images = ConfigurationManager.AppSettings["Images"];
                    string scripts = ConfigurationManager.AppSettings["Scripts"];
                    string styles = ConfigurationManager.AppSettings["Styles"];

                    // Create matter landing assets and web dashboard assets folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + styles);

                    // Add web dashboard folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + styles);

                    // Add common assets folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + styles);

                    // Add document landing folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + styles);

                    siteAssets.Update();
                    clientContext.ExecuteQuery();

                    Console.WriteLine("Created matter landing and web dashboard assets folders.");

                    Console.WriteLine(" ------- Starting to upload assets ------- ");

                    // Upload matter landing assets
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder, clientContext, listval);
                    // Upload web dashboard images
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + images, clientContext, listval);
                    // Upload web dashboard scripts
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload web dashboard styles
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + styles, clientContext, listval);
                    // Upload matter landing images
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + images, clientContext, listval);
                    // Upload matter landing scripts
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload matter landing styles
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + styles, clientContext, listval);
                    // Upload common images
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + images, clientContext, listval);
                    // Upload common scripts
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload common styles
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + styles, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + scripts, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + images, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + styles, clientContext, listval);
                }
                else
                {
                    Console.WriteLine("\n Something went wrong. The matter center assets folder is not created or unable to browse.");
                }
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorFilePath, "Message: " + exception.Message + "\nStacktrace: " + exception.StackTrace);
            }
        }
Esempio n. 11
0
        private static Folder EnsureFolderImplementation(FolderCollection folderCollection, string folderName)
        {
            // TODO: Check for any other illegal characters in SharePoint
            if (folderName.Contains('/') || folderName.Contains('\\'))
            {
                throw new ArgumentException("The argument must be a single folder name and cannot contain path characters.", "folderName");
            }

            folderCollection.Context.Load(folderCollection);
            folderCollection.Context.ExecuteQuery();
            foreach (Folder folder in folderCollection)
            {
                if (string.Equals(folder.Name, folderName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return folder;
                }
            }

            var newFolder = folderCollection.Add(folderName);
            folderCollection.Context.Load(newFolder);
            folderCollection.Context.ExecuteQuery();

            return newFolder;
        }
Esempio n. 12
0
        /// <summary>
        /// Upload files to SharePoint assets library
        /// </summary>
        /// <param name="matterCenterAssetsFolder">Matter Center Assets Folder</param>
        /// <param name="clientContext">Client Context</param>
        /// <param name="siteAssets">Site Assets</param>
        /// <param name="listFolders">List Folders</param>
        /// <param name="listval">Configuration values from Configuration Excel</param>
        private static void UploadFilesToFolder(string matterCenterAssetsFolder, ClientContext clientContext, List siteAssets, FolderCollection listFolders, Dictionary <string, string> listval)
        {
            try
            {
                // Add matter landing folder
                listFolders.Add(matterCenterAssetsFolder);
                siteAssets.RootFolder.Update();
                siteAssets.Update();
                clientContext.Load(listFolders, folders => folders.Include(folderProperty => folderProperty.Name, folderProperty => folderProperty.ServerRelativeUrl).Where(folder => folder.Name == matterCenterAssetsFolder));
                clientContext.ExecuteQuery();

                Console.WriteLine("\n " + matterCenterAssetsFolder + " folder created...");
                string matterCenterAssetsUrl = listFolders.FirstOrDefault().ServerRelativeUrl;
                string staticContentFolder   = ConfigurationManager.AppSettings["StaticContentFolder"];
                if (!string.IsNullOrWhiteSpace(matterCenterAssetsUrl))
                {
                    string parentDirectory             = Convert.ToString(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, CultureInfo.InvariantCulture) + "\\" + staticContentFolder + "\\" + matterCenterAssetsFolder;
                    string matterLandingAssetsFolder   = ConfigurationManager.AppSettings["MatterLandingAssets"];
                    string webDashboardAssetsFolder    = ConfigurationManager.AppSettings["WebDashboardAssets"];
                    string commonAssetsFolder          = ConfigurationManager.AppSettings["CommonAssets"];
                    string documentLandingAssetsFolder = ConfigurationManager.AppSettings["DocumentLandingAssets"];

                    string images  = ConfigurationManager.AppSettings["Images"];
                    string scripts = ConfigurationManager.AppSettings["Scripts"];
                    string styles  = ConfigurationManager.AppSettings["Styles"];

                    // Create matter landing assets and web dashboard assets folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + styles);

                    // Add web dashboard folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + styles);

                    // Add common assets folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + styles);

                    // Add document landing folder
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + images);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + scripts);
                    listFolders.Add(matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + styles);

                    siteAssets.Update();
                    clientContext.ExecuteQuery();

                    Console.WriteLine("Created matter landing and web dashboard assets folders.");

                    Console.WriteLine(" ------- Starting to upload assets ------- ");

                    // Upload matter landing assets
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder, clientContext, listval);
                    // Upload web dashboard images
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + images, clientContext, listval);
                    // Upload web dashboard scripts
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload web dashboard styles
                    UploadAssets(parentDirectory + "\\" + webDashboardAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + webDashboardAssetsFolder + "/" + styles, clientContext, listval);
                    // Upload matter landing images
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + images, clientContext, listval);
                    // Upload matter landing scripts
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload matter landing styles
                    UploadAssets(parentDirectory + "\\" + matterLandingAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + matterLandingAssetsFolder + "/" + styles, clientContext, listval);
                    // Upload common images
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + images, clientContext, listval);
                    // Upload common scripts
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + scripts, clientContext, listval);
                    // Upload common styles
                    UploadAssets(parentDirectory + "\\" + commonAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + commonAssetsFolder + "/" + styles, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + scripts, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + scripts, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + images, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + images, clientContext, listval);

                    UploadAssets(parentDirectory + "\\" + documentLandingAssetsFolder + "\\" + styles, "*", matterCenterAssetsUrl + "/" + documentLandingAssetsFolder + "/" + styles, clientContext, listval);
                }
                else
                {
                    Console.WriteLine("\n Something went wrong. The matter center assets folder is not created or unable to browse.");
                }
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorFilePath, "Message: " + exception.Message + "\nStacktrace: " + exception.StackTrace);
            }
        }
Esempio n. 13
0
        public FileUploadResult UploadFile(Stream file, string filename, string mimeType, string folderName)
        {
            // TODO: Consider updating this to use PnP Core when it supports .NET Standard (https://github.com/pnp/PnP-Sites-Core)
            Uri       site = new Uri(_options.SiteUrl);
            const int fileChunkSizeInMB = 3;

            Microsoft.SharePoint.Client.File uploadFile = null;

            using (var authenticationManager = new AuthenticationManager(_options.TenantId))
            {
                using (var ctx = authenticationManager.GetContext(site, _options.ClientId, _options.ClientSecret))
                {
                    // https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-sharepoint

                    // Each sliced upload requires a unique id
                    Guid uploadId = Guid.NewGuid();

                    // TODO: Library name would be dynamic, based on type of amendment (dependent on amendment data model)
                    List uploadLibrary = ctx.Web.Lists.GetByTitle("Amendment");

                    // create dedicated folder if one has not already been created for this amendment
                    // TODO: Re-work interface so that folder is created ahead of uploading first file, then this
                    // check can be removed.
                    FolderCollection folders = uploadLibrary.RootFolder.Folders;
                    ctx.Load(folders);
                    ctx.ExecuteQuery();

                    folderName = folderName.Trim();

                    var targetFolder = folders.FirstOrDefault(x => x.Name == folderName);

                    if (targetFolder == null)
                    {
                        // create folder
                        targetFolder = folders.Add(folderName);
                        ctx.Load(targetFolder);
                        ctx.ExecuteQuery();
                    }

                    // Calculate block size in bytes
                    int blockSize = fileChunkSizeInMB * 1024 * 1024;

                    // Get the size of the file
                    long fileSize = file.Length;

                    if (fileSize <= blockSize)
                    {
                        // Use regular approach
                        FileCreationInformation fileInfo = new FileCreationInformation();
                        fileInfo.ContentStream = file;
                        fileInfo.Url           = filename;
                        fileInfo.Overwrite     = true;
                        uploadFile             = targetFolder.Files.Add(fileInfo);
                        ctx.Load(uploadFile);
                        ctx.ExecuteQuery();
                    }
                    else
                    {
                        // Use large file upload approach
                        ClientResult <long> bytesUploaded = null;

                        using (BinaryReader br = new BinaryReader(file))
                        {
                            byte[] buffer         = new byte[blockSize];
                            long   fileoffset     = 0;
                            long   totalBytesRead = 0;
                            int    bytesRead;
                            bool   first = true;

                            // Read data from filesystem in blocks
                            while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                totalBytesRead = totalBytesRead + bytesRead;

                                if (first)
                                {
                                    using (MemoryStream contentStream = new MemoryStream())
                                    {
                                        // Add an empty file.
                                        FileCreationInformation fileInfo = new FileCreationInformation();
                                        fileInfo.ContentStream = contentStream;
                                        fileInfo.Url           = filename;
                                        fileInfo.Overwrite     = true;
                                        uploadFile             = targetFolder.Files.Add(fileInfo);

                                        // Start upload by uploading the first slice.
                                        using (MemoryStream s = new MemoryStream(buffer))
                                        {
                                            // Call the start upload method on the first slice
                                            bytesUploaded = uploadFile.StartUpload(uploadId, s);
                                            ctx.ExecuteQuery();
                                            // fileoffset is the pointer where the next slice will be added
                                            fileoffset = bytesUploaded.Value;
                                        }

                                        // we can only start the upload once
                                        first = false;
                                    }
                                }
                                else
                                {
                                    // Get a reference to our file
                                    uploadFile = ctx.Web.GetFileByServerRelativeUrl(
                                        targetFolder.ServerRelativeUrl + System.IO.Path.AltDirectorySeparatorChar + filename);

                                    if (totalBytesRead == fileSize)
                                    {
                                        // We've reached the end of the file
                                        using (MemoryStream s = new MemoryStream(buffer, 0, bytesRead))
                                        {
                                            // End sliced upload by calling FinishUpload
                                            uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s);
                                            ctx.ExecuteQuery();

                                            break;
                                        }
                                    }
                                    else
                                    {
                                        using (MemoryStream s = new MemoryStream(buffer))
                                        {
                                            // Continue sliced upload
                                            bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
                                            ctx.ExecuteQuery();
                                            // update fileoffset for the next slice
                                            fileoffset = bytesUploaded.Value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(new FileUploadResult
            {
                FileId = uploadFile.UniqueId,
                FileName = filename,
                FileSizeInBytes = uploadFile.Length,
                FolderName = folderName
            });
        }