Tuesday, October 6, 2015

Upload file to a Document Library with File metadata

Upload file to a Document Library with  File metadata




  public SPListItem UploadFile(SPList listName)
        {
            SPListItem fileItem = null;
            try
            {
                SPWeb web = listName.ParentWeb;

                SPFolderCollection sfc = web.Folders;
                string path = web.Url + "/" + listName.Title.ToString(); // +"/" + fileUpload1.FileName.ToString().Split('.')[0];
                sfc.Add(path);

                SPFile sfile = web.Files.Add(path + "/" + fileUpload1.FileName.ToString() + System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName),
                fileUpload1.FileContent, true);
                listName.Update();
                fileItem = sfile.Item;
            }
            catch (Exception ex)
            {
                Message(lblerror, ex.Message);
            }

            return fileItem; // sfile.Item;

        }

No comments:

Post a Comment