Example #1
0
        public static List <string> GetFolderListFromSharePoint(string targetDocLib)
        {
            List <string> yearFolderList = new List <string>();

            try
            {
                using (ClientContext clientContext = GetContextObject())
                {
                    Web web = clientContext.Web;
                    clientContext.Load(web, website => website.ServerRelativeUrl);
                    clientContext.ExecuteQuery();

                    Regex            regex = new Regex(Configuration.SiteUrl, RegexOptions.IgnoreCase);
                    string           strSiteRelavtiveURL = regex.Replace(targetDocLib, string.Empty);
                    FolderCollection folderList          = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + strSiteRelavtiveURL).Folders;
                    clientContext.Load(folderList);
                    clientContext.ExecuteQuery();
                    var folders = folderList.ToList();
                    foreach (Folder folder in folders)
                    {
                        if (folder.Name != "Forms") //Direct files at library comes under Forms folder
                        {
                            yearFolderList.Add(folder.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(yearFolderList);
        }
Example #2
0
        public static bool DeleteAllFileFromSharePoint(string libraryName, string ThreadFolderPath)
        {
            try
            {
                using (ClientContext clientContext = GetContextObject())
                {
                    Web web = clientContext.Web;
                    clientContext.Load(web, website => website.ServerRelativeUrl);
                    clientContext.ExecuteQuery();

                    Regex  regex           = new Regex(Configuration.SiteUrl, RegexOptions.IgnoreCase);
                    string documentLibrary = regex.Replace(Configuration.FileUrl, string.Empty);

                    FolderCollection folderList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + ThreadFolderPath).Folders;
                    clientContext.Load(folderList);
                    clientContext.ExecuteQuery();
                    var folders = folderList.ToList();
                    foreach (Microsoft.SharePoint.Client.Folder folderDelete in folders)
                    {
                        folderDelete.DeleteObject();
                        folderDelete.Recycle();
                        clientContext.ExecuteQuery();
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Example #3
0
        public static List <string> GetFileListFromSP(string libraryName, string folderName, string groupType, string SPDirPath)
        {
            List <string> filePaths = new List <string>();

            using (ClientContext context = GetContextObject())
            {
                Web web = context.Web;
                context.Load(web, website => website.ServerRelativeUrl);
                context.ExecuteQuery();

                IEnumerable <List> docLibs = context.LoadQuery(web.Lists.Where(l => l.BaseTemplate == 101));

                context.ExecuteQuery();

                if (groupType == "Multi")
                {
                    FolderCollection folderList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Folders;
                    context.Load(folderList);
                    context.ExecuteQuery();
                    var folders = folderList.ToList();

                    foreach (Folder folder in folders)
                    {
                        Console.WriteLine(folder.Name + "---- File count " + folder.ItemCount);

                        FileCollection fileList = folder.Files;
                        context.Load(fileList);
                        context.ExecuteQuery();
                        foreach (Microsoft.SharePoint.Client.File file in fileList)
                        {
                            Console.WriteLine(folder.Name + "---- File Name " + file.Name);
                            string filePath = Path.Combine(SPDirPath, folderName.Replace("/", "\\"), folder.Name, file.Name);
                            //string groupfullName = Path.Combine(folderName, folder.Name).Replace("\\", "/");
                            //filePaths.Add(new Tuple<string,string> (filePath, groupfullName));
                            filePaths.Add(filePath);
                        }
                    }
                }
                else if (groupType == "Single")
                {
                    FileCollection fileList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Files;
                    context.Load(fileList);
                    context.ExecuteQuery();
                    var files = fileList.ToList();

                    foreach (Microsoft.SharePoint.Client.File file in fileList)
                    {
                        Console.WriteLine(folderName + "---- File Name " + file.Name);
                        string filePath = Path.Combine(SPDirPath, folderName.Replace("/", "\\"), file.Name);
                        //filePaths.Add(new Tuple<string, string>(filePath, folderName.Replace("\\", "/")));
                        filePaths.Add(filePath);
                    }
                }
                else if (groupType == "")
                {
                }
            }
            return(filePaths);
        }
Example #4
0
        public async Task SaveFolderList()
        {
            IsFolderListChanged = false;
            List <FolderItem> folderList = new List <FolderItem>();

            folderList = FolderCollection.ToList();
            await App._instance.IO.SetLocalDataAsync(StaticString.FolderListFileName, JsonConvert.SerializeObject(folderList));
        }
Example #5
0
        public static bool DeleteFileFromSharePoint(string ThreadFolderPath, string TargetDocLib, string DeleteThreadFile)
        {
            try
            {
                bool existAndDeleted = FileExistAndDelete(ThreadFolderPath, TargetDocLib, DeleteThreadFile);
                if (existAndDeleted == false)
                {
                    using (ClientContext clientContext = GetContextObject())
                    {
                        Web web = clientContext.Web;
                        clientContext.Load(web, website => website.ServerRelativeUrl);
                        clientContext.ExecuteQuery();
                        Regex            regex           = new Regex(Configuration.SiteUrl, RegexOptions.IgnoreCase);
                        string           documentLibrary = regex.Replace(TargetDocLib, string.Empty);
                        FolderCollection folderList      = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + documentLibrary + "/" + ThreadFolderPath).Folders;
                        clientContext.Load(folderList);
                        clientContext.ExecuteQuery();
                        var folders = folderList.ToList();

                        foreach (Folder folder in folders)
                        {
                            string ThreadFolderPath2 = ThreadFolderPath + "\\" + folder.Name;
                            existAndDeleted = FileExistAndDelete(ThreadFolderPath2, TargetDocLib, DeleteThreadFile);
                            if (existAndDeleted == true)
                            {
                                break;
                            }
                        }
                    }
                }
                return(existAndDeleted);
            }
            catch (Exception ex)
            {
                //throw ex;
                return(false);
            }
        }
Example #6
0
        public static void GetFolders(Web web, ClientContext context, string libraryName, string folderName, string SPDirPath, int spyear)
        {
            Console.WriteLine("Getfolders1");
            FolderCollection folderList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Folders;

            context.Load(folderList);
            context.ExecuteQuery();
            var folders = folderList.ToList();

            if (folders.Count > 0)
            {
                Console.WriteLine("Getfolders1");
                foreach (Folder folder in folders)
                {
                    if (folder.Name == "Forms")
                    {
                        continue;
                    }
                    if (!folder.Files.AreItemsAvailable)
                    {
                        GetFolders(web, context, libraryName, folderName + "/" + folder.Name, SPDirPath, spyear);
                    }
                }
            }
            else
            {
                Console.WriteLine("Getfolders3");
                FileCollection fileList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Files;
                context.Load(fileList);
                context.ExecuteQuery();
                var files = fileList.ToList();
                fileCount = fileCount + files.Count();
                foreach (Microsoft.SharePoint.Client.File file in fileList)
                {
                    string[] split     = folderName.Split('/');
                    string   firstPart = string.Join("/", split.Take(split.Length - 1));
                    string   lastPart  = split.Last();
                    int      folderId  = 0;
                    Int32.TryParse(lastPart, out folderId);
                    string groupFolderName = string.Empty;
                    if (folderId > 0)
                    {
                        groupFolderName = split.Take(split.Length - 1).ToList()[0].ToString();
                        if (groupFolderName == "Private_Conversations" && spyear < 2014)
                        {
                            groupFolderName = folderName.Replace("/", "\\");
                        }
                    }
                    else
                    {
                        groupFolderName = lastPart;
                    }
                    //folderName.Substring(folderName.LastIndexOf("/") + 1);
                    Console.WriteLine(groupFolderName + "---- File Name " + file.Name);
                    string filePath = Path.Combine(SPDirPath, groupFolderName, file.Name);
                    Console.WriteLine(filePath);
                    filePaths.Add(filePath);
                    //filePaths.Add(filePath);
                }
            }
        }
        public static List <string> GetFileListFromSP(string libraryName, string folderName, string groupType, string SPDirPath)
        {
            List <string> filePaths = new List <string>();

            using (ClientContext context = GetContextObject())
            {
                Web web = context.Web;
                context.Load(web, website => website.ServerRelativeUrl);
                context.ExecuteQuery();

                IEnumerable <List> docLibs = context.LoadQuery(web.Lists.Where(l => l.BaseTemplate == 101));

                context.ExecuteQuery();

                if (groupType == "Multi")
                {
                    FolderCollection folderList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Folders;
                    context.Load(folderList);
                    context.ExecuteQuery();
                    var folders = folderList.ToList();

                    foreach (Folder folder in folders)
                    {
                        Console.WriteLine(folder.Name + "---- File count " + folder.ItemCount);

                        FileCollection fileList = folder.Files;
                        context.Load(fileList);
                        context.ExecuteQuery();
                        foreach (Microsoft.SharePoint.Client.File file in fileList)
                        {
                            Console.WriteLine(folder.Name + "---- File Name " + file.Name);
                            string filePath = Path.Combine(SPDirPath, folderName.Replace("/", "\\"), folder.Name, file.Name);
                            //string groupfullName = Path.Combine(folderName, folder.Name).Replace("\\", "/");
                            //filePaths.Add(new Tuple<string,string> (filePath, groupfullName));
                            filePaths.Add(filePath);
                        }
                    }
                }
                else if (groupType == "Single")
                {
                    FileCollection fileList = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + libraryName + "/" + folderName).Files;
                    context.Load(fileList);
                    context.ExecuteQuery();
                    var files = fileList.ToList();

                    foreach (Microsoft.SharePoint.Client.File file in fileList)
                    {
                        Console.WriteLine(folderName + "---- File Name " + file.Name);
                        string filePath = Path.Combine(SPDirPath, folderName.Replace("/", "\\"), file.Name);
                        //filePaths.Add(new Tuple<string, string>(filePath, folderName.Replace("\\", "/")));
                        filePaths.Add(filePath);
                    }
                }
                else if (groupType == "")
                {
                    //    Microsoft.SharePoint.Client.List docList = web.Lists.GetByTitle(libraryName);
                    //    context.Load(docList);
                    //    CamlQuery camlQuery = new CamlQuery();
                    //    camlQuery.ViewXml = string.Format("<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='File_x0020_Type'/>" +
                    //"<Value Type='Text'>{0}</Value></Eq></Where></Query></View>", "zip" );
                    //    ListItemCollection listCol = docList.GetItems(camlQuery);
                    //    context.Load(listCol);
                    //    context.ExecuteQuery();
                    //    foreach (Microsoft.SharePoint.Client.ListItem item in listCol)
                    //    {
                    //        Console.WriteLine("FileLeafRef: {0}", item["FileRef"]);
                    //        Console.WriteLine();
                    //        string SPFilePath = item["FileRef"].ToString();
                    //        string dirFilePath = SPFilePath.Replace(web.ServerRelativeUrl + "/" + libraryName, SPDirPath);
                    //        filePaths.Add(dirFilePath.Replace("/","\\"));
                    //    }
                }
            }
            return(filePaths);
        }