Upload metadata file

Could you help me add the file to the Sharepoint document library? I found some articles in .NET, but I did not get a complete concept on how to do this.

I uploaded the file without metadata using this code:

if (fuDocument.PostedFile != null) { if (fuDocument.PostedFile.ContentLength > 0) { Stream fileStream = fuDocument.PostedFile.InputStream; byte[] byt = new byte[Convert.ToInt32(fuDocument.PostedFile.ContentLength)]; fileStream.Read(byt, 0, Convert.ToInt32(fuDocument.PostedFile.ContentLength)); fileStream.Close(); using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb webcollection = site.OpenWeb()) { SPFolder myfolder = webcollection.Folders["My Library"]; webcollection.AllowUnsafeUpdates = true; myfolder.Files.Add(System.IO.Path.GetFileName(fuDocument.PostedFile.FileName), byt); } } } } 

This code works fine as it is, but I need to upload a metadata file. Please help me by editing this code if possible. I created 3 columns in my document library.

+4
source share
2 answers

SPFolder.Files.Add returns an SPFile object

SPFile.Item returns an SPListItem object

Then you can use SPlistItem ["FieldName"] to access each field (see the bottom of the SPListItem link)

So by adding this to your code (this is not tested, but you should get an idea)

 SPFile file = myfolder.Files.Add(System.IO.Path.GetFileName(document.PostedFile.FileName); SPListItem item = file.Item; item["My Field"] = "Some value for your field"; item.Update() 
+15
source

There is also an overload in which you can send a hash table with the metadata you want to add. For instance:

 Hashtable metaData = new Hashtable(); metaData.Add("ContentTypeId", "some CT ID"); metaData.Add("Your Custom Field", "Your custom value"); SPFile file = library.RootFolder.Files.Add( "filename.fileextension", bytearray, metaData, false); 
+8
source

Source: https://habr.com/ru/post/1308337/


All Articles