Sunday, 22 February 2015

SharePoint 2010 Programatically Upload document and update column of document library

In this post i am sharing learning about "Upload a document  through File Upload control to a SharePoint document library and update a document library too", Below is the code

private void UploadFile(string fileName, byte[] fileBytes)
        {
                using (SPSite site = SPContext.Current.Site)
                {
                    if (site != null)
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPFolder docLibrary = web.Folders["DocumentLibraryName"];
                            if (docLibrary != null)
                            {
                                Boolean replaceExistingFiles = true;
                               
                                SPFile spFile = docLibrary.Files.Add(fileName, fileBytes, replaceExistingFiles);
                                spFile.Item["ColumnName"] = "Some information";
                               
                                spFile.Item.Update();
                                docLibrary.Update();
                            }
                        }
                    }
                }
            }

Cheers :)

No comments:

Post a Comment