Skip to main content

Posts

Showing posts from March, 2014

Copy files from a document library to a files system by looping through all folders and subfolders in a library

If you have a need to copy file files from a document library to a SharePoint files system specifically the layouts folder.      private void LoopSubFolders(string folderPath, SPFolderCollection subFolders)         {             string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");             string FolderPath = Path.Combine(templatePath, @"LAYOUTS\ATP\");             try             {                 foreach (SPFolder folder in subFolders)                 {                     if (folder.Name.ToLower() != "forms")                     {                         string folderN...

Delete all files and folders in a document library

Are you looking to delete all files and folders in a document library, look no further. Ensure your users are empowered to do so by elevating their privilege, allow unsafe update then loop through the library in question. SPSecurity.RunWithElevatedPrivileges(delegate()             {                 using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))                 {                     using (SPWeb web = elevatedSite.RootWeb)                     {                         web.AllowUnsafeUpdates = true;                         web.ValidateFormDigest();                         ...

Download a file from a document library

You can download a file from a file stream quickly by passing in a SPFile object and the path where you want it to be written to: static void DownloadFileUsingFileStream(SPFile file, string path)         {             byte[] filecontent = file.OpenBinary();             FileStream fs = new FileStream(path + file.Name, FileMode.CreateNew);             using (fs)             {                 BinaryWriter bw = new BinaryWriter(fs);                 bw.Write(filecontent, 0, filecontent.Length);                 bw.Close();             }         } QED