Programmatically upload a Document to SharePoint Library


using (SPSite siteCollection = new SPSite("SiteURL"))
            {
                using (SPWeb spWeb = siteCollection.OpenWeb())
                {
                    SPList spList = spWeb.Lists.TryGetList("Shared Documents");

                    string fileName = "new resume.doc";
                    FileStream fileStream = null;
                    Byte[] fileContent = null;

                    try
                    {
                        string docPath =@"D:\Kannan\"; //physical location of the file
                        fileStream = File.OpenRead(docPath + fileName);
                        fileContent = new byte[Convert.ToInt32(fileStream.Length)];
                        fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));

                        spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true);
                        spList.Update();
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        if (fileStream != null)
                        {
                            fileStream.Close();
                        }
                    }
                }
            }

Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell