Example #1
0
        /// <summary>
        /// Upload document to library
        /// </summary>
        /// <param name="web">Site to be processed - can be root web or sub site</param>
        /// <param name="filePath">Path to source location like c:\fuu.txt</param>
        /// <param name="libraryName">Name of the document library</param>
        /// <param name="createLibrary">Should library be created if it's not present</param>
        public static void UploadDocumentToLibrary(this Web web, string filePath, string libraryName, bool createLibrary = false)
        {
            if (!web.ListExists(libraryName))
            {
                if (createLibrary)
                {
                    web.AddDocumentLibrary(libraryName);
                }
                else
                {
                    // have to abort, list does not exist.
                    string errorMessage = string.Format("Target list does not exist in the web. Web: {0}, List: {1}", web.Url, libraryName);
                    LoggingUtility.LogError(errorMessage, null, EventCategory.Unknown);
                    throw new WebException(errorMessage);
                }
            }

            UploadDocumentToLibrary(web.Lists.GetByTitle(libraryName), filePath);
        }
Example #2
0
        public static void UploadDocumentToLibrary(this Web web, string filePath, string libraryName, bool createLibrary = false)
        {
            if (!web.ListExists(libraryName))
            {
                if (createLibrary)
                {
                    web.AddDocumentLibrary(libraryName);
                }
                else
                {
                    LoggingUtility.Internal.TraceError((int)EventId.LibraryMissing, CoreResources.FileFolderExtensions_LibraryMissing, web.Url, libraryName);
                    // have to abort, list does not exist.
                    string errorMessage = string.Format(CoreResources.FileFolderExtensions_LibraryMissing, web.Url, libraryName);
                    throw new WebException(errorMessage);
                }
            }

            var list = web.Lists.GetByTitle(libraryName);

            list.RootFolder.UploadFile(filePath);
        }