Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file with metadata

Could you help me for how to add a file to the Sharepoint document library? I found some articles in .NET, but I didn't get the complete concept of how to accomplish this.

I uploaded a file without metadata by 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 is working fine as is, but I need to upload a file with metadata. Please help me by editing this code if it is possible. I created 3 columns in my document library.

like image 215
MAC Avatar asked Apr 29 '10 09:04

MAC


People also ask

What is metadata in file process?

File system metadata includes the times recorded by the operating system when a file is modified, accessed, or created.

How is metadata stored in a file?

Metadata can be stored in a variety of places. Where the metadata relates to databases, the data is often stored in tables and fields within the database. Sometimes the metadata exists in a specialist document or database designed to store such data, called a data dictionary or metadata repository.

How does file metadata work?

Metadata is created anytime a document, a file or other information asset is modified, including its deletion. Accurate metadata can be helpful in prolonging the lifespan of existing data by helping users find new ways to apply it. Metadata organizes a data object by using terms associated with that particular object.


1 Answers

SPFolder.Files.Add returns a SPFile object

SPFile.Item returns an SPListItem object

You can then use SPlistItem["FieldName"] to access each field (see bottom of SPListItem link)

So adding this into your code (this is not tested, but you should get the 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()
like image 71
Ryan Avatar answered Oct 02 '22 18:10

Ryan