ArticleDirectory
- Project Analysis
- Code Implementation
The document library of Sharepoint is very convenient and can manage daily documents. What's more powerful is that you can create folders for classification. It is usually very simple on the UI interface. Click folders, create a folder. However, how do I create folders manually and create subfolders in folders.
Project Analysis
- For example, I need to upload a document to the document library. I want to upload the file to a sub-folder named by year, month, day, or uploader, for example: june 17, --> June 11 --> Chen xiaochun, as shown in.
- Click February 1, 2013 to enter the current month sub-folder.
- Click the current month to enter the sub-folder named by the uploaded person.
- Click Upload (Chen xiaochun) to view the uploaded document.
Code Implementation
- First, we complete the parameter settings and initialize the parameters.
// Get annual Parameters String Folderyearparameter = datetime. Now. year. tostring () + " Year " ; // Obtain monthly Parameters String Foldermonthparameter = datetime. Now. Month. tostring () + " Month " ; // Obtain the current logon User Name: String Currentloginname =Spcontext. Current. Web. currentuser. loginname; currentloginname = Currentloginname. substring (currentloginname. lastindexof ( " \\ " ) + 1 ); String Currentuserdisplayname; // Get display name // When there is a problem with the Administrator upload? Because when the Administrator logs on, displayname = wllgwh \ administrator, invalid path Spuser =Spcontext. Current. Web. allusers [spcontext. Current. Web. currentuser. loginname]; currentuserdisplayname = Spuser. Name; If (Currentuserdisplayname. Contains ( " \\ " ) {Currentuserdisplayname = Currentuserdisplayname. substring (currentuserdisplayname. lastindexof ( " \\ " ) + 1 );}
Before creating a folder, check whether the folder exists. If it does not exist, create it. Then create a byte array and append the uploaded document to subfolder.
Spsecurity. runwithelevatedprivileges ( Delegate (){ VaR Homeurl = Spcontext. Current. Site. url; spsite site = New Spsite (homeurl + " /Jxkp " ); Spweb = Site. openweb (); // Get list Splist list = web. Lists. trygetlist ( " Performance Evaluation " ); 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 ;} // Determine whether foldermonth exists. If not, create Spfolder foldermon = List. parentweb. getfolder (list. rootfolder. serverrelativeurl + " / " + Folderyearparameter + " / " + Foldermonthparameter ); If (! Foldermon. exists ){ // Create a subfolder Splistitem subfolder = List. additem (list. rootfolder. serverrelativeurl, spfilesystemobjecttype. folder, folderyearparameter + " / " + Foldermonthparameter); web. allowunsafeupdates = True ; Subfolder. Update (); web. allowunsafeupdates = False ;} // Determine whether currentlyuser exists. If not, create a long string and then refactor it. 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 = "the performance evaluation has been uploaded successfully "; String Successscript =String . Format ( " $. Jnotify ('{0 }'); " , " Uploaded " ); Scriptmanager. registerstartupscript ( This . Page, This . GetType (), " Smmanager " , Successscript, True );} }});
Summary
In the final analysis, the document library of Sharepoint is still a list. Learning Sharepoint is a long way to go. I will search for it from the ground up and there is no fear.