Private voidAddfiletodocumentlibrary (stringDocumentlibraryurl,stringFileNamebyte[] file_bytes,stringItemtitletext) {Spsecurity.runwithelevatedprivileges (Delegate() { using(SPSite site =NewSPSite (Documentlibraryurl)) { using(SPWeb Web =site. OpenWeb ()) {web. Allowunsafeupdates=true; Spdocumentlibrary DocumentLibrary= (spdocumentlibrary) web. lists["Mydocumentlibraryname"]; SPFileCollection Files=DocumentLibrary.RootFolder.Files; SPFile NewFile= files. ADD (DocumentLibrary.RootFolder.Url +"/"+ filename, file_bytes,true); SPList documentlibraryaslist= Web. lists["Mydocumentlibraryname"]; SPListItem itemjustadded=Documentlibraryaslist.getitembyid (NewFile.ListItemAllFields.ID); Spcontenttype Documentcontenttype= documentlibraryaslist.contenttypes["Document"];//Amend with your document-derived custom Content Typeitemjustadded["Contenttypeid"] =documentcontenttype.id; itemjustadded["Title"] =Itemtitletext; //set other propeerties here.itemjustadded.update (); Newfile.checkin ("New", Spcheckintype.overwritecheckin); Web. Allowunsafeupdates=false; } } }); }
Add a file to a document Library and update metadata "properties in a" method to add documents