Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Uploading Files to SharePoint Online Using Managed Code

$
0
0

Hi guys,

I am a newbie with SP Online :). I am trying to create a document library and upload documents to Sharepoint Online 2013.

I tried the code below but produces an error 403. Help please. Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint.Client;
//using Microsoft.SharePoint.Samples;
using Microsoft.IdentityModel.S2S.Tokens;
using System.Net;
using System.IO;
using System.Xml;

namespace SPProjecttestAppWeb.Pages
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var siteUrl = "https://barheadptyltd.sharepoint.com/sites/AndrewDeveloperSite/_layouts/15/start.aspx#/SitePages/DevHome.aspx";
            using (var context = new ClientContext(siteUrl))
            {
                try
                {
                    var web = context.Web;
                    var list = web.Lists.GetByTitle("Documents");
                    var filePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                            @"\ExcelTestList.xlsx";
                    var fci = new FileCreationInformation();
                    fci.Content = System.IO.File.ReadAllBytes(filePath);
                    fci.Url = "ExcelTestList.xlsx";
                    fci.Overwrite = true;
                    var file = list.RootFolder.Files.Add(fci);

                    var item = file.ListItemAllFields;
                    item["Year"] = DateTime.Now.Year;
                    item["Coordinator"] = web.CurrentUser;
                    item.Update();

                    context.ExecuteQuery();


                }
                catch (Exception)
                {

                    throw;
                }
            }
            
        }
    }
}


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>