public bool IsFolderValid(string url, Guid listId, string folderName, string currentDir)
        {
            folderName = folderName.Trim();
            if (String.IsNullOrEmpty(folderName))
            {
                return(false);
            }

            using (var clientContext = new SPContext(url, credentials.Get(url)))
            {
                SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir);
                clientContext.Load(parentFolder);
                var subfolders = clientContext.LoadQuery(parentFolder.Folders.Where(folder => folder.Name == folderName));
                try
                {
                    clientContext.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    SPLog.FileNotFound(ex, "Coult not load subfolders for a directory '{0}'", currentDir);
                    return(false);
                }

                return(subfolders.Any());
            }
        }
        public SP.Folder NewFolder(string url, Guid listId, string folderName, string currentDir)
        {
            if (!IsFolderValid(url, listId, folderName, currentDir))
            {
                return(null);
            }

            using (var clientContext = new SPContext(url, credentials.Get(url)))
            {
                SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir);
                clientContext.Load(parentFolder);

                // add a new folder
                SP.Folder newFolder = parentFolder.Folders.Add(folderName);
                parentFolder.Update();
                clientContext.Load(newFolder);
                try
                {
                    clientContext.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    SPLog.FileNotFound(ex, "Error occurred while creating a new folder with a name '{0}' for a directory '{1}'.", folderName, currentDir);
                    return(null);
                }

                CacheService.RemoveByTags(new[] { Documents.Tag(listId), Folders.Tag(listId) }, CacheScope.All);

                return(newFolder);
            }
        }
Beispiel #3
0
        public Folder Rename(string url, Guid libraryId, string folderPath, FolderRenameQuery folderRenameQuery)
        {
            Folder folder;

            if (string.IsNullOrEmpty(folderPath))
            {
                throw new InvalidOperationException("ServerRelativeUrl cannot be empty.");
            }

            try
            {
                using (var clientContext = new SPContext(url, credentials.Get(url)))
                {
                    var spfolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath);
                    var parent   = spfolder.ParentFolder;
                    clientContext.Load(spfolder);

                    // check that new folder name is unique
                    string folderName = folderRenameQuery.Name;
                    var    subfolderWithTheSameName = clientContext.LoadQuery(parent.Folders.Where(f => f.Name == folderName));
                    try
                    {
                        clientContext.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Rename() method for SharePoint ServerRelativeUrl: '{1}' in SPWebUrl: '{2}'. The exception message is: {3}", ex.GetType(), folderPath, url, ex.Message);
                        SPLog.FileNotFound(ex, message);

                        throw new SPInternalException(message, ex);
                    }

                    if (subfolderWithTheSameName != null && subfolderWithTheSameName.Any())
                    {
                        throw new InvalidOperationException(string.Format("The folder with '{0}' name already exists at ServerRelativeUrl: '{1}' in SPWebUrl: '{2}'.", folderName, folderPath, url));
                    }

                    var folderToRenameQuery = clientContext.Web.Lists.GetById(libraryId).GetItems(GetFolderByServerRelativeUrl(spfolder.ServerRelativeUrl));
                    var spfolderListItems   = clientContext.LoadQuery(SP.ClientObjectQueryableExtension.IncludeWithDefaultProperties(folderToRenameQuery, f => f["FileLeafRef"]));
                    clientContext.ExecuteQuery();

                    var spfolderListItem = spfolderListItems != null?spfolderListItems.FirstOrDefault() : null;

                    if (spfolderListItem == null)
                    {
                        throw new Exception("The folder cannot be loaded, because CAML request failed or invalid.");
                    }

                    spfolderListItem["FileLeafRef"] = folderRenameQuery.Name;
                    spfolderListItem.Update();

                    clientContext.Load(spfolder);

                    clientContext.ExecuteQuery();

                    folder = new Folder(spfolder.Name, spfolder.ServerRelativeUrl, spfolder.ItemCount, libraryId);
                }
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            catch (SPInternalException)
            {
                throw;
            }
            catch (Exception ex)
            {
                string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Rename() method FolderName: '{1}' ServerRelativeUrl: '{2}' in SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), folderRenameQuery.Name, folderPath, url, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }

            return(folder);
        }
Beispiel #4
0
        public Folder Create(string url, Guid libraryId, FolderCreateQuery folderCreateQuery)
        {
            Folder folder;

            try
            {
                SP.ListItem folderListItem;

                using (var clientContext = new SPContext(url, credentials.Get(url)))
                {
                    var splist       = clientContext.Web.Lists.GetById(libraryId);
                    var parentFolder = !String.IsNullOrEmpty(folderCreateQuery.Path) ? clientContext.Web.GetFolderByServerRelativeUrl(folderCreateQuery.Path) : splist.RootFolder;

                    clientContext.Load(parentFolder);

                    // check that new folder name is unique
                    var folderName = folderCreateQuery.Name;
                    var subfolderWithTheSameName = clientContext.LoadQuery(parentFolder.Folders.Where(f => f.Name == folderName));
                    try
                    {
                        clientContext.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        var message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Create() method for SharePoint ServerRelativeUrl: '{1}' in SPWebUrl: '{2}'. The exception message is: {3}", ex.GetType(), folderCreateQuery.Path, url, ex.Message);
                        SPLog.FileNotFound(ex, message);

                        throw new SPInternalException(message, ex);
                    }

                    if (subfolderWithTheSameName != null && subfolderWithTheSameName.Any())
                    {
                        throw new InvalidOperationException(string.Format("The folder with '{0}' name already exists at ServerRelativeUrl: '{1}' in SPWebUrl: '{2}'.", folderName, folderCreateQuery.Path, url));
                    }

                    var newFolder = parentFolder.Folders.Add(folderCreateQuery.Name);
                    clientContext.Load(newFolder, f => f.Name, f => f.ServerRelativeUrl, f => f.ItemCount);

                    parentFolder.Update();
                    clientContext.ExecuteQuery();

                    var query           = GetFolderByServerRelativeUrl(newFolder.ServerRelativeUrl);
                    var folderListItems = clientContext.LoadQuery(splist.GetItems(query));
                    clientContext.ExecuteQuery();

                    folder         = new Folder(newFolder.Name, newFolder.ServerRelativeUrl, newFolder.ItemCount, libraryId);
                    folderListItem = folderListItems.First();
                }

                listItemDataService.AddUpdate(new ItemBase(libraryId,
                                                           Guid.Parse(folderListItem["UniqueId"].ToString()),
                                                           folderListItem.Id,
                                                           Convert.ToDateTime(folderListItem["Modified"]))
                {
                    IsIndexable = false
                });
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            catch (SPInternalException)
            {
                throw;
            }
            catch (Exception ex)
            {
                string message = string.Format("An exception of type {0} occurred in the InternalApi.SPFolderService.Create() method while creating a new folder with the Name: '{1}' ServerRelativeUrl: '{2}' in SPWebUrl: '{3}'. The exception message is: {4}", ex.GetType(), folderCreateQuery.Name, folderCreateQuery.Path, url, ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }

            return(folder);
        }