文章目錄
SharePoint的文件庫非常方便,可以管理日常的文檔,更強大的是可以建立檔案夾來分類,通常在UI介面上是非常簡單了,點擊檔案夾,Create a Folder即可。但是,怎樣手動去建立檔案夾呢,怎樣在檔案夾中建立子檔案夾。
項目分析
- 舉個栗子,我需要上傳文檔到文件庫,希望把該檔案上傳到以年、月、日、上傳人命名的子檔案夾中,如:2013年-->5月-->11日-->陳小春,如所示這樣
代碼實現
//擷取年度參數 string folderYearParameter = DateTime.Now.Year.ToString() + "年"; //擷取月度參數 string folderMonthParameter = DateTime.Now.Month.ToString() + "月"; //擷取當前登入使用者名稱: string currentLoginName = SPContext.Current.Web.CurrentUser.LoginName; currentLoginName = currentLoginName.Substring(currentLoginName.LastIndexOf("\\") + 1); string currentUserDisplayName; //獲得Display name //當Administrator上傳有問題 ?因為 當administrator登陸時 DisplayName=Wllgwh\administrator,invalid Path SPUser spUser = SPContext.Current.Web.AllUsers[SPContext.Current.Web.CurrentUser.LoginName]; currentUserDisplayName = spUser.Name; if (currentUserDisplayName.Contains("\\")) { currentUserDisplayName = currentUserDisplayName.Substring(currentUserDisplayName.LastIndexOf("\\")+1); }
建立檔案夾之前,判斷是否存在該檔案夾,若不存在,則建立之。之後建立Byte數組,將上傳的文檔附加到SubFolder中。
SPSecurity.RunWithElevatedPrivileges(delegate() { var homeUrl = SPContext.Current.Site.Url; SPSite site = new SPSite(homeUrl + "/jxkp"); SPWeb web = site.OpenWeb(); //擷取List SPList list = web.Lists.TryGetList("績效考評"); SPFolder folderYear = list.ParentWeb.GetFolder(list.RootFolder.ServerRelativeUrl + "/" + folderYearParameter); if (!folderYear.Exists) { SPListItem folder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderYearParameter); web.AllowUnsafeUpdates = true; folder.Update(); web.AllowUnsafeUpdates = false; } //判斷folderMonth是否存在,不存在則建立 SPFolder folderMon = list.ParentWeb.GetFolder(list.RootFolder.ServerRelativeUrl + "/" + folderYearParameter + "/" + folderMonthParameter); if (!folderMon.Exists) { //建立子檔案夾 SPListItem subFolder = list.AddItem(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderYearParameter + "/" + folderMonthParameter); web.AllowUnsafeUpdates = true; subFolder.Update(); web.AllowUnsafeUpdates = false; } //判斷CurrentlyUser是否存在,不存在則建立,這麼長的一串,之後重構 SPFolder folderCurrUser = list.ParentWeb.GetFolder(list.RootFolder.ServerRelativeUrl + "/" + folderYearParameter + "/" + folderMonthParameter + "/" + currentUserDisplayName); if (!folderCurrUser.Exists) { SPListItem userFolder = list.AddItem(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderYearParameter + "/" + folderMonthParameter + "/" + currentUserDisplayName); web.AllowUnsafeUpdates = true; userFolder.Update(); web.AllowUnsafeUpdates = false; } if (fileUploadJxkp.PostedFile != null) { if (fileUploadJxkp.PostedFile.ContentLength > 0) { System.IO.Stream stream = fileUploadJxkp.PostedFile.InputStream; byte[] bytFile = new byte[Convert.ToInt32(fileUploadJxkp.PostedFile.ContentLength)]; stream.Read(bytFile, 0, Convert.ToInt32(fileUploadJxkp.PostedFile.ContentLength)); stream.Close(); SPFolder subSPFolder = list.ParentWeb.GetFolder(list.RootFolder.ServerRelativeUrl + "/" + folderYearParameter + "/" + folderMonthParameter + "/" + currentUserDisplayName); web.AllowUnsafeUpdates = true; subSPFolder.Files.Add(System.IO.Path.GetFileName(fileUploadJxkp.PostedFile.FileName), bytFile, true); web.AllowUnsafeUpdates = false; // labMsg.Text = "績效考評已上傳成功"; string successScript = string.Format("$.jnotify('{0}');", "上傳成功"); ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "smManager", successScript, true); } } });
小結
SharePoint的文件庫歸根到底還是List,學習SharePoint是條漫長的路,吾將上下而求索,有距,無懼。