예제 #1
0
        /// <summary>
        /// Retrieves or creates the folder as a ListItem
        /// </summary>
        /// <param name="onlineLibrary"></param>
        /// <param name="destinationFolder"></param>
        /// <param name="folderName"></param>
        /// <param name="defaultLastItemId"></param>
        /// <returns>The listitem as a folder</returns>
        public static Folder GetOrCreateFolder(this List onlineLibrary, Folder destinationFolder, string folderName, int?defaultLastItemId = default(int?))
        {
            destinationFolder.EnsureProperties(afold => afold.ServerRelativeUrl, afold => afold.Folders);
            var folderRelativeUrl = destinationFolder.ServerRelativeUrl;
            // Remove invalid characters
            var      trimmedFolder = HelperExtensions.GetCleanDirectory(folderName, string.Empty);
            ListItem folderItem    = null;

            var camlFields     = new string[] { "Title", "ContentType", "ID" };
            var camlViewFields = CAML.ViewFields(camlFields.Select(s => CAML.FieldRef(s)).ToArray());


            var camlClause = CAML.And(
                CAML.And(
                    CAML.Eq(CAML.FieldValue("FileDirRef", FieldType.Text.ToString("f"), folderRelativeUrl)),
                    CAML.Or(
                        CAML.Eq(CAML.FieldValue("LinkFilename", FieldType.Text.ToString("f"), trimmedFolder)),
                        CAML.Eq(CAML.FieldValue("Title", FieldType.Text.ToString("f"), trimmedFolder))
                        )
                    )
                ,
                CAML.Eq(CAML.FieldValue("FSObjType", FieldType.Integer.ToString("f"), 1.ToString()))
                );

            var camlQueries = onlineLibrary.SafeCamlClauseFromThreshold(camlClause, defaultLastItemId);

            foreach (var camlAndValue in camlQueries)
            {
                var camlWhereClause = CAML.Where(camlAndValue);
                var camlQuery       = new CamlQuery()
                {
                    ViewXml = CAML.ViewQuery(ViewScope.RecursiveAll, camlWhereClause, string.Empty, camlViewFields, 5)
                };
                var listItems = onlineLibrary.GetItems(camlQuery);
                onlineLibrary.Context.Load(listItems);
                onlineLibrary.Context.ExecuteQueryRetry();

                if (listItems.Count() > 0)
                {
                    folderItem = listItems.FirstOrDefault();
                    System.Diagnostics.Trace.TraceInformation("Item {0} exists in the destination folder.  Skip item creation file.....", folderName);
                    break;
                }
            }
            ;

            if (folderItem != null)
            {
                return(folderItem.Folder);
            }

            try
            {
                var info = new ListItemCreationInformation();
                info.UnderlyingObjectType = FileSystemObjectType.Folder;
                info.LeafName             = trimmedFolder;
                info.FolderUrl            = folderRelativeUrl;

                folderItem          = onlineLibrary.AddItem(info);
                folderItem["Title"] = trimmedFolder;
                folderItem.Update();
                onlineLibrary.Context.ExecuteQueryRetry();
                System.Diagnostics.Trace.TraceInformation("{0} folder Created", trimmedFolder);
                return(folderItem.Folder);
            }
            catch (Exception Ex)
            {
                System.Diagnostics.Trace.TraceError("Failed to create or get folder for name {0} MSG:{1}", folderName, Ex.Message);
            }

            return(null);
        }
        /// <summary>
        /// Retrieves or creates the folder as a ListItem
        /// </summary>
        /// <param name="onlineLibrary">The target list for the folder</param>
        /// <param name="destinationFolder">The parent folder of the folter to be created</param>
        /// <param name="folderName">The folder to be created</param>
        /// <param name="defaultStartItemId">(OPTIONAL) If the list/library is above the threshold this will be the start index of the caml queries</param>
        /// <param name="defaultLastItemId">(OPTIONAL) If the list/library is above the threshold this will be the terminating index of the caml queries</param>
        /// <returns>The listitem as a folder</returns>
        public static Folder GetOrCreateFolder(this List onlineLibrary, Folder destinationFolder, string folderName, Nullable <int> defaultStartItemId = default(int?), Nullable <int> defaultLastItemId = default(int?))
        {
            if (!onlineLibrary.IsPropertyAvailable(lctx => lctx.BaseType))
            {
                onlineLibrary.Context.Load(onlineLibrary, lctx => lctx.BaseType, lctx => lctx.BaseTemplate);
                onlineLibrary.Context.ExecuteQueryRetry();
            }

            if (!destinationFolder.IsPropertyAvailable(fctx => fctx.ServerRelativeUrl) ||
                !destinationFolder.IsObjectPropertyInstantiated(fctx => fctx.Folders))
            {
                destinationFolder.Context.Load(destinationFolder, afold => afold.ServerRelativeUrl, afold => afold.Folders);
                destinationFolder.Context.ExecuteQueryRetry();
            }


            ListItem folderItem = null;

            var folderRelativeUrl = destinationFolder.ServerRelativeUrl;
            var camlFields        = new string[] { "Title", "ID" };
            var camlViewFields    = CAML.ViewFields(camlFields.Select(s => CAML.FieldRef(s)).ToArray());

            // Remove invalid characters
            var trimmedFolder  = HelperExtensions.GetCleanDirectory(folderName, string.Empty);
            var linkFileFilter = CAML.Eq(CAML.FieldValue("Title", FieldType.Text.ToString("f"), trimmedFolder));

            if (onlineLibrary.BaseType == BaseType.DocumentLibrary)
            {
                linkFileFilter = CAML.Or(
                    linkFileFilter,
                    CAML.Eq(CAML.FieldValue("LinkFilename", FieldType.Text.ToString("f"), trimmedFolder)));
            }

            var camlClause = CAML.And(
                CAML.Eq(CAML.FieldValue("FileDirRef", FieldType.Text.ToString("f"), folderRelativeUrl)),
                CAML.And(
                    CAML.Eq(CAML.FieldValue("FSObjType", FieldType.Integer.ToString("f"), 1.ToString())),
                    linkFileFilter
                    )
                );

            var camlQueries = onlineLibrary.SafeCamlClauseFromThreshold(1000, camlClause, defaultStartItemId, defaultLastItemId);

            foreach (var camlAndValue in camlQueries)
            {
                ListItemCollectionPosition itemPosition = null;
                var camlWhereClause = CAML.Where(camlAndValue);
                var camlQuery       = new CamlQuery()
                {
                    ViewXml = CAML.ViewQuery(
                        ViewScope.RecursiveAll,
                        camlWhereClause,
                        string.Empty,
                        camlViewFields,
                        5)
                };
                camlQuery.ListItemCollectionPosition = itemPosition;
                var listItems = onlineLibrary.GetItems(camlQuery);
                onlineLibrary.Context.Load(listItems, lti => lti.ListItemCollectionPosition);
                onlineLibrary.Context.ExecuteQueryRetry();

                if (listItems.Count() > 0)
                {
                    folderItem = listItems.FirstOrDefault();
                    System.Diagnostics.Trace.TraceInformation("Folder {0} exists in the destination folder.  Skip folder creation .....", folderName);
                    break;
                }
            }
            ;

            if (folderItem != null)
            {
                return(folderItem.Folder);
            }

            try
            {
                var info = new ListItemCreationInformation
                {
                    UnderlyingObjectType = FileSystemObjectType.Folder,
                    LeafName             = trimmedFolder,
                    FolderUrl            = folderRelativeUrl
                };

                folderItem          = onlineLibrary.AddItem(info);
                folderItem["Title"] = trimmedFolder;
                folderItem.Update();
                onlineLibrary.Context.ExecuteQueryRetry();
                System.Diagnostics.Trace.TraceInformation("{0} folder Created", trimmedFolder);
                return(folderItem.Folder);
            }
            catch (Exception Ex)
            {
                System.Diagnostics.Trace.TraceError("Failed to create or get folder for name {0} MSG:{1}", folderName, Ex.Message);
            }

            return(null);
        }