/// <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 library does not exist in the web. Web: {0}, List: {1}", web.Url, libraryName); LoggingUtility.Internal.TraceError((int)EventId.LibraryMissing, errorMessage); throw new WebException(errorMessage); } } UploadDocumentToLibrary(web.Lists.GetByTitle(libraryName), filePath); }
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); }