Thursday, February 5, 2015

upload a file to a document library in sharepoint


String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointSite))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        if (!System.IO.File.Exists(fileToUpload))
            throw new FileNotFoundException("File not found.", fileToUpload);                   

        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit
        myLibrary.Update();
    }
}

 string filePath = @"C:\styles\MyStyles.css";
    string siteURL = "http://MyDomain.net/";
    string libraryName = "Style Library";

    using (SPSite oSite = new SPSite(siteURL))
    {
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            if (!System.IO.File.Exists(filePath))
                throw new FileNotFoundException("File not found.", filePath);                    

            SPFolder libFolder = oWeb.Folders[libraryName];

            // Prepare to upload
            string fileName = System.IO.Path.GetFileName(filePath);
            FileStream fileStream = File.OpenRead(filePath);

            //Check the existing File out if the Library Requires CheckOut
            if (libFolder.RequiresCheckout)
            {
                try {
                    SPFile fileOld = libFolder.Files[fileName];
                    fileOld.CheckOut();
                } catch {}
            }

            // Upload document
            SPFile spfile = libFolder.Files.Add(fileName, fileStream, true);

            // Commit 
            myLibrary.Update();

            //Check the File in and Publish a Major Version
            if (libFolder.RequiresCheckout)
            {
                    spFile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
                    spFile.Publish("Publish Comment");
            }
        }
    }

private void UploadToSharePoint(string p, out string newUrl)  //p is path to file to load
    {
        string siteUrl = "https://myCompany.sharepoint.com/site/";
        //Insert Credentials
        ClientContext context = new ClientContext(siteUrl);

        SecureString passWord = new SecureString();
        foreach (var c in "mypassword") passWord.AppendChar(c);
        context.Credentials = new SharePointOnlineCredentials("myUserName", passWord);
        Web site = context.Web;

        //Get the required RootFolder
        string barRootFolderRelativeUrl = "Shared Documents/foo/bar";
        Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);

        //Create new subFolder to load files into
        string newFolderName = baseName + DateTime.Now.ToString("yyyyMMddHHmm");
        barFolder.Folders.Add(newFolderName);
        barFolder.Update();

        //Add file to new Folder
        Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
        FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true };
        currentRunFolder.Files.Add(newFile);
        currentRunFolder.Update();

        context.ExecuteQuery();

        //Return the URL of the new uploaded file
        newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p);
    }
using (ClientContext clientContext = new ClientContext("http://contoso.com/subsite"))
{
    try
    {
        List documentLibrary = clientContext.Web.Lists.GetByTitle("List Title");
        FileCreationInformation newFile = new FileCreationInformation();
        newFile.Content = "Add File Stream Array";
        newFile.Overwrite = true;
        newFile.Url = "http://contoso.com/subsite/listurl/" + strFileName.Trim();
        Microsoft.SharePoint.Client.File uploadFile = documentLibrary.RootFolder.Files.Add(newFile);
    uploadFile.ListItemAllFields["Column Internal Name"] = "Column Internal Value";

        uploadFile.ListItemAllFields.Update();
        clientContext.ExecuteQuery();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

No comments:

Post a Comment